diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2012-06-08 16:33:09 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2012-06-08 16:33:09 (GMT) |
commit | a4415141da431c74c405efc0ea2791183285f8ee (patch) | |
tree | b0aed8191d2b8f94e08417e7ab53b4fc22abbfdd /Lib/datetime.py | |
parent | ed36b2e55be884afb7517905e02da313973998d1 (diff) | |
download | cpython-a4415141da431c74c405efc0ea2791183285f8ee.zip cpython-a4415141da431c74c405efc0ea2791183285f8ee.tar.gz cpython-a4415141da431c74c405efc0ea2791183285f8ee.tar.bz2 |
Issue #2736: Added datetime.timestamp() method.
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r-- | Lib/datetime.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index 59f3c68..5d8d9b3 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1434,6 +1434,15 @@ class datetime(date): self.hour, self.minute, self.second, dst) + def timestamp(self): + "Return POSIX timestamp as float" + if self._tzinfo is None: + return _time.mktime((self.year, self.month, self.day, + self.hour, self.minute, self.second, + -1, -1, -1)) + self.microsecond / 1e6 + else: + return (self - _EPOCH).total_seconds() + def utctimetuple(self): "Return UTC time tuple compatible with time.gmtime()." offset = self.utcoffset() @@ -1889,7 +1898,7 @@ class timezone(tzinfo): timezone.utc = timezone._create(timedelta(0)) timezone.min = timezone._create(timezone._minoffset) timezone.max = timezone._create(timezone._maxoffset) - +_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc) """ Some time zone algebra. For a datetime x, let x.n = x stripped of its timezone -- its naive time. |