1

I want to change GMT Zone(date and time) to EPOCH Time stamp in milliseconds.

For example:

I want to take current system date and by default the time will be "16:00:00" and convert this date & time like below:

If Date & Time is "12/15/2015 16:00:00" GMT to be converted to "1450195200000"

Here the code i used to achieve but no solution:

import datetime
dt = time.strftime("%d/%m/%Y ")
ti = "16:00:00"
dt_ti = dt + ti
pattern = '%d/%m/%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(dt_ti, pattern)))
print (epoch)

output is 1450175400

But i want to achieve is this 1450195200000

Please help me on this.

Teja R
  • 131
  • 3
  • 12

1 Answers1

3

time.mktime assumes the time_struct you feed it is local time. calendar.timegm() does something similar but assumes you give it a UTC time_struct.

import datetime
import calendar
dt = time.strftime("15/12/2015 ")
ti = "16:00:00"
dt_ti = dt + ti
pattern = '%d/%m/%Y %H:%M:%S'

epoch = int(time.mktime(time.strptime(dt_ti, pattern)))
print (epoch)
# 1450224000

utc_epoch = int(calendar.timegm(time.strptime(dt_ti, pattern)))
print (utc_epoch)
# 1450195200
personjerry
  • 1,045
  • 8
  • 28
  • Hi @personjerry thanks , The code is working fine but one small requirement i have. I want to print value like `1450195200000` . Because i am going to perform greater than and lesser than operation in program. If the value is `1450195200`. I am facing problem is there any way that we can get value like `1450195200000` as it is. – Teja R Dec 15 '15 at 06:48
  • 1
    Well it seems like you could multiply by 1000? – personjerry Dec 15 '15 at 06:49