Difference between Python datetime vs time modules
Asked Answered
O

4

207

I am trying to figure out the differences between the datetime and time modules, and what each should be used for.

I know that datetime provides both dates and time. What is the use of the time module?

Examples would be appreciated and differences concerning timezones would especially be of interest.

Oeillade answered 20/9, 2011 at 3:26 Comment(0)
B
151

The time module is principally for working with Unix time stamps; expressed as a floating point number taken to be seconds since the Unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.

Bambi answered 20/9, 2011 at 4:0 Comment(2)
Further, what are the differences between time and datetime.time?Grizzly
Would be nice to detail properties like if it's realtime/monotonic. Like in the POSIX standard you have: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_THREAD, etc... On POSIX clocks see: #3523942Layard
P
40

Stick to time to prevent DST ambiguity.

Use exclusively the system time module instead of the datetime module to prevent ambiguity issues with daylight savings time (DST).

Conversion to any time format, including local time, is pretty easy:

import time
t = time.time()

time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))
'2019-05-27 12:03 CEST'

time.strftime('%Y-%m-%d %H:%M %Z', time.gmtime(t))
'2019-05-27 10:03 GMT'

time.time() is a floating point number representing the time in seconds since the system epoch. time.time() is ideal for unambiguous time stamping.

If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.

Here is the documentation of the time module.

Pastypat answered 18/7, 2017 at 9:57 Comment(5)
In your example, you use time.localtime(), which of course does have DST baked in. If we're going to be purists, shouldn't we use time.gmtime() instead? :)Crispi
@Seamus Simply testing both commands in ipython shows that time.gmtime() yields a tuple, whereas time.time() gives the UNIX epoch time as a single decimal value of seconds elapsed since 00:00:00 UTC, Thursday, 1 January 1970. The function time.localtime(t) converts the epoch time to a local time tuple. So, the answer to your question is «no».Pastypat
Is there a way to add days to a time object ? with datetime there's the timedelta(days=6) method.Cung
@NehemiasHerrera t = time.time() is a floating point number representing the time in seconds since the system epoch. Hence, one can simply add or substract 86400 seconds for every day; t += 86400Pastypat
To all those that will say "Just use datetime like the others": this is the only way to read time in really old python versions (i'm talking about even 2.1 and 2.2!) or when generally datetime is not available. Thank you @SergeStroobandtIdeomotor
C
5

The time module can be used when you just need the time of a particular record - like lets say you have a seperate table/file for the transactions for each day, then you would just need the time. However the time datatype is usually used to store the time difference between 2 points of time.

This can also be done using datetime, but if we are only dealing with time for a particular day, then time module can be used.

Datetime is used to store a particular data and time for a record. Like in a rental agency. The due date would be a datetime datatype.

Cardwell answered 20/9, 2011 at 3:56 Comment(0)
S
4

Just noticed that time is more precise than datetime with an extra digit.

import time as tm
from datetime import datetime as dt
restime = tm.time()
resdtime = dt.timestamp(dt.now())
print("TIME:".rjust(10," "),restime)
print("DATETIME:".rjust(10," "),resdtime)

Output

     TIME: 1637357103.7650678
 DATETIME: 1637357103.765067
Spew answered 19/11, 2021 at 21:31 Comment(2)
Before you shoot me down... I know it doesn't run at the exact time... but try iterating it afew times... it actually has another digit.Spew
What you're seeing there is the output of datetime's __str__ implementation, which doesn't necessarily demonstrate the precision of the representations. It is correct that datetimes are limited to microsecond precision, but if you need to be more precise than that, you probably want to use time.time_ns or similar. time.time() is converted to a float, which loses precision -- even though you may see extra digits, you may not be gaining actual precision.Jeroldjeroma

© 2022 - 2024 — McMap. All rights reserved.