How do I get the current time?
Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
How do I get the current time in Python:
Use
datetime
:How do I get the current time in Python:
The
time
moduleThe
time
module provides functions that tell us the time in “seconds since the epoch” as well as other utilities.Unix Epoch Time
This is the format you should get timestamps in for saving in databases. It is a simple floating-point number that can be converted to an integer. It is also good for arithmetic in seconds, as it represents the number of seconds since Jan 1, 1970, 00:00:00, and it is memory light relative to the other representations of time we’ll be looking at next:
This timestamp does not account for leap-seconds, so it’s not linear – leap seconds are ignored. So while it is not equivalent to the international UTC standard, it is close, and therefore quite good for most cases of record-keeping. This is not ideal for human scheduling, however. If you have a future event you wish to take place at a certain point in time, you’ll want to store that time with a string that can be parsed into a
datetime
object or a serializeddatetime
object (these will be described later).time.ctime
You can also represent the current time in the way preferred by your operating system (which means it can change when you change your system preferences, so don’t rely on this to be standard across all systems, as I’ve seen others expect). This is typically user friendly, but doesn’t typically result in strings one can sort chronologically:
You can hydrate timestamps into human readable form with
ctime
as well:This conversion is also not good for record-keeping (except in text that will only be parsed by humans – and with improved Optical Character Recognition and Artificial Intelligence, I think the number of these cases will diminish).
datetime
moduleThe
datetime
module is also quite useful here:datetime.datetime.now
The
datetime.now
is a class method that returns the current time. It uses thetime.localtime
without the timezone info (if not given, otherwise see timezone aware below). It has a representation (which would allow you to recreate an equivalent object) echoed on the shell, but when printed (or coerced to astr
), it is in human readable (and nearly ISO) format, and the lexicographic sort is equivalent to the chronological sort:datetime’s
utcnow
You can get a datetime object in UTC time, a global standard, by doing this:
UTC is a time standard that is nearly equivalent to the GMT timezone. (While GMT and UTC do not change for Daylight Savings Time, their users may switch to other timezones, like British Summer Time, during the Summer.)
datetime timezone aware
However, none of the datetime objects we’ve created so far can be easily converted to various timezones. We can solve that problem with the
pytz
module:Equivalently, in Python 3 we have the
timezone
class with a utctimezone
instance attached, which also makes the object timezone aware (but to convert to another timezone without the handypytz
module is left as an exercise to the reader):And we see we can easily convert to timezones from the original UTC object.
You can also make a naive datetime object aware with the
pytz
timezonelocalize
method, or by replacing the tzinfo attribute (withreplace
, this is done blindly), but these are more last resorts than best practices:The
pytz
module allows us to make ourdatetime
objects timezone aware and convert the times to the hundreds of timezones available in thepytz
module. One could ostensibly serialize this object for UTC time and store that in a database, but it would require far more memory and be more prone to error than simply storing the Unix Epoch time, which I demonstrated first. The other ways of viewing times are much more error-prone, especially when dealing with data that may come from different time zones. You want there to be no confusion as to which timezone a string or serialized datetime object was intended for. If you’re displaying the time with Python for the user,ctime
works nicely, not in a table (it doesn’t typically sort well), but perhaps in a clock. However, I personally recommend, when dealing with time in Python, either using Unix time, or a timezone aware UTCdatetime
object.