What is Timestamp

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.

It should also be pointed out that this point in time technically does not change no matter where you are located on the globe.


Convert String date to Timestamp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from __future__ import division
import datetime

td = datetime.datetime(2014,1,1,06,0,02)

def totimestamp(dt, epoch=datetime.datetime(1970,1,1)):
td = dt - epoch
# return td.total_seconds()
return (td.microseconds + (td.seconds + td.days * 86400) * 10**6) / 10**6

print totimestamp(td)

#######################################################################
# result
# 1388556002.0

WRONG VERSION
time module is for time access and conversions.
datetime module is for basic date and time types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time
import datetime

# default time duration, formatting: "DD/MM/YYYY"
mDateStart = '01/01/2014 06:00:02' # 1388505600
mDateEnd = '02/01/2014' # 1388592000

# date 'DD/MM/YYYY HH:MM:SS'
def date2Unix(date):
unixTime = time.mktime(datetime.datetime.strptime(date, "%d/%m/%Y %H:%M:%S").timetuple()) # DO NOT USE IT WITH UTC DATE

return unixTime

print date2Unix(mDateStart)

#########################################################################
# result
# 1388552402.0

datetime.strptime() return a datetime parsed accroding to format.
.utctimetuple() complete time obj with full 9-tuple, default adding numbers as 0, equivalent to time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))
time.mktime() takes argument as full 9-tuple time obj, and returns a floating point number.

mktime() may return a wrong result if d corresponds to an ambiguous local time (e.g., during DST transition) or if d is a past(future) date when the utc offset might have been different and the C mktime() has no access to the tz database on the given platform.


Python Timezone

pytz module can be used for time localization, date arithmetic and etc.
pytz.country_timezones('country code') could return a list of time zone names. We can new a tzinfo with pytz.timezone('time zone name').

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> timeZone = pytz.country_timezones('cn')
>>> pytz.timezone(timeZone[0])
<DstTzInfo 'Asia/Shanghai' LMT+8:06:00 STD>

>>> pytz.timezone(timeZone[1])
<DstTzInfo 'Asia/Harbin' LMT+8:27:00 STD>

>>> pytz.timezone(timeZone[2])
<DstTzInfo 'Asia/Chongqing' LMT+7:06:00 STD>

>>> pytz.timezone(timeZone[3])
<DstTzInfo 'Asia/Urumqi' LMT+5:50:00 STD>

>>> pytz.timezone(timeZone[4])
<DstTzInfo 'Asia/Kashgar' LMT+5:04:00 STD>

>>> tz = pytz.timezone(pytz.country_timezones('tw')[0])
>>> tz
<DstTzInfo 'Asia/Taipei' CST+8:00:00 STD>

The wired thing is, there’s always a time offset between any Chinese local times and Beijing local time. But when it comes to Taipei local time, it’s the same with Beijing local time. Then we can use Taipei time as a alternative.


Local time 2 UTC time

UTCtime = Localtime - timezone
E.g. Beijing local time 18:00, UTCtime = 18:00 - (+ 08:00) = 10:00 + 00:00, so UTC time is 10:00.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# argument1 should be String 'DD/MM/YYYY', zoneName e.g. 'Asia/Shanghai', 'Asia/Taipei'

def locT2utcT(time, zoneName):

local = pytz.timezone (zoneName)

naiveTime = datetime.datetime.strptime(time, "%d/%m/%Y %H:%M:%S")

local_dt = local.localize(naiveTime, is_dst=None)

utc_dt = local_dt.astimezone(pytz.utc)

utc_dt = utc_dt.strftime ("%d/%m/%Y %H:%M:%S")

return utc_dt

Reference

[1] Epoch Unix Time Stamp Converter
[2] StackOverFlow: Convert string date to timestamp in Python
[3] 用datetime和pytz来转换时区
[4] StackOverFlow: Converting datetime.date to UTC timestamp in Python