blob: e97c011a3d0e94d50995e5228d44b7fc510e3a55 [file] [log] [blame]
Guido van Rossum483705c1996-12-12 19:03:11 +00001#! /usr/bin/env python
2
3# Sanity checker for time.strftime
4
5import time, calendar, sys, string, os
Guido van Rossum2bde7831996-12-20 03:03:39 +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 Warsaw4c23b5f1996-12-13 18:08:58 +000010 now = time.time()
Guido van Rossume69be3e1997-03-07 20:30:03 +000011 strftest(now)
12 verbose = 0
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):
16 for i in range(25):
17 strftest(now + (i + j*100)*23*3603)
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000018
Guido van Rossume69be3e1997-03-07 20:30:03 +000019def strftest(now):
20 if verbose:
21 print "strftime test for", time.ctime(now)
Guido van Rossum3f11da01997-05-16 13:51:48 +000022 nowsecs = str(long(now))[:-1]
Guido van Rossume69be3e1997-03-07 20:30:03 +000023 gmt = time.gmtime(now)
24 now = time.localtime(now)
Guido van Rossum483705c1996-12-12 19:03:11 +000025
Guido van Rossume69be3e1997-03-07 20:30:03 +000026 if now[3] < 12: ampm='AM'
27 else: ampm='PM'
Guido van Rossum483705c1996-12-12 19:03:11 +000028
Guido van Rossume69be3e1997-03-07 20:30:03 +000029 jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
Guido van Rossum483705c1996-12-12 19:03:11 +000030
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000031 try:
32 if now[8]: tz = time.tzname[1]
33 else: tz = time.tzname[0]
34 except AttributeError:
35 tz = ''
Guido van Rossum483705c1996-12-12 19:03:11 +000036
Guido van Rossume69be3e1997-03-07 20:30:03 +000037 if now[3] > 12: clock12 = now[3] - 12
38 elif now[3] > 0: clock12 = now[3]
39 else: clock12 = 12
Guido van Rossum483705c1996-12-12 19:03:11 +000040
Guido van Rossume69be3e1997-03-07 20:30:03 +000041 expectations = (
42 ('%A', calendar.day_name[now[6]], 'full weekday name'),
43 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
44 ('%B', calendar.month_name[now[1]], 'full month name'),
45 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
46 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
47 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
48 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
49 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
50 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
51 ('%H', '%02d' % now[3], 'hour (00-23)'),
52 ('%I', '%02d' % clock12, 'hour (01-12)'),
53 ('%j', '%03d' % now[7], 'julian day (001-366)'),
54 ('%M', '%02d' % now[4], 'minute, (00-59)'),
55 ('%m', '%02d' % now[1], 'month as number (01-12)'),
56 ('%p', ampm, 'AM or PM as appropriate'),
57 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
58 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
59 '%I:%M:%S %p'),
60 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
61 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
62 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
63 ('%U', '%02d' % ((now[7] + jan1[6])/7),
64 'week number of the year (Sun 1st)'),
65 ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7),
66 'week number of the year (Mon 1st)'),
67 ('%w', '%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
68 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
69 '%m/%d/%y %H:%M:%S'),
70 ('%Y', '%d' % now[0], 'year with century'),
71 ('%y', '%02d' % (now[0]%100), 'year without century'),
Guido van Rossume69be3e1997-03-07 20:30:03 +000072 ('%%', '%', 'single percent sign'),
73 )
Guido van Rossum483705c1996-12-12 19:03:11 +000074
Guido van Rossume69be3e1997-03-07 20:30:03 +000075 nonstandard_expectations = (
76 ('%C', '%02d' % (now[0]/100), 'century'),
77 # This is for IRIX; on Solaris, %C yields date(1) format.
78 # Tough.
79 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
Guido van Rossum3f11da01997-05-16 13:51:48 +000080 ('%s', nowsecs, 'seconds since the Epoch in UCT'),
Guido van Rossume69be3e1997-03-07 20:30:03 +000081 ('%3y', '%03d' % (now[0]%100),
82 'year without century rendered using fieldwidth'),
83 ('%n', '\n', 'newline character'),
84 ('%t', '\t', 'tab character'),
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000085 ('%Z', tz, 'time zone name'),
Guido van Rossume69be3e1997-03-07 20:30:03 +000086 )
Guido van Rossum15d10791996-12-12 19:07:19 +000087
Guido van Rossume69be3e1997-03-07 20:30:03 +000088 if verbose:
89 print "Strftime test, platform: %s, Python version: %s" % \
90 (sys.platform, string.split(sys.version)[0])
Guido van Rossum483705c1996-12-12 19:03:11 +000091
Guido van Rossume69be3e1997-03-07 20:30:03 +000092 for e in expectations:
Guido van Rossum62bd30c1997-04-11 22:26:42 +000093 try:
94 result = time.strftime(e[0], now)
95 except ValueError, error:
96 print "Standard '%s' format gave error:" % e[0], error
97 continue
Guido van Rossume69be3e1997-03-07 20:30:03 +000098 if result == e[1]: continue
99 if result[0] == '%':
100 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
101 else:
Guido van Rossum5eaf4571996-12-18 18:03:10 +0000102 print "Conflict for %s (%s):" % (e[0], e[2])
103 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000104
105 for e in nonstandard_expectations:
Guido van Rossum62bd30c1997-04-11 22:26:42 +0000106 try:
107 result = time.strftime(e[0], now)
108 except ValueError, result:
109 if verbose:
110 print "Error for nonstandard '%s' format (%s): %s" % \
Guido van Rossum4dfd4581997-05-14 21:38:03 +0000111 (e[0], e[2], str(result))
Guido van Rossum62bd30c1997-04-11 22:26:42 +0000112 continue
Guido van Rossume69be3e1997-03-07 20:30:03 +0000113 if result == e[1]:
114 if verbose:
115 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
116 elif result[0] == '%':
117 if verbose:
Guido van Rossum62bd30c1997-04-11 22:26:42 +0000118 print "Does not appear to support '%s' format (%s)" % (e[0],
119 e[2])
Guido van Rossume69be3e1997-03-07 20:30:03 +0000120 else:
121 if verbose:
Guido van Rossum62bd30c1997-04-11 22:26:42 +0000122 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
123 e[2])
Guido van Rossume69be3e1997-03-07 20:30:03 +0000124 print " Expected %s, but got %s" % (e[1], result)
125
126def fixasctime(s):
127 if s[8] == ' ':
128 s = s[:8] + '0' + s[9:]
129 return s
130
131main()