Skip to content

Commit b337955

Browse files
author
Ralf Schlatterbeck
committed
Add a class-method as a constructor of a Date object from a timestamp.
The timestamp is not limited by the year-range of common gmtime implementations.
1 parent 1697fcd commit b337955

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

roundup/date.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: date.py,v 1.91 2007-03-09 14:54:39 schlatterbeck Exp $
18+
# $Id: date.py,v 1.92 2007-03-14 15:07:24 schlatterbeck Exp $
1919

2020
"""Date, time and time interval handling.
2121
"""
@@ -548,6 +548,17 @@ def setTranslator(self, translator):
548548
self._ = translator.gettext
549549
self.ngettext = translator.ngettext
550550

551+
def fromtimestamp(cls, ts):
552+
"""Create a date object from a timestamp.
553+
554+
The timestamp may be outside the gmtime year-range of
555+
1902-2038.
556+
"""
557+
usec = int((ts - int(ts)) * 1000000.)
558+
delta = datetime.timedelta(seconds = int(ts), microseconds = usec)
559+
return cls(datetime.datetime(1970, 1, 1) + delta)
560+
fromtimestamp = classmethod(fromtimestamp)
561+
551562
class Interval:
552563
'''
553564
Date intervals are specified using the suffixes "y", "m", and "d". The

0 commit comments

Comments
 (0)