0

I have 2 datetime objects with 2 different time zones:

datetime1 = 18:26:23, with tzinfo = UTC

datetime2 = 14:30:00, with tzinfo = US/Eastern

Both dates are on the same day.

There should be exactly 1 hour, 3 minutes and 37 seconds difference between the 2 datetimes, which is: 3817 seconds total difference.

However, when I use the following code to compare:

time_diff = (datetime2 - datetime1).total_seconds()

time_diff gives me a value of: 3576.

Am I doing the difference in seconds wrong? Or am I not utilizing pytz for time zones correctly?

Many thanks.

2
  • Can you put this in the form of a short but complete program?
    – Jon Skeet
    May 21, 2015 at 17:41
  • What are the dates on these datetimes, and how have you imbued the timezone? Starting with US/Eastern without a date gives you the original version of that timezone (which was 19:04:00 GMT, not an even hour), not the current version. If you then pack a date into the already-aware-of-the-wrong-offset time, you keep the wrong offset.
    – abarnert
    May 21, 2015 at 17:49

2 Answers 2

1

There are two likely scenarios here.

  1. Either you are creating the timezone on your datetime objects incorrectly
  2. The timezone is correct but your datetime objects are not actually representing the time you say they are.

For example, regardless of timezone, I don't see how the diff between 18:26:23 and 14:30:00 could possibly give you an even number of seconds, which makes scenario #2 more likely.

Can you print the value of the datetime objects right before you run the line:

time_diff = (datetime2 - datetime1).total_seconds()

Here is some sample code for reference that gives you the expected seconds:

from pytz import timezone
from datetime import datetime

eastern = timezone('US/Eastern')
utc = timezone('UTC')
datetime1 = utc.localize(datetime(2002, 10, 27, 18, 26, 23))
datetime2 = eastern.localize(datetime(2002, 10, 27, 14, 30, 00))
time_diff = (datetime2 - datetime1).total_seconds()
print(time_diff)  # prints 3817
5
  • This doesn't answer his problem, because his problem is almost certainly that he created the datetime objects wrong, and just showing that completely different code works isn't much help.
    – abarnert
    May 21, 2015 at 17:50
  • And I can just as easily provide an example that doesn't work. Try the same thing with a DST time line 2002-07-27, or a time before EST changed like 1890-10-27.
    – abarnert
    May 21, 2015 at 17:53
  • good points, I suspect the timezones aren't the issue and his datetime objects don't represent the time he thinks it does. May 21, 2015 at 20:18
  • Try it with 1890 and the results exactly duplicate what he's seeing. That's why I think the timezones are the problem—or, rather, the way he's trying to create aware datetime objects from those timezones is the problem. But without seeing his code, there's no way to tell him what he's doing wrong, except a vague guess.
    – abarnert
    May 22, 2015 at 20:10
  • You guys are absolutely correct. I created the datetime wrong. My original code created the datetime by passing in a timezone object into the creator, instead of using the localize function.
    – TheBear
    May 26, 2015 at 9:42
-1

doc for timedelta: https://docs.python.org/2/library/datetime.html

def make_timedelta(seconds):
        return timedelta(days=seconds // 86399, seconds=seconds % 86399)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.