-1

I am converting some date time, but I got a problem. the date time is

my_time = "2012-03-18 14:50:36.90"

my code to convert this date time is

import time
import datetime
import calendar

time_con = datetime.datetime.strptime(my_time,'%Y-%m-%d %H:%M:%S.%f')
final_time = calendar.timegm(time_con.timetuple())

when print, i got

>> print "time_con", time_con

datetime.datetime(2012, 3, 18, 14, 50, 36, 900000)

when I print final_time , I got

>> print "final_time", final_time
1332082236

I am unable to get milliseconds, and I don,t know how to deal with them properly because I need those milliseconds in my date time. I search SO but no clue. but in Can help would be great and it will save my day. thanks

roy
  • 305
  • 1
  • 2
  • 9
  • `dat-time` is an invalid variable name in Python. – linusg Jan 25 '17 at 16:13
  • @linusg I change variable name. thanx a lot , but problem still exists. – roy Jan 25 '17 at 16:15
  • `my-time`, too the dash is not allowed. – linusg Jan 25 '17 at 16:15
  • @linusg I hope now its fine. thanx for the pointing my mistakes. – roy Jan 25 '17 at 16:17
  • 1
    Look, this will never ever run and therefore not work. `final-time` has ... a dash in it (see http://stackoverflow.com/questions/2064329/why-python-does-not-allow-hyphens if still don't believe) – linusg Jan 25 '17 at 16:21
  • @linusg thanx . I got the point. Actually I changed the names of variables, therefore I make mistakes. but I learn now and i make corrections accordingly. – roy Jan 25 '17 at 16:26
  • Fine! I'm halv on a solution, wait a sec. – linusg Jan 25 '17 at 16:28
  • 1
    See my answer below, which is different from ForceBru and shows it is indeed possible. – linusg Jan 25 '17 at 16:39

3 Answers3

2

This is easy to solve. Since you have not the milliseconds but microseconds stored in time_con, we can do time_con.microsecond*1000 to get milliseconds.

import time
import datetime
import calendar

my_time = "2012-03-18 14:50:36.90"
time_con = datetime.datetime.strptime(my_time,'%Y-%m-%d %H:%M:%S.%f')
microsecs =  time_con.microsecond
final_time = calendar.timegm(time_con.timetuple())*1000000 + millisecs
# final_time is in microseconds

# To print in seconds
# print(final_time/1000000)

print(final_time)

Results in

1332082236900

Which is hopefully what you want. Hope this helps!

linusg
  • 6,289
  • 4
  • 28
  • 78
1

In Python 3 you can do

import datetime

my_time = "2012-03-18 14:50:36.90"
time_con = datetime.datetime.strptime(my_time,'%Y-%m-%d %H:%M:%S.%f')
final_time = time_con.replace(tzinfo=datetime.timezone.utc).timestamp()
# 1332082236.9

Here is the link to datetime.datetime.timestamp's documentation.

daragua
  • 1,133
  • 6
  • 8
0

Please read the docs for calendar.timegm (emphasis mine):

calendar.timegm(tuple)

An unrelated but handy function that takes a time tuple such as returned by the gmtime() function in the time module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding.

The UNIX timestamp is defined to be a non-negative integer. Well, Wikipedia says that it can be floating-point, but anyways, UNIX systems normally represent it with a variable of C type time_t, which is whole non-negative. An integer, because it's meant to represent the number of seconds passed since the beginning of the UNIX epoch.

So, it clearly cannot represent milliseconds because it's defined to be a whole number.

Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98