blob: c713d05cc0c699b908fb035f7e073fab002c201a [file] [log] [blame]
Guido van Rossum483705c1996-12-12 19:03:11 +00001#! /usr/bin/env python
2
3# Sanity checker for time.strftime
4
Guido van Rossum7944ea51998-09-14 15:50:40 +00005import time, calendar, sys, string, os, re
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):
Guido van Rossum41360a41998-03-26 19:42:58 +000016 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:
Guido van Rossum41360a41998-03-26 19:42:58 +000021 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:
Guido van Rossum41360a41998-03-26 19:42:58 +000032 if now[8]: tz = time.tzname[1]
33 else: tz = time.tzname[0]
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000034 except AttributeError:
Guido van Rossum41360a41998-03-26 19:42:58 +000035 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 = (
Guido van Rossum41360a41998-03-26 19:42:58 +000042 ('%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'),
46 # %c see below
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)'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000057 ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
Guido van Rossum41360a41998-03-26 19:42:58 +000058 ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7),
59 'week number of the year (Mon 1st)'),
60 # %x see below
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'),
64 # %Z see below
65 ('%%', '%', 'single percent sign'),
66 )
Guido van Rossum483705c1996-12-12 19:03:11 +000067
Guido van Rossume69be3e1997-03-07 20:30:03 +000068 nonstandard_expectations = (
Guido van Rossum41360a41998-03-26 19:42:58 +000069 # 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)),
72 '%m/%d/%y %H:%M:%S'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000073 ('%Z', '%s' % tz, 'time zone name'),
Guido van Rossum2b41fdc1997-08-14 22:23:42 +000074
Guido van Rossum41360a41998-03-26 19:42:58 +000075 # 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),
82 '%I:%M:%S %p'),
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'),
89 )
Guido van Rossum15d10791996-12-12 19:07:19 +000090
Guido van Rossume69be3e1997-03-07 20:30:03 +000091 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000092 print "Strftime test, platform: %s, Python version: %s" % \
93 (sys.platform, string.split(sys.version)[0])
Guido van Rossum483705c1996-12-12 19:03:11 +000094
Guido van Rossume69be3e1997-03-07 20:30:03 +000095 for e in expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +000096 try:
97 result = time.strftime(e[0], now)
98 except ValueError, error:
99 print "Standard '%s' format gave error:" % e[0], error
100 continue
Guido van Rossum7944ea51998-09-14 15:50:40 +0000101 if re.match(e[1], result): continue
Guido van Rossum9b112791999-04-08 17:23:11 +0000102 if not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000103 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
104 else:
105 print "Conflict for %s (%s):" % (e[0], e[2])
106 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000107
108 for e in nonstandard_expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +0000109 try:
110 result = time.strftime(e[0], now)
111 except ValueError, result:
112 if verbose:
113 print "Error for nonstandard '%s' format (%s): %s" % \
114 (e[0], e[2], str(result))
115 continue
Guido van Rossum7944ea51998-09-14 15:50:40 +0000116 if re.match(e[1], result):
Guido van Rossum41360a41998-03-26 19:42:58 +0000117 if verbose:
118 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
Guido van Rossum0b7dd081999-04-08 20:22:46 +0000119 elif not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000120 if verbose:
121 print "Does not appear to support '%s' format (%s)" % (e[0],
122 e[2])
123 else:
124 if verbose:
125 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
126 e[2])
127 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000128
129def fixasctime(s):
130 if s[8] == ' ':
Guido van Rossum41360a41998-03-26 19:42:58 +0000131 s = s[:8] + '0' + s[9:]
Guido van Rossume69be3e1997-03-07 20:30:03 +0000132 return s
133
134main()