Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times Today and Tomorrow

Drew Butcher
Drew Butcher
33,160 Points

Off by 4 Hours?

If I assign

epoc_date = datetime.datetime(year= 1970, month=1, day=1, hour=0, minute=0, second=0, microsecond=0)

and

now = datetime.datetime.now()

and calculate the difference using

diff = now - epoc_date

then I should get a timedelta that represents the timestamp, correct? However when I compute:

now.timestamp() - (diff.days*86400 + diff.seconds)

I end up with 14400 seconds (which is exactly 4 hours) and some microseconds.

Why didn't I get back 0 seconds?

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm, doing more or less the same thing, I get 7 hours. datetime.datetime.now() shouldn't have a timezone unless you specified one, which you didn't. Really, epoc_time should have a timezone of GMT, but I don't think that'll affect anything. Weird. I'll ask around a bit.

Also, easier to use diff.total_seconds() instead of (diff.days*86400 + diff.seconds) to get the total number of elapsed seconds.

Drew Butcher
Drew Butcher
33,160 Points

I guess it has something to do with time zones, which is what Kenneth Love mentioned at the end of the video; however, it seems like since i used now() I get my time zone and timestamp() should also be related to my time zone too.?.?