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

5
  • 1
    Can't you use cronjobs or scheduled tasks? Commented Sep 17, 2015 at 9:01
  • 2
    I don’t get it. What are you trying to do? Why are you making that calculation? Commented Sep 17, 2015 at 9: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. Commented Sep 17, 2015 at 9: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... Commented Sep 17, 2015 at 9:40
  • related: Find if 24 hrs have passed between datetimes - Python Commented Sep 17, 2015 at 13:33

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.