def test_offset(self): one_hour = 60 * 60 half_hour = 30 * 60 half_minute = 30 (*_, offset), _, offset_fraction = _strptime._strptime("+0130", "%z") self.assertEqual(offset, one_hour + half_hour) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-0100", "%z") self.assertEqual(offset, -one_hour) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-013030", "%z") self.assertEqual(offset, -(one_hour + half_hour + half_minute)) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-013030.000001", "%z") self.assertEqual(offset, -(one_hour + half_hour + half_minute)) self.assertEqual(offset_fraction, -1) (*_, offset), _, offset_fraction = _strptime._strptime("+01:00", "%z") self.assertEqual(offset, one_hour) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-01:30", "%z") self.assertEqual(offset, -(one_hour + half_hour)) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30", "%z") self.assertEqual(offset, -(one_hour + half_hour + half_minute)) self.assertEqual(offset_fraction, 0) (*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30.000001", "%z") self.assertEqual(offset, -(one_hour + half_hour + half_minute)) self.assertEqual(offset_fraction, -1) (*_, offset), _, offset_fraction = _strptime._strptime("+01:30:30.001", "%z") self.assertEqual(offset, one_hour + half_hour + half_minute) self.assertEqual(offset_fraction, 1000) (*_, offset), _, offset_fraction = _strptime._strptime("Z", "%z") self.assertEqual(offset, 0) self.assertEqual(offset_fraction, 0)
def test_ValueError(self): # Make sure ValueError is raised when match fails or format is bad self.assertRaises(ValueError, _strptime._strptime_time, data_string="%d", format="%A") for bad_format in ("%", "% ", "%e"): try: _strptime._strptime_time("2005", bad_format) except ValueError: continue except Exception as err: self.fail("'%s' raised %s, not ValueError" % (bad_format, err.__class__.__name__)) else: self.fail("'%s' did not raise ValueError" % bad_format) # Ambiguous or incomplete cases using ISO year/week/weekday directives # 1. ISO week (%V) is specified, but the year is specified with %Y # instead of %G with self.assertRaises(ValueError): _strptime._strptime("1999 50", "%Y %V") # 2. ISO year (%G) and ISO week (%V) are specified, but weekday is not with self.assertRaises(ValueError): _strptime._strptime("1999 51", "%G %V") # 3. ISO year (%G) and weekday are specified, but ISO week (%V) is not for w in ('A', 'a', 'w', 'u'): with self.assertRaises(ValueError): _strptime._strptime("1999 51","%G %{}".format(w)) # 4. ISO year is specified alone (e.g. time.strptime('2015', '%G')) with self.assertRaises(ValueError): _strptime._strptime("2015", "%G") # 5. Julian/ordinal day (%j) is specified with %G, but not %Y with self.assertRaises(ValueError): _strptime._strptime("1999 256", "%G %j")
def test_bad_offset(self): with self.assertRaises(ValueError): _strptime._strptime("-01:30:30.", "%z") with self.assertRaises(ValueError): _strptime._strptime("-0130:30", "%z") with self.assertRaises(ValueError): _strptime._strptime("-01:30:30.1234567", "%z") with self.assertRaises(ValueError): _strptime._strptime("-01:30:30:123456", "%z") with self.assertRaises(ValueError) as err: _strptime._strptime("-01:3030", "%z") self.assertEqual("Unconsistent use of : in -01:3030", str(err.exception))
def test_fraction(self): # Test microseconds import datetime d = datetime.datetime(2012, 12, 20, 12, 34, 56, 78987) tup, frac = _strptime._strptime(str(d), format="%Y-%m-%d %H:%M:%S.%f") self.assertEqual(frac, d.microsecond)
def test_fraction(self): # Test microseconds import datetime d = datetime.datetime(2012, 12, 20, 12, 34, 56, 78987) tup, frac, _ = _strptime._strptime(str(d), format="%Y-%m-%d %H:%M:%S.%f") self.assertEqual(frac, d.microsecond)
def strptime(string, format="%a %b %d %H:%M:%S %Y"): """strptime(string, format) -> struct_time Parse a string to a time tuple according to a format specification. See the library reference manual for formatting codes (same as strftime()).""" import _strptime # from the CPython standard library return _strptime._strptime(string, format)[0]
def test_ValueError(self): self.assertRaises(ValueError, _strptime._strptime_time, data_string ='%d', format='%A') for bad_format in ('%', '% ', '%e'): try: _strptime._strptime_time('2005', bad_format) except ValueError: continue except Exception as err: self.fail("'%s' raised %s, not ValueError" % (bad_format, err.__class__.__name__)) else: self.fail("'%s' did not raise ValueError" % bad_format) with self.assertRaises(ValueError): _strptime._strptime('1999 50', '%Y %V') with self.assertRaises(ValueError): _strptime._strptime('1999 51', '%G %V') for w in ('A', 'a', 'w', 'u'): with self.assertRaises(ValueError): _strptime._strptime('1999 51', '%G %{}'.format(w)) with self.assertRaises(ValueError): _strptime._strptime('2015', '%G') with self.assertRaises(ValueError): _strptime._strptime('1999 256', '%G %j')
def test_fraction(self): import datetime now = datetime.datetime.now() tup, frac = _strptime._strptime(str(now), format="%Y-%m-%d %H:%M:%S.%f") self.assertEqual(frac, now.microsecond)
def update_event(self, inp=-1): self.set_output_val(0, _strptime._strptime(self.input(0), self.input(1)))
# coding=UTF-8 import time # make sure we have a strptime function! # 确认有函数 strptime from _strptime import _strptime try: _strptime = _strptime except AttributeError: from _strptime import _strptime print _strptime("31 Nov 00", "%d %b %y") print _strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p")
#!usr/bin/env python #-*- coding:utf-8 -*- import time from _strptime import _strptime print time.asctime() print time.localtime() print time.mktime(_strptime())
def parseDate(x: pystr, cFormat: pystr) -> pydate: # rework to be more efficient in bulk by parsing format separately from x or handle x as an array / range dt, micro, _ = _strptime(x, cFormat) return datetime.date(dt[0], dt[1], dt[2])