Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | # Sanity checker for time.strftime |
| 4 | |
Eric S. Raymond | 83ff749 | 2001-02-09 12:03:45 +0000 | [diff] [blame] | 5 | import time, calendar, sys, os, re |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 6 | from test_support import verbose |
Guido van Rossum | 15d1079 | 1996-12-12 19:07:19 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 8 | def main(): |
| 9 | global verbose |
Barry Warsaw | 2108bc7 | 2001-03-23 20:24:07 +0000 | [diff] [blame] | 10 | # 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 Warsaw | 4c23b5f | 1996-12-13 18:08:58 +0000 | [diff] [blame] | 22 | now = time.time() |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 23 | 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 Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 28 | for i in range(25): |
| 29 | strftest(now + (i + j*100)*23*3603) |
Barry Warsaw | 4eb01cd | 1996-12-13 18:07:07 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 31 | def strftest(now): |
| 32 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 33 | print "strftime test for", time.ctime(now) |
Guido van Rossum | 3f11da0 | 1997-05-16 13:51:48 +0000 | [diff] [blame] | 34 | nowsecs = str(long(now))[:-1] |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 35 | gmt = time.gmtime(now) |
| 36 | now = time.localtime(now) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 37 | |
Jack Jansen | 00ce51e | 2000-09-15 12:57:35 +0000 | [diff] [blame] | 38 | if now[3] < 12: ampm='(AM|am)' |
| 39 | else: ampm='(PM|pm)' |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 41 | jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6)) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 9d9af2c | 1997-08-12 18:21:08 +0000 | [diff] [blame] | 43 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 44 | if now[8]: tz = time.tzname[1] |
| 45 | else: tz = time.tzname[0] |
Guido van Rossum | 9d9af2c | 1997-08-12 18:21:08 +0000 | [diff] [blame] | 46 | except AttributeError: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 47 | tz = '' |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 49 | if now[3] > 12: clock12 = now[3] - 12 |
| 50 | elif now[3] > 0: clock12 = now[3] |
| 51 | else: clock12 = 12 |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 53 | expectations = ( |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 54 | ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'), |
| 55 | ('%A', calendar.day_name[now[6]], 'full weekday name'), |
| 56 | ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'), |
| 57 | ('%B', calendar.month_name[now[1]], 'full month name'), |
| 58 | # %c see below |
| 59 | ('%d', '%02d' % now[2], 'day of month as number (00-31)'), |
| 60 | ('%H', '%02d' % now[3], 'hour (00-23)'), |
| 61 | ('%I', '%02d' % clock12, 'hour (01-12)'), |
| 62 | ('%j', '%03d' % now[7], 'julian day (001-366)'), |
| 63 | ('%m', '%02d' % now[1], 'month as number (01-12)'), |
| 64 | ('%M', '%02d' % now[4], 'minute, (00-59)'), |
| 65 | ('%p', ampm, 'AM or PM as appropriate'), |
| 66 | ('%S', '%02d' % now[5], 'seconds of current time (00-60)'), |
| 67 | ('%U', '%02d' % ((now[7] + jan1[6])/7), |
| 68 | 'week number of the year (Sun 1st)'), |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 69 | ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'), |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 70 | ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7), |
| 71 | 'week number of the year (Mon 1st)'), |
| 72 | # %x see below |
| 73 | ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
| 74 | ('%y', '%02d' % (now[0]%100), 'year without century'), |
| 75 | ('%Y', '%d' % now[0], 'year with century'), |
| 76 | # %Z see below |
| 77 | ('%%', '%', 'single percent sign'), |
| 78 | ) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 80 | nonstandard_expectations = ( |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 81 | # These are standard but don't have predictable output |
| 82 | ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'), |
| 83 | ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), |
| 84 | '%m/%d/%y %H:%M:%S'), |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 85 | ('%Z', '%s' % tz, 'time zone name'), |
Guido van Rossum | 2b41fdc | 1997-08-14 22:23:42 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 87 | # These are some platform specific extensions |
| 88 | ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'), |
| 89 | ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'), |
| 90 | ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'), |
| 91 | ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'), |
| 92 | ('%n', '\n', 'newline character'), |
| 93 | ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm), |
| 94 | '%I:%M:%S %p'), |
| 95 | ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'), |
| 96 | ('%s', nowsecs, 'seconds since the Epoch in UCT'), |
| 97 | ('%t', '\t', 'tab character'), |
| 98 | ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
| 99 | ('%3y', '%03d' % (now[0]%100), |
| 100 | 'year without century rendered using fieldwidth'), |
| 101 | ) |
Guido van Rossum | 15d1079 | 1996-12-12 19:07:19 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 103 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 104 | print "Strftime test, platform: %s, Python version: %s" % \ |
Eric S. Raymond | 83ff749 | 2001-02-09 12:03:45 +0000 | [diff] [blame] | 105 | (sys.platform, sys.version.split()[0]) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 107 | for e in expectations: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 108 | try: |
| 109 | result = time.strftime(e[0], now) |
| 110 | except ValueError, error: |
| 111 | print "Standard '%s' format gave error:" % e[0], error |
| 112 | continue |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 113 | if re.match(e[1], result): continue |
Guido van Rossum | 9b11279 | 1999-04-08 17:23:11 +0000 | [diff] [blame] | 114 | if not result or result[0] == '%': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 115 | print "Does not support standard '%s' format (%s)" % (e[0], e[2]) |
| 116 | else: |
| 117 | print "Conflict for %s (%s):" % (e[0], e[2]) |
| 118 | print " Expected %s, but got %s" % (e[1], result) |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 119 | |
| 120 | for e in nonstandard_expectations: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 121 | try: |
| 122 | result = time.strftime(e[0], now) |
| 123 | except ValueError, result: |
| 124 | if verbose: |
| 125 | print "Error for nonstandard '%s' format (%s): %s" % \ |
| 126 | (e[0], e[2], str(result)) |
| 127 | continue |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 128 | if re.match(e[1], result): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 129 | if verbose: |
| 130 | print "Supports nonstandard '%s' format (%s)" % (e[0], e[2]) |
Guido van Rossum | 0b7dd08 | 1999-04-08 20:22:46 +0000 | [diff] [blame] | 131 | elif not result or result[0] == '%': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 132 | if verbose: |
| 133 | print "Does not appear to support '%s' format (%s)" % (e[0], |
| 134 | e[2]) |
| 135 | else: |
| 136 | if verbose: |
| 137 | print "Conflict for nonstandard '%s' format (%s):" % (e[0], |
| 138 | e[2]) |
| 139 | print " Expected %s, but got %s" % (e[1], result) |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 140 | |
| 141 | def fixasctime(s): |
| 142 | if s[8] == ' ': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 143 | s = s[:8] + '0' + s[9:] |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 144 | return s |
| 145 | |
| 146 | main() |