python3 datetime.timestamp in python2?

前端 未结 2 1112
余生分开走
余生分开走 2021-01-17 11:30

I have a piece of python3 code, that calls a function at 22:00.

# Imports
from datetime import datetime, date, time, timedelta
import sched         


        
相关标签:
2条回答
  • 2021-01-17 11:58

    use the following to convert to a timestamp in python 2

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

    0 讨论(0)
  • 2021-01-17 12:03

    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:

    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()
    

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

    0 讨论(0)
提交回复
热议问题