201

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.

alex
  • 6,818
  • 9
  • 52
  • 103
Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101

4 Answers4

146

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.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 41
    Further, what are the differences between `time` and `datetime.time`? – SparkAndShine Nov 30 '15 at 23:13
  • 1
    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: http://stackoverflow.com/questions/3523442/difference-between-clock-realtime-and-clock-monotonic – Vajk Hermecz Jan 28 '16 at 18:30
37

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.

Community
  • 1
  • 1
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
  • 2
    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? :) –  May 06 '18 at 13:15
  • 2
    @Seamus Simply testing both commands in `ipython` shows that `time.gmtime()` yields a tuple, whereas `time.time()` gives the [UNIX epoch time](https://en.wikipedia.org/wiki/Unix_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». – Serge Stroobandt May 06 '18 at 17:07
  • Is there a way to add days to a time object ? with datetime there's the timedelta(days=6) method. – Nehemias Herrera May 27 '19 at 00:53
  • 1
    @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 += 86400` – Serge Stroobandt May 27 '19 at 06:40
  • 1
    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 @SergeStroobandt – LukeSavefrogs Mar 16 '23 at 11:18
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.

Jan S
  • 1,831
  • 15
  • 21
2

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
  • 10
    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. – seldomspeechless Nov 19 '21 at 21:35