PyXR

c:\python24\lib \ test \ test_strftime.py



0001 #! /usr/bin/env python
0002 
0003 # Sanity checker for time.strftime
0004 
0005 import time, calendar, sys, os, re
0006 from test.test_support import verbose
0007 
0008 def main():
0009     global verbose
0010     # For C Python, these tests expect C locale, so we try to set that
0011     # explicitly.  For Jython, Finn says we need to be in the US locale; my
0012     # understanding is that this is the closest Java gets to C's "C" locale.
0013     # Jython ought to supply an _locale module which Does The Right Thing, but
0014     # this is the best we can do given today's state of affairs.
0015     try:
0016         import java
0017         java.util.Locale.setDefault(java.util.Locale.US)
0018     except ImportError:
0019         # Can't do this first because it will succeed, even in Jython
0020         import locale
0021         locale.setlocale(locale.LC_TIME, 'C')
0022     now = time.time()
0023     strftest(now)
0024     verbose = 0
0025     # Try a bunch of dates and times,  chosen to vary through time of
0026     # day and daylight saving time
0027     for j in range(-5, 5):
0028         for i in range(25):
0029             strftest(now + (i + j*100)*23*3603)
0030 
0031 def escapestr(text, ampm):
0032     """Escape text to deal with possible locale values that have regex
0033     syntax while allowing regex syntax used for the comparison."""
0034     new_text = re.escape(text)
0035     new_text = new_text.replace(re.escape(ampm), ampm)
0036     new_text = new_text.replace("\%", "%")
0037     new_text = new_text.replace("\:", ":")
0038     new_text = new_text.replace("\?", "?")
0039     return new_text
0040 
0041 def strftest(now):
0042     if verbose:
0043         print "strftime test for", time.ctime(now)
0044     nowsecs = str(long(now))[:-1]
0045     gmt = time.gmtime(now)
0046     now = time.localtime(now)
0047 
0048     if now[3] < 12: ampm='(AM|am)'
0049     else: ampm='(PM|pm)'
0050 
0051     jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0)))
0052 
0053     try:
0054         if now[8]: tz = time.tzname[1]
0055         else: tz = time.tzname[0]
0056     except AttributeError:
0057         tz = ''
0058 
0059     if now[3] > 12: clock12 = now[3] - 12
0060     elif now[3] > 0: clock12 = now[3]
0061     else: clock12 = 12
0062 
0063     # Make sure any characters that could be taken as regex syntax is
0064     # escaped in escapestr()
0065     expectations = (
0066         ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
0067         ('%A', calendar.day_name[now[6]], 'full weekday name'),
0068         ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
0069         ('%B', calendar.month_name[now[1]], 'full month name'),
0070         # %c see below
0071         ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
0072         ('%H', '%02d' % now[3], 'hour (00-23)'),
0073         ('%I', '%02d' % clock12, 'hour (01-12)'),
0074         ('%j', '%03d' % now[7], 'julian day (001-366)'),
0075         ('%m', '%02d' % now[1], 'month as number (01-12)'),
0076         ('%M', '%02d' % now[4], 'minute, (00-59)'),
0077         ('%p', ampm, 'AM or PM as appropriate'),
0078         ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
0079         ('%U', '%02d' % ((now[7] + jan1[6])//7),
0080          'week number of the year (Sun 1st)'),
0081         ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
0082         ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)//7),
0083          'week number of the year (Mon 1st)'),
0084         # %x see below
0085         ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
0086         ('%y', '%02d' % (now[0]%100), 'year without century'),
0087         ('%Y', '%d' % now[0], 'year with century'),
0088         # %Z see below
0089         ('%%', '%', 'single percent sign'),
0090         )
0091 
0092     nonstandard_expectations = (
0093         # These are standard but don't have predictable output
0094         ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
0095         ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
0096          '%m/%d/%y %H:%M:%S'),
0097         ('%Z', '%s' % tz, 'time zone name'),
0098 
0099         # These are some platform specific extensions
0100         ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
0101         ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
0102         ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
0103         ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
0104         ('%n', '\n', 'newline character'),
0105         ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
0106          '%I:%M:%S %p'),
0107         ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
0108         ('%s', nowsecs, 'seconds since the Epoch in UCT'),
0109         ('%t', '\t', 'tab character'),
0110         ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
0111         ('%3y', '%03d' % (now[0]%100),
0112          'year without century rendered using fieldwidth'),
0113         )
0114 
0115     if verbose:
0116         print "Strftime test, platform: %s, Python version: %s" % \
0117               (sys.platform, sys.version.split()[0])
0118 
0119     for e in expectations:
0120         try:
0121             result = time.strftime(e[0], now)
0122         except ValueError, error:
0123             print "Standard '%s' format gave error:" % e[0], error
0124             continue
0125         if re.match(escapestr(e[1], ampm), result): continue
0126         if not result or result[0] == '%':
0127             print "Does not support standard '%s' format (%s)" % (e[0], e[2])
0128         else:
0129             print "Conflict for %s (%s):" % (e[0], e[2])
0130             print "  Expected %s, but got %s" % (e[1], result)
0131 
0132     for e in nonstandard_expectations:
0133         try:
0134             result = time.strftime(e[0], now)
0135         except ValueError, result:
0136             if verbose:
0137                 print "Error for nonstandard '%s' format (%s): %s" % \
0138                       (e[0], e[2], str(result))
0139             continue
0140         if re.match(escapestr(e[1], ampm), result):
0141             if verbose:
0142                 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
0143         elif not result or result[0] == '%':
0144             if verbose:
0145                 print "Does not appear to support '%s' format (%s)" % (e[0],
0146                                                                        e[2])
0147         else:
0148             if verbose:
0149                 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
0150                                                                       e[2])
0151                 print "  Expected %s, but got %s" % (e[1], result)
0152 
0153 def fixasctime(s):
0154     if s[8] == ' ':
0155         s = s[:8] + '0' + s[9:]
0156     return s
0157 
0158 main()
0159 

Generated by PyXR 0.9.4
SourceForge.net Logo