blob: e9d3826f40fca2b397a982c212c38407b5fb7b00 [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
Barry Warsaw04f357c2002-07-23 19:04:11 +00006from test.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
Brett Cannonc82208e2004-03-20 23:09:40 +000031def escapestr(text, ampm):
32 """Escape text to deal with possible locale values that have regex
33 syntax while allowing regex syntax used for the comparison."""
34 new_text = re.escape(text)
35 new_text = new_text.replace(re.escape(ampm), ampm)
36 new_text = new_text.replace("\%", "%")
37 new_text = new_text.replace("\:", ":")
38 new_text = new_text.replace("\?", "?")
39 return new_text
40
Guido van Rossume69be3e1997-03-07 20:30:03 +000041def strftest(now):
42 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000043 print "strftime test for", time.ctime(now)
Guido van Rossum3f11da01997-05-16 13:51:48 +000044 nowsecs = str(long(now))[:-1]
Guido van Rossume69be3e1997-03-07 20:30:03 +000045 gmt = time.gmtime(now)
46 now = time.localtime(now)
Guido van Rossum483705c1996-12-12 19:03:11 +000047
Jack Jansen00ce51e2000-09-15 12:57:35 +000048 if now[3] < 12: ampm='(AM|am)'
49 else: ampm='(PM|pm)'
Guido van Rossum483705c1996-12-12 19:03:11 +000050
Brett Cannond1080a32004-03-02 04:38:10 +000051 jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0)))
Guido van Rossum483705c1996-12-12 19:03:11 +000052
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000053 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000054 if now[8]: tz = time.tzname[1]
55 else: tz = time.tzname[0]
Guido van Rossum9d9af2c1997-08-12 18:21:08 +000056 except AttributeError:
Guido van Rossum41360a41998-03-26 19:42:58 +000057 tz = ''
Guido van Rossum483705c1996-12-12 19:03:11 +000058
Guido van Rossume69be3e1997-03-07 20:30:03 +000059 if now[3] > 12: clock12 = now[3] - 12
60 elif now[3] > 0: clock12 = now[3]
61 else: clock12 = 12
Guido van Rossum483705c1996-12-12 19:03:11 +000062
Brett Cannonc82208e2004-03-20 23:09:40 +000063 # Make sure any characters that could be taken as regex syntax is
64 # escaped in escapestr()
Guido van Rossume69be3e1997-03-07 20:30:03 +000065 expectations = (
Guido van Rossum41360a41998-03-26 19:42:58 +000066 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
67 ('%A', calendar.day_name[now[6]], 'full weekday name'),
68 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
69 ('%B', calendar.month_name[now[1]], 'full month name'),
70 # %c see below
71 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
72 ('%H', '%02d' % now[3], 'hour (00-23)'),
73 ('%I', '%02d' % clock12, 'hour (01-12)'),
74 ('%j', '%03d' % now[7], 'julian day (001-366)'),
75 ('%m', '%02d' % now[1], 'month as number (01-12)'),
76 ('%M', '%02d' % now[4], 'minute, (00-59)'),
77 ('%p', ampm, 'AM or PM as appropriate'),
78 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
Guido van Rossum54e54c62001-09-04 19:14:14 +000079 ('%U', '%02d' % ((now[7] + jan1[6])//7),
Guido van Rossum41360a41998-03-26 19:42:58 +000080 'week number of the year (Sun 1st)'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000081 ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
Guido van Rossum54e54c62001-09-04 19:14:14 +000082 ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)//7),
Guido van Rossum41360a41998-03-26 19:42:58 +000083 'week number of the year (Mon 1st)'),
84 # %x see below
85 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
86 ('%y', '%02d' % (now[0]%100), 'year without century'),
87 ('%Y', '%d' % now[0], 'year with century'),
88 # %Z see below
89 ('%%', '%', 'single percent sign'),
90 )
Guido van Rossum483705c1996-12-12 19:03:11 +000091
Guido van Rossume69be3e1997-03-07 20:30:03 +000092 nonstandard_expectations = (
Guido van Rossum41360a41998-03-26 19:42:58 +000093 # These are standard but don't have predictable output
94 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
95 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
96 '%m/%d/%y %H:%M:%S'),
Guido van Rossum7944ea51998-09-14 15:50:40 +000097 ('%Z', '%s' % tz, 'time zone name'),
Guido van Rossum2b41fdc1997-08-14 22:23:42 +000098
Guido van Rossum41360a41998-03-26 19:42:58 +000099 # These are some platform specific extensions
100 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
101 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
102 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
103 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
104 ('%n', '\n', 'newline character'),
105 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
106 '%I:%M:%S %p'),
107 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
108 ('%s', nowsecs, 'seconds since the Epoch in UCT'),
109 ('%t', '\t', 'tab character'),
110 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
111 ('%3y', '%03d' % (now[0]%100),
112 'year without century rendered using fieldwidth'),
113 )
Guido van Rossum15d10791996-12-12 19:07:19 +0000114
Guido van Rossume69be3e1997-03-07 20:30:03 +0000115 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000116 print "Strftime test, platform: %s, Python version: %s" % \
Eric S. Raymond83ff7492001-02-09 12:03:45 +0000117 (sys.platform, sys.version.split()[0])
Guido van Rossum483705c1996-12-12 19:03:11 +0000118
Guido van Rossume69be3e1997-03-07 20:30:03 +0000119 for e in expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +0000120 try:
121 result = time.strftime(e[0], now)
122 except ValueError, error:
123 print "Standard '%s' format gave error:" % e[0], error
124 continue
Brett Cannonc82208e2004-03-20 23:09:40 +0000125 if re.match(escapestr(e[1], ampm), result): continue
Guido van Rossum9b112791999-04-08 17:23:11 +0000126 if not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000127 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
128 else:
129 print "Conflict for %s (%s):" % (e[0], e[2])
130 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000131
132 for e in nonstandard_expectations:
Guido van Rossum41360a41998-03-26 19:42:58 +0000133 try:
134 result = time.strftime(e[0], now)
135 except ValueError, result:
136 if verbose:
137 print "Error for nonstandard '%s' format (%s): %s" % \
138 (e[0], e[2], str(result))
139 continue
Brett Cannonc82208e2004-03-20 23:09:40 +0000140 if re.match(escapestr(e[1], ampm), result):
Guido van Rossum41360a41998-03-26 19:42:58 +0000141 if verbose:
142 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
Guido van Rossum0b7dd081999-04-08 20:22:46 +0000143 elif not result or result[0] == '%':
Guido van Rossum41360a41998-03-26 19:42:58 +0000144 if verbose:
145 print "Does not appear to support '%s' format (%s)" % (e[0],
146 e[2])
147 else:
148 if verbose:
149 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
150 e[2])
151 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossume69be3e1997-03-07 20:30:03 +0000152
153def fixasctime(s):
154 if s[8] == ' ':
Guido van Rossum41360a41998-03-26 19:42:58 +0000155 s = s[:8] + '0' + s[9:]
Guido van Rossume69be3e1997-03-07 20:30:03 +0000156 return s
157
158main()