0

The following comparison does not work, is there any way to get number of seconds passed (w.r.t localtime) since epoch from time module?

(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.mktime(time.localtime()) => 3600.9646549224854

OR

(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.time() => 3599.9999861717224

Thanks

Anandan
  • 319
  • 3
  • 10
  • 1
    Can't you use cronjobs or scheduled tasks? – moooeeeep Sep 17 '15 at 09:01
  • 2
    I don’t get it. What are you trying to do? Why are you making that calculation? – poke Sep 17 '15 at 09:01
  • Yeah, on second though the whole logic is convoluted. Still my datetime and time comparison doubt remains, I will edit the question to reflect just that. – Anandan Sep 17 '15 at 09:17
  • I'm not sure why you say they don't work. Those results are expected, but is admittedly a very roundabout way to get yur current UTC offset... – Lennart Regebro Sep 17 '15 at 09:40
  • related: [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/a/26313848/4279) – jfs Sep 17 '15 at 13:33

2 Answers2

5

datetime.now() returns a local datetime, while datetime.utcfromtimestamp() returns a UTC datetime. So of course you will have the difference of your timezone accounted for in your calculation. In order to avoid that, either always use local time, or always use universal time.

>>> (datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.time()
0.0
poke
  • 369,085
  • 72
  • 557
  • 602
3

Epoch is in GMT, so the number of seconds that have passed since Epoch is the same no matter what timezone you are in. Hence, the correct answer to your question:

is there any way to get number of seconds passed (w.r.t localtime) since epoch from time module?

is

>>> import time
>>> time.time()
1442482454.94842
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 1
    note: It is irrelevant that Epoch is usually expressed in UTC. I can use `1970-01-01 01:00:00 CET+0100` as Epoch and nothing changes. "seconds since epoch" does not depend on any timezone (with the exception of non-POSIX "right" timezones and the like). – jfs Sep 17 '15 at 13:31
  • No, you can't use CET as epoch. epoch is UTC. "seconds since epoch does not depend on any timezone". That's exactly my point. You'd probably get more useful answers if you explained what the problem is. – Lennart Regebro Sep 29 '15 at 10:40
  • Epoch is a moment in time, you can label it using whatever time zone you like: `datetime.fromtimestamp(0, pytz.timezone("Europe/Paris")` is a valid expression. Epoch is as much in GMT as it is in CET. We both agree that the actual number is the same in all timezones for the same moment in time. – jfs Sep 29 '15 at 10:51
  • Right, I see what you mean now with "expressed". Yes, of course is can be expressed in whatever timezone. So my answer stands. You get the number of seconds that has passed since epoch with `time.time()`. – Lennart Regebro Oct 01 '15 at 11:05
  • 1
    my comment is about "Epoch is in GMT", nothing else in your answer. Otherwise your answer is ok that is why I've upvoted it. – jfs Oct 01 '15 at 13:13