3 # Sanity checker for time.strftime
5 import time
, calendar
, sys
, string
, os
, re
6 from test_support
import verbose
13 # Try a bunch of dates and times, chosen to vary through time of
14 # day and daylight saving time
15 for j
in range(-5, 5):
17 strftest(now
+ (i
+ j
*100)*23*3603)
21 print "strftime test for", time
.ctime(now
)
22 nowsecs
= str(long(now
))[:-1]
23 gmt
= time
.gmtime(now
)
24 now
= time
.localtime(now
)
26 if now
[3] < 12: ampm
='AM'
29 jan1
= time
.localtime(time
.mktime((now
[0], 1, 1) + (0,)*6))
32 if now
[8]: tz
= time
.tzname
[1]
33 else: tz
= time
.tzname
[0]
34 except AttributeError:
37 if now
[3] > 12: clock12
= now
[3] - 12
38 elif now
[3] > 0: clock12
= now
[3]
42 ('%a', calendar
.day_abbr
[now
[6]], 'abbreviated weekday name'),
43 ('%A', calendar
.day_name
[now
[6]], 'full weekday name'),
44 ('%b', calendar
.month_abbr
[now
[1]], 'abbreviated month name'),
45 ('%B', calendar
.month_name
[now
[1]], 'full month name'),
47 ('%d', '%02d' % now
[2], 'day of month as number (00-31)'),
48 ('%H', '%02d' % now
[3], 'hour (00-23)'),
49 ('%I', '%02d' % clock12
, 'hour (01-12)'),
50 ('%j', '%03d' % now
[7], 'julian day (001-366)'),
51 ('%m', '%02d' % now
[1], 'month as number (01-12)'),
52 ('%M', '%02d' % now
[4], 'minute, (00-59)'),
53 ('%p', ampm
, 'AM or PM as appropriate'),
54 ('%S', '%02d' % now
[5], 'seconds of current time (00-60)'),
55 ('%U', '%02d' % ((now
[7] + jan1
[6])/7),
56 'week number of the year (Sun 1st)'),
57 ('%w', '0?%d' % ((1+now
[6]) % 7), 'weekday as a number (Sun 1st)'),
58 ('%W', '%02d' % ((now
[7] + (jan1
[6] - 1)%7)/7),
59 'week number of the year (Mon 1st)'),
61 ('%X', '%02d:%02d:%02d' % (now
[3], now
[4], now
[5]), '%H:%M:%S'),
62 ('%y', '%02d' % (now
[0]%100), 'year without century'),
63 ('%Y', '%d' % now
[0], 'year with century'),
65 ('%%', '%', 'single percent sign'),
68 nonstandard_expectations
= (
69 # These are standard but don't have predictable output
70 ('%c', fixasctime(time
.asctime(now
)), 'near-asctime() format'),
71 ('%x', '%02d/%02d/%02d' % (now
[1], now
[2], (now
[0]%100)),
73 ('%Z', '%s' % tz
, 'time zone name'),
75 # These are some platform specific extensions
76 ('%D', '%02d/%02d/%02d' % (now
[1], now
[2], (now
[0]%100)), 'mm/dd/yy'),
77 ('%e', '%2d' % now
[2], 'day of month as number, blank padded ( 0-31)'),
78 ('%h', calendar
.month_abbr
[now
[1]], 'abbreviated month name'),
79 ('%k', '%2d' % now
[3], 'hour, blank padded ( 0-23)'),
80 ('%n', '\n', 'newline character'),
81 ('%r', '%02d:%02d:%02d %s' % (clock12
, now
[4], now
[5], ampm
),
83 ('%R', '%02d:%02d' % (now
[3], now
[4]), '%H:%M'),
84 ('%s', nowsecs
, 'seconds since the Epoch in UCT'),
85 ('%t', '\t', 'tab character'),
86 ('%T', '%02d:%02d:%02d' % (now
[3], now
[4], now
[5]), '%H:%M:%S'),
87 ('%3y', '%03d' % (now
[0]%100),
88 'year without century rendered using fieldwidth'),
92 print "Strftime test, platform: %s, Python version: %s" % \
93 (sys
.platform
, string
.split(sys
.version
)[0])
95 for e
in expectations
:
97 result
= time
.strftime(e
[0], now
)
98 except ValueError, error
:
99 print "Standard '%s' format gave error:" % e
[0], error
101 if re
.match(e
[1], result
): continue
102 if not result
or result
[0] == '%':
103 print "Does not support standard '%s' format (%s)" % (e
[0], e
[2])
105 print "Conflict for %s (%s):" % (e
[0], e
[2])
106 print " Expected %s, but got %s" % (e
[1], result
)
108 for e
in nonstandard_expectations
:
110 result
= time
.strftime(e
[0], now
)
111 except ValueError, result
:
113 print "Error for nonstandard '%s' format (%s): %s" % \
114 (e
[0], e
[2], str(result
))
116 if re
.match(e
[1], result
):
118 print "Supports nonstandard '%s' format (%s)" % (e
[0], e
[2])
119 elif not result
or result
[0] == '%':
121 print "Does not appear to support '%s' format (%s)" % (e
[0],
125 print "Conflict for nonstandard '%s' format (%s):" % (e
[0],
127 print " Expected %s, but got %s" % (e
[1], result
)
131 s
= s
[:8] + '0' + s
[9:]