Date Time, Timestamp and Datetime conversions
Python has a great class for storing and
manipulating the date and time: datetime.
Unfortunately, it is not the only way to refer to
a point in time. Sometimes you need to convert
between the two and I frequently forget how to do
it.
Table of Conversions
Here is a table of conversions. Pick the row
corresponding to the form of time that you have
(from the left), and them pick the column
corresponding to the form that you would like
(from the top). The intersecting cell is the code
snippet to get you there.
I have some measure of confidence in the correctness since
the table, indeed, this entire post, was created by a script
which tested the code shown in the table and lists below.
That script is available
here.
However, I expect that many of the conversions can be improved upon
and many may be subtly, or not so subtly, incorrect. Please let me know
if you have an improvement.
- "dt" represents an input datetime.
- "t" represents an input time struct.
- "secs" represents an input timestamp (seconds since the Epoch GMT).
- "localtz" represents the local timezone. This is necessary to account
for functions or formats that convert to or from the local timezone.
- "othertz" represents a timezone that you want to to have. It can be
UTC or any other pytz timezone.
- You need to import "pytz" for timezones.
- You need to import the "datetime" class from the the datetime module.
- Note that time structs can not store time at a resolution finer
than a second.
I'm sorry, but this table just isn't going to work on mobile.
Below is
a breakout by the type of representation that you are converting to.
Table of Conversions between various date and time formats
|
To
Timezone Naive Datetime Local
|
To
Timezone Naive Datetime UTC
|
To
Timezone Aware Datetime
|
To
Time Struct Local
|
To
Time Struct UTC
|
To
Timestamp
|
Build as of now
|
datetime . now ( )
|
datetime . utcnow ( )
|
localtz . localize ( datetime . now ( ) ) . astimezone ( othertz )
|
time . localtime ( )
|
time . gmtime ( )
|
time . time ( )
|
From
Timezone Naive Datetime Local
|
|
localtz . localize ( dt ) . astimezone ( pytz . utc ) . replace ( tzinfo = None )
|
localtz . localize ( dt ) . astimezone ( othertz )
|
dt . timetuple ( )
|
localtz . localize ( dt ) . utctimetuple ( )
|
time . mktime ( dt . timetuple ( ) ) + 1e-6 * dt . microsecond
|
From
Timezone Naive Datetime UTC
|
pytz . utc . localize ( dt ) . astimezone ( localtz ) . replace ( tzinfo = None )
|
|
pytz . utc . localize ( dt ) . astimezone ( othertz )
|
pytz . utc . localize ( dt ) . astimezone ( localtz ) . timetuple ( )
|
dt . utctimetuple ( )
|
calendar . timegm ( pytz . utc . localize ( dt ) . timetuple ( ) ) + 1e-6 * dt . microsecond
|
From
Timezone Aware Datetime
|
dt . astimezone ( localtz ) . replace ( tzinfo = None )
|
dt . astimezone ( pytz . utc ) . replace ( tzinfo = None )
|
|
dt . astimezone ( localtz ) . timetuple ( )
|
dt . utctimetuple ( )
|
calendar . timegm ( dt . utctimetuple ( ) ) + 1e-6 * dt . microsecond
|
From
Time Struct Local
|
datetime ( *t[:6] )
|
datetime . utcfromtimestamp ( time . mktime ( t ) )
|
localtz . localize ( datetime ( *t[:6] ) ) . astimezone ( othertz )
|
|
time . gmtime ( time . mktime ( t ) )
|
time . mktime ( t )
|
From
Time Struct UTC
|
pytz . utc . localize ( datetime ( *t[:6] ) ) . astimezone ( localtz ) . replace ( tzinfo = None )
|
datetime . utcfromtimestamp ( calendar . timegm ( t ) )
|
pytz . utc . localize ( datetime ( *t[:6] ) ) . astimezone ( othertz )
|
time . localtime ( calendar . timegm ( t ) )
|
|
calendar . timegm ( t )
|
From
Timestamp
|
datetime . fromtimestamp ( secs )
|
datetime . utcfromtimestamp ( secs )
|
localtz . localize ( datetime . fromtimestamp ( secs ) )
|
time . localtime ( secs )
|
time . gmtime ( secs )
|
|
List of Specific Conversions
Timezone Naive Datetime Local
From
|
To Timezone Naive Datetime Local
|
Timezone Naive Datetime UTC
|
pytz.utc.localize(dt).astimezone(localtz).replace(tzinfo=None)
|
Timezone Aware Datetime
|
dt.astimezone(localtz).replace(tzinfo=None)
|
Time Struct Local
|
datetime(*t[:6])
|
Time Struct UTC
|
pytz.utc.localize(datetime(*t[:6])).astimezone(localtz).replace(tzinfo=None)
|
Timestamp
|
datetime.fromtimestamp(secs)
|
To build as of now:
datetime.now()
Timezone Naive Datetime UTC
From
|
To Timezone Naive Datetime UTC
|
Timezone Naive Datetime Local
|
localtz.localize(dt).astimezone(pytz.utc).replace(tzinfo=None)
|
Timezone Aware Datetime
|
dt.astimezone(pytz.utc).replace(tzinfo=None)
|
Time Struct Local
|
datetime.utcfromtimestamp(time.mktime(t))
|
Time Struct UTC
|
datetime.utcfromtimestamp(calendar.timegm(t))
|
Timestamp
|
datetime.utcfromtimestamp(secs)
|
To build as of now:
datetime.utcnow()
Timezone Aware Datetime
From
|
To Timezone Aware Datetime
|
Timezone Naive Datetime Local
|
localtz.localize(dt).astimezone(othertz)
|
Timezone Naive Datetime UTC
|
pytz.utc.localize(dt).astimezone(othertz)
|
Time Struct Local
|
localtz.localize(datetime(*t[:6])).astimezone(othertz)
|
Time Struct UTC
|
pytz.utc.localize(datetime(*t[:6])).astimezone(othertz)
|
Timestamp
|
localtz.localize(datetime.fromtimestamp(secs))
|
To build as of now:
localtz.localize(datetime.now()).astimezone(othertz)
Time Struct Local
From
|
To Time Struct Local
|
Timezone Naive Datetime Local
|
dt.timetuple()
|
Timezone Naive Datetime UTC
|
pytz.utc.localize(dt).astimezone(localtz).timetuple()
|
Timezone Aware Datetime
|
dt.astimezone(localtz).timetuple()
|
Time Struct UTC
|
time.localtime(calendar.timegm(t))
|
Timestamp
|
time.localtime(secs)
|
To build as of now:
time.localtime()
Time Struct UTC
From
|
To Time Struct UTC
|
Timezone Naive Datetime Local
|
localtz.localize(dt).utctimetuple()
|
Timezone Naive Datetime UTC
|
dt.utctimetuple()
|
Timezone Aware Datetime
|
dt.utctimetuple()
|
Time Struct Local
|
time.gmtime(time.mktime(t))
|
Timestamp
|
time.gmtime(secs)
|
To build as of now:
time.gmtime()
Timestamp
From
|
To Timestamp
|
Timezone Naive Datetime Local
|
time.mktime(dt.timetuple())+1e-6*dt.microsecond
|
Timezone Naive Datetime UTC
|
calendar.timegm(pytz.utc.localize(dt).timetuple())+1e-6*dt.microsecond
|
Timezone Aware Datetime
|
calendar.timegm(dt.utctimetuple())+1e-6*dt.microsecond
|
Time Struct Local
|
time.mktime(t)
|
Time Struct UTC
|
calendar.timegm(t)
|
To build as of now:
time.time()
Timezones
import pytz, datetime
pytz.timezone('UTC').localize(datetime.utcnow())
pytz.timezone('US/Central').localize(datetime.now())
print 'Common timezones:',pytz.common_timezones
References