3 # Sanity checker for time.strftime
5 import time
, calendar
, sys
, os
, re
6 from test
.test_support
import verbose
10 # 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.
17 java
.util
.Locale
.setDefault(java
.util
.Locale
.US
)
19 # Can't do this first because it will succeed, even in Jython
21 locale
.setlocale(locale
.LC_TIME
, 'C')
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):
29 strftest(now
+ (i
+ j
*100)*23*3603)
33 print "strftime test for", time
.ctime(now
)
34 nowsecs
= str(long(now
))[:-1]
35 gmt
= time
.gmtime(now
)
36 now
= time
.localtime(now
)
38 if now
[3] < 12: ampm
='(AM|am)'
41 jan1
= time
.localtime(time
.mktime((now
[0], 1, 1) + (0,)*6))
44 if now
[8]: tz
= time
.tzname
[1]
45 else: tz
= time
.tzname
[0]
46 except AttributeError:
49 if now
[3] > 12: clock12
= now
[3] - 12
50 elif now
[3] > 0: clock12
= now
[3]
54 ('%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'),
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)'),
69 ('%w', '0?%d' % ((1+now
[6]) % 7), 'weekday as a number (Sun 1st)'),
70 ('%W', '%02d' % ((now
[7] + (jan1
[6] - 1)%7)//7),
71 'week number of the year (Mon 1st)'),
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'),
77 ('%%', '%', 'single percent sign'),
80 nonstandard_expectations
= (
81 # 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)),
85 ('%Z', '%s' % tz
, 'time zone name'),
87 # 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
),
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'),
104 print "Strftime test, platform: %s, Python version: %s" % \
105 (sys
.platform
, sys
.version
.split()[0])
107 for e
in expectations
:
109 result
= time
.strftime(e
[0], now
)
110 except ValueError, error
:
111 print "Standard '%s' format gave error:" % e
[0], error
113 if re
.match(e
[1], result
): continue
114 if not result
or result
[0] == '%':
115 print "Does not support standard '%s' format (%s)" % (e
[0], e
[2])
117 print "Conflict for %s (%s):" % (e
[0], e
[2])
118 print " Expected %s, but got %s" % (e
[1], result
)
120 for e
in nonstandard_expectations
:
122 result
= time
.strftime(e
[0], now
)
123 except ValueError, result
:
125 print "Error for nonstandard '%s' format (%s): %s" % \
126 (e
[0], e
[2], str(result
))
128 if re
.match(e
[1], result
):
130 print "Supports nonstandard '%s' format (%s)" % (e
[0], e
[2])
131 elif not result
or result
[0] == '%':
133 print "Does not appear to support '%s' format (%s)" % (e
[0],
137 print "Conflict for nonstandard '%s' format (%s):" % (e
[0],
139 print " Expected %s, but got %s" % (e
[1], result
)
143 s
= s
[:8] + '0' + s
[9:]