blob: 47f35f52064ceda45c707cf5f3debbe061df2a3f [file] [log] [blame]
Guido van Rossum483705c1996-12-12 19:03:11 +00001#! /usr/bin/env python
2
3# Sanity checker for time.strftime
4
Eric S. Raymond83ff7492001-02-09 12:03:45 +00005import time, calendar, sys, os, re
Fredrik Lundhf7850422001-01-17 21:51:36 +00006from test_support import verbose
Guido van Rossum15d10791996-12-12 19:07:19 +00007
Guido van Rossume69be3e1997-03-07 20:30:03 +00008def main():
9 global verbose
Barry Warsaw2108bc72001-03-23 20:24:07 +000010 # For C Python, these tests expect C locale, so we try to set that
11 # explicitly. For Jython, Finn says we need to be in the US locale; my
12 # understanding is that this is the closest Java gets to C's "C" locale.
13 # Jython ought to supply an _locale module which Does The Right Thing, but
14 # this is the best we can do given today's state of affairs.
15 try:
16 import java
17 java.util.Locale.setDefault(java.util.Locale.US)
18 except ImportError:
19 # Can't do this first because it will succeed, even in Jython
20 import locale
21 locale.setlocale(locale.LC_TIME, 'C')
Barry Warsaw4c23b5f1996-12-13 18:08:58 +000022 now = time.time()
Guido van Rossume69be3e1997-03-07 20:30:03 +000023 strftest(now)
24 verbose = 0
25 # Try a bunch of dates and times, chosen to vary through time of
26 # day and daylight saving time
27 for j in range(-5, 5):
Guido van Rossum41360a41998-03-26 19:42:58 +000028 for i in range(25):
29 strftest(now + (i + j*100)*23*3603)
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000030
Guido van Rossume69be3e1997-03-07 20:30:03 +000031def strftest(now):
32 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000033 print "strftime test for", time.ctime(now)
Guido van Rossum3f11da01997-05-16 13:51:48 +000034 nowsecs = str(long(now))[:-1]
Guido van Rossume69be3e1997-03-07 20:30:03 +000035 gmt = time.gmtime(now)
36 now = time.localtime(now)
Guido van Rossum483705c1996-12-12 19:03:11 +000037
Jack Jansen00ce51e2000-09-15 12:57:35 +000038 if now[3] < 12: ampm='(AM|am)'
39 else: ampm='(PM|pm)'
Guido van Rossum483705c1996-12-12 19:03:11 +000040
Guido van Rossume69be3e1997-03-07 20:30:03 +000041 jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
Guido van Rossum483705c1996-12-12 19:03:11 +000042
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000043 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000044 if now[8]: tz = time.tzname[1]
45 else: tz = time.tzname[0]
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000046 except AttributeError:
Guido van Rossum41360a41998-03-26 19:42:58 +000047 tz = ''
Guido van Rossum483705c1996-12-12 19:03:11 +000048
Guido van Rossume69be3e1997-03-07 20:30:03 +000049 if now[3] > 12: clock12 = now[3] - 12
50 elif now[3] > 0: clock12 = now[3]
51 else: clock12 = 12
Guido van Rossum483705c1996-12-12 19:03:11 +000052
Guido van Rossume69be3e1997-03-07 20:30:03 +000053 expectations = (
Guido van Rossum41360a41998-03-26 19:42:58 +000054 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
55 ('%A', calendar.day_name[now[6]], 'full weekday name'),
56 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
57 ('%B', calendar.month_name[now[1]], 'full month name'),
58 # %c see below
59 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
60 ('%H', '%02d' % now[3], 'hour (00-23)'),
61 ('%I', '%02d' % clock12, 'hour (01-12)'),
62 ('%j', '%03d' % now[7], 'julian day (001-366)'),
63 ('%m', '%02d' % now[1], 'month as number (01-12)'),
64 ('%M', '%02d' % now[4], 'minute, (00-59)'),
65 ('%p', ampm, 'AM or PM as appropriate'),
66 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
67 ('%U', '%02d' % ((now[7] + jan1[6])/7),
68 'week number of the year (Sun 1st)'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000069 ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
Guido van Rossum41360a41998-03-26 19:42:58 +000070 ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7),
71 'week number of the year (Mon 1st)'),
72 # %x see below
73 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
74 ('%y', '%02d' % (now[0]%100), 'year without century'),
75 ('%Y', '%d' % now[0], 'year with century'),
76 # %Z see below
77 ('%%', '%', 'single percent sign'),
78 )
Guido van Rossum483705c1996-12-12 19:03:11 +000079
Guido van Rossume69be3e1997-03-07 20:30:03 +000080 nonstandard_expectations = (
Guido van Rossum41360a41998-03-26 19:42:58 +000081 # These are standard but don't have predictable output
82 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
83 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
84 '%m/%d/%y %H:%M:%S'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000085 ('%Z', '%s' % tz, 'time zone name'),
Guido van Rossum2b41fdc1997-08-14 22:23:42 +000086
Guido van Rossum41360a41998-03-26 19:42:58 +000087 # These are some platform specific extensions
88 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
89 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
90 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
91 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
92 ('%n', '\n', 'newline character'),
93 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
94 '%I:%M:%S %p'),
95 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
96 ('%s', nowsecs, 'seconds since the Epoch in UCT'),
97 ('%t', '\t', 'tab character'),
98 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
99 ('%3y', '%03d' % (now[0]%100),
100 'year without century rendered using fieldwidth'),
101 )
Guido van Rossum15d10791996-12-12 19:07:19 +0000102
Guido van Rossume69be3e1997-03-07 20:30:03 +0000103 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000104 print "Strftime test, platform: %s, Python version: %s" % \
Eric S. Raymond83ff7492001-02-09 12:03:45 +0000105 (sys.platform, sys.version.split()[0])
Guido van Rossum483705c1996-12-12 19:03:11 +0000106
Guido van Rossume69be3e1997-03-07 20:30:03 +0000107 for e in expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +0000108 try:
109 result = time.strftime(e[0], now)
110 except ValueError, error:
111 print "Standard '%s' format gave error:" % e[0], error
112 continue
Guido van Rossum7944ea51998-09-14 15:50:40 +0000113 if re.match(e[1], result): continue
Guido van Rossum9b112791999-04-08 17:23:11 +0000114 if not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000115 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
116 else:
117 print "Conflict for %s (%s):" % (e[0], e[2])
118 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000119
120 for e in nonstandard_expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +0000121 try:
122 result = time.strftime(e[0], now)
123 except ValueError, result:
124 if verbose:
125 print "Error for nonstandard '%s' format (%s): %s" % \
126 (e[0], e[2], str(result))
127 continue
Guido van Rossum7944ea51998-09-14 15:50:40 +0000128 if re.match(e[1], result):
Guido van Rossum41360a41998-03-26 19:42:58 +0000129 if verbose:
130 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
Guido van Rossum0b7dd081999-04-08 20:22:46 +0000131 elif not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000132 if verbose:
133 print "Does not appear to support '%s' format (%s)" % (e[0],
134 e[2])
135 else:
136 if verbose:
137 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
138 e[2])
139 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000140
141def fixasctime(s):
142 if s[8] == ' ':
Guido van Rossum41360a41998-03-26 19:42:58 +0000143 s = s[:8] + '0' + s[9:]
Guido van Rossume69be3e1997-03-07 20:30:03 +0000144 return s
145
146main()