1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- # $Id: date.py,v 1.89 2006-12-28 22:38:02 richard Exp $
18+ # $Id: date.py,v 1.90 2007-03-09 10:25:09 schlatterbeck Exp $
1919
2020"""Date, time and time interval handling.
2121"""
@@ -263,7 +263,6 @@ def __init__(self, spec='.', offset=0, add_granularity=0, translator=i18n):
263263 elif isinstance (spec , datetime .datetime ):
264264 # Python 2.3+ datetime object
265265 y ,m ,d ,H ,M ,S ,x ,x ,x = spec .timetuple ()
266- if y < 1970 : raise ValueError , 'year must be > 1970'
267266 S += spec .microsecond / 1000000.
268267 spec = (y ,m ,d ,H ,M ,S ,x ,x ,x )
269268 elif hasattr (spec , 'tuple' ):
@@ -272,7 +271,6 @@ def __init__(self, spec='.', offset=0, add_granularity=0, translator=i18n):
272271 spec = spec .get_tuple ()
273272 try :
274273 y ,m ,d ,H ,M ,S ,x ,x ,x = spec
275- if y < 1970 : raise ValueError , 'year must be > 1970'
276274 frac = S - int (S )
277275 self .year , self .month , self .day , self .hour , self .minute , \
278276 self .second = _local_to_utc (y , m , d , H , M , S , offset )
@@ -309,20 +307,16 @@ def set(self, spec, offset=0, date_re=date_re,
309307 _add_granularity (info , 'SMHdmyab' )
310308
311309 # get the current date as our default
312- ts = time .time ()
313- frac = ts - int (ts )
314- y ,m ,d ,H ,M ,S ,x ,x ,x = time .gmtime (ts )
315- # gmtime loses the fractional seconds
316- S = S + frac
317- if str (S ) == '60.0' : S = 59.9
310+ dt = datetime .datetime .utcnow ()
311+ y ,m ,d ,H ,M ,S ,x ,x ,x = dt .timetuple ()
312+ S += dt .microsecond / 1000000.
318313
319314 # whether we need to convert to UTC
320315 adjust = False
321316
322317 if info ['y' ] is not None or info ['a' ] is not None :
323318 if info ['y' ] is not None :
324319 y = int (info ['y' ])
325- if y < 1970 : raise ValueError , 'year must be > 1970'
326320 m ,d = (1 ,1 )
327321 if info ['m' ] is not None :
328322 m = int (info ['m' ])
@@ -344,20 +338,19 @@ def set(self, spec, offset=0, date_re=date_re,
344338 S = float (info ['S' ])
345339 adjust = True
346340
347- if add_granularity :
348- S = S - 1
349341
350342 # now handle the adjustment of hour
351343 frac = S - int (S )
352- ts = calendar .timegm ((y ,m ,d ,H ,M ,S ,0 ,0 ,0 ))
353- y , m , d , H , M , S , x , x , x = time .gmtime (ts )
344+ dt = datetime .datetime (y ,m ,d ,H ,M ,int (S ), int (frac * 1000000. ))
345+ if add_granularity :
346+ dt = dt - datetime .timedelta (seconds = 1 )
347+ y , m , d , H , M , S , x , x , x = dt .timetuple ()
354348 if adjust :
355349 y , m , d , H , M , S = _local_to_utc (y , m , d , H , M , S , offset )
356350 self .year , self .month , self .day , self .hour , self .minute , \
357351 self .second = y , m , d , H , M , S
358352 # we lost the fractional part along the way
359- self .second = self .second + frac
360- if str (self .second ) == '60.0' : self .second = 59.9
353+ self .second += dt .microsecond / 1000000.
361354
362355 if info .get ('o' , None ):
363356 try :
@@ -493,7 +486,7 @@ def __str__(self):
493486 return self .formal ()
494487
495488 def formal (self , sep = '.' , sec = '%02d' ):
496- f = '%%4d -%%02d-%%02d%s%%02d:%%02d:%s' % (sep , sec )
489+ f = '%%04d -%%02d-%%02d%s%%02d:%%02d:%s' % (sep , sec )
497490 return f % (self .year , self .month , self .day , self .hour , self .minute ,
498491 self .second )
499492
@@ -503,13 +496,10 @@ def pretty(self, format='%d %B %Y'):
503496 Note that if the day is zero, and the day appears first in the
504497 format, then the day number will be removed from output.
505498 '''
506- # Python2.4 strftime() enforces the non-zero-ness of the day-of-year
507- # component of the time tuple, so we need to figure it out
508- t = (self .year , self .month , self .day , self .hour , self .minute ,
509- int (self .second ), 0 , 0 , 0 )
510- t = calendar .timegm (t )
511- t = time .gmtime (t )
512- str = time .strftime (format , t )
499+ dt = datetime .datetime (self .year , self .month , self .day , self .hour ,
500+ self .minute , int (self .second ),
501+ (self .second - int (self .second )) * 1000000. )
502+ str = dt .strftime (format )
513503
514504 # handle zero day by removing it
515505 if format .startswith ('%d' ) and str [0 ] == '0' :
@@ -535,7 +525,7 @@ def get_tuple(self):
535525 self .second , 0 , 0 , 0 )
536526
537527 def serialise (self ):
538- return '%4d %02d%02d%02d%02d%06.3f' % (self .year , self .month ,
528+ return '%04d %02d%02d%02d%02d%06.3f' % (self .year , self .month ,
539529 self .day , self .hour , self .minute , self .second )
540530
541531 def timestamp (self ):
0 commit comments