Skip to content Skip to sidebar Skip to footer

Python3 Datetime.timestamp In Python2?

I have a piece of python3 code, that calls a function at 22:00. # Imports from datetime import datetime, date, time, timedelta import sched import time as mod_time # Find the nex

Solution 1:

use the following to convert to a timestamp in python 2

int((mod_time.mktime(first_run.timetuple())+first_run.microsecond/1000000.0))

Solution 2:

use time.time() in python2, it's analog to datetime.timestamp() in python3

datetime.datetime.timestamp

IF you need for current datetime realization, see implementation of that in python3:

deftimestamp(self):
    "Return POSIX timestamp as float"if self._tzinfo isNone:
        return _time.mktime((self.year, self.month, self.day,
                             self.hour, self.minute, self.second,
                             -1, -1, -1)) + self.microsecond / 1e6else:
        return (self - _EPOCH).total_seconds()

where _EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)

Post a Comment for "Python3 Datetime.timestamp In Python2?"