blob: 7128fd740832545b1d41b2643a4c1d1a6237df9a [file] [log] [blame]
Christian Heimesb186d002008-03-18 15:15:01 +00001"""
2Unittest for time.strftime
3"""
Guido van Rossum483705c1996-12-12 19:03:11 +00004
Christian Heimesb186d002008-03-18 15:15:01 +00005import calendar
6import sys
7import os
8import re
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Christian Heimesb186d002008-03-18 15:15:01 +000010import time
11import unittest
Guido van Rossum483705c1996-12-12 19:03:11 +000012
Guido van Rossum15d10791996-12-12 19:07:19 +000013
Christian Heimesb186d002008-03-18 15:15:01 +000014# helper functions
Guido van Rossume69be3e1997-03-07 20:30:03 +000015def fixasctime(s):
16 if s[8] == ' ':
Guido van Rossum41360a41998-03-26 19:42:58 +000017 s = s[:8] + '0' + s[9:]
Guido van Rossume69be3e1997-03-07 20:30:03 +000018 return s
19
Christian Heimesb186d002008-03-18 15:15:01 +000020def escapestr(text, ampm):
21 """
22 Escape text to deal with possible locale values that have regex
23 syntax while allowing regex syntax used for comparison.
24 """
25 new_text = re.escape(text)
26 new_text = new_text.replace(re.escape(ampm), ampm)
27 new_text = new_text.replace('\%', '%')
28 new_text = new_text.replace('\:', ':')
29 new_text = new_text.replace('\?', '?')
30 return new_text
31
32
33class StrftimeTest(unittest.TestCase):
34
35 def _update_variables(self, now):
36 # we must update the local variables on every cycle
37 self.gmt = time.gmtime(now)
38 now = time.localtime(now)
39
40 if now[3] < 12: self.ampm='(AM|am)'
41 else: self.ampm='(PM|pm)'
42
43 self.jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0)))
44
45 try:
46 if now[8]: self.tz = time.tzname[1]
47 else: self.tz = time.tzname[0]
48 except AttributeError:
49 self.tz = ''
50
51 if now[3] > 12: self.clock12 = now[3] - 12
52 elif now[3] > 0: self.clock12 = now[3]
53 else: self.clock12 = 12
54
55 self.now = now
56
57 def setUp(self):
58 try:
59 import java
60 java.util.Locale.setDefault(java.util.Locale.US)
61 except ImportError:
62 import locale
63 locale.setlocale(locale.LC_TIME, 'C')
64
65 def test_strftime(self):
66 now = time.time()
67 self._update_variables(now)
68 self.strftest1(now)
69 self.strftest2(now)
70
Benjamin Petersonee8712c2008-05-20 21:35:26 +000071 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +000072 print("Strftime test, platform: %s, Python version: %s" % \
73 (sys.platform, sys.version.split()[0]))
74
75 for j in range(-5, 5):
76 for i in range(25):
77 arg = now + (i+j*100)*23*3603
78 self._update_variables(arg)
79 self.strftest1(arg)
80 self.strftest2(arg)
81
82 def strftest1(self, now):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000083 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +000084 print("strftime test for", time.ctime(now))
85 now = self.now
86 # Make sure any characters that could be taken as regex syntax is
87 # escaped in escapestr()
88 expectations = (
89 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
90 ('%A', calendar.day_name[now[6]], 'full weekday name'),
91 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
92 ('%B', calendar.month_name[now[1]], 'full month name'),
93 # %c see below
94 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
95 ('%H', '%02d' % now[3], 'hour (00-23)'),
96 ('%I', '%02d' % self.clock12, 'hour (01-12)'),
97 ('%j', '%03d' % now[7], 'julian day (001-366)'),
98 ('%m', '%02d' % now[1], 'month as number (01-12)'),
99 ('%M', '%02d' % now[4], 'minute, (00-59)'),
100 ('%p', self.ampm, 'AM or PM as appropriate'),
101 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
102 ('%U', '%02d' % ((now[7] + self.jan1[6])//7),
103 'week number of the year (Sun 1st)'),
104 ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
105 ('%W', '%02d' % ((now[7] + (self.jan1[6] - 1)%7)//7),
106 'week number of the year (Mon 1st)'),
107 # %x see below
108 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
109 ('%y', '%02d' % (now[0]%100), 'year without century'),
110 ('%Y', '%d' % now[0], 'year with century'),
111 # %Z see below
112 ('%%', '%', 'single percent sign'),
113 )
114
115 for e in expectations:
116 # musn't raise a value error
117 try:
118 result = time.strftime(e[0], now)
119 except ValueError as error:
120 print("Standard '%s' format gaver error:" % (e[0], error))
121 continue
122 if re.match(escapestr(e[1], self.ampm), result):
123 continue
124 if not result or result[0] == '%':
125 print("Does not support standard '%s' format (%s)" % \
126 (e[0], e[2]))
127 else:
128 print("Conflict for %s (%s):" % (e[0], e[2]))
129 print(" Expected %s, but got %s" % (e[1], result))
130
131 def strftest2(self, now):
132 nowsecs = str(int(now))[:-1]
133 now = self.now
134
135 nonstandard_expectations = (
136 # These are standard but don't have predictable output
137 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
138 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
139 '%m/%d/%y %H:%M:%S'),
140 ('%Z', '%s' % self.tz, 'time zone name'),
141
142 # These are some platform specific extensions
143 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
144 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
145 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
146 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
147 ('%n', '\n', 'newline character'),
148 ('%r', '%02d:%02d:%02d %s' % (self.clock12, now[4], now[5], self.ampm),
149 '%I:%M:%S %p'),
150 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
151 ('%s', nowsecs, 'seconds since the Epoch in UCT'),
152 ('%t', '\t', 'tab character'),
153 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
154 ('%3y', '%03d' % (now[0]%100),
155 'year without century rendered using fieldwidth'),
156 )
157
158
159 for e in nonstandard_expectations:
160 try:
161 result = time.strftime(e[0], now)
162 except ValueError as result:
163 msg = "Error for nonstandard '%s' format (%s): %s" % \
164 (e[0], e[2], str(result))
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000165 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +0000166 print(msg)
167 continue
168 if re.match(escapestr(e[1], self.ampm), result):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000169 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +0000170 print("Supports nonstandard '%s' format (%s)" % (e[0], e[2]))
171 elif not result or result[0] == '%':
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000172 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +0000173 print("Does not appear to support '%s' format (%s)" % \
174 (e[0], e[2]))
175 else:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000176 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +0000177 print("Conflict for nonstandard '%s' format (%s):" % \
178 (e[0], e[2]))
179 print(" Expected %s, but got %s" % (e[1], result))
180
181def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000182 support.run_unittest(StrftimeTest)
Christian Heimesb186d002008-03-18 15:15:01 +0000183
184if __name__ == '__main__':
185 test_main()