Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a formatted time string to milliseconds

I am trying to convert '2015-09-15T17:13:29.380Z' to milliseconds.

At first I used:

time.mktime(
  datetime.datetime.strptime(
    "2015-09-15T17:13:29.380Z",
    "%Y-%m-%dT%H:%M:%S.%fZ"
  ).timetuple()
)

I got 1442330009.0 - with no microseconds. I think time.mktime rounds the number to the nearest second.

In the end I did:

origTime = '2015-09-15T17:13:29.380Z'
tupleTime = datetime.datetime.strptime(origTime, "%Y-%m-%dT%H:%M:%S.%fZ")
microsecond = tupleTime.microsecond
updated = float(time.mktime(tupleTime.timetuple())) + (microsecond * 0.000001)

Is there a better way to do this and how do I work with the timezone?

like image 465
Karl Stulik Avatar asked Sep 16 '15 10:09

Karl Stulik


People also ask

How to convert a time in seconds to a string format?

Given a time in seconds and the task is to convert the time into a string format hh:mm:ss. There are two approaches to solve this problem: The Date () constructor expects a UNIX timestamp as one of its forms. A UNIX timestamp is a number of milliseconds that have passed since the epoch time (January 1, 1970, 00:00:00 UTC).

How to parse milliseconds from datetime value?

DateTime.valueOf is broken. It doesn't support milliseconds, so you can't parse milliseconds. However, if you have a properly formatted ISO 8601 date/time value, you can deserialize it from JSON: DateTime d = (DateTime)JSON.deserialize ('"1234-01-23T12:34:56.789Z"', DateTime.class); System.debug (d.millisecond ()); // Outputs 789

How do you convert seconds to milliseconds in JavaScript?

The time in seconds is converted to a time in milliseconds by multiplying it by 1000. This is passed to the Date () constructor. The Date object created can then be used to access the hours, minutes and seconds as required for the format. The hours value is extracted from the date using the getUTCHours () method.

How to print the date and time in milliseconds in Python?

We can specify the character which separates the date and time to be ' ' using the sep parameter and the timespace parameter that determines the time component to be milliseconds. Python. python Copy. from datetime import datetime date_s = datetime.now().isoformat(sep=' ', timespec='milliseconds') print(date_s) Output:


2 Answers

Your input time is in UTC; it is incorrect to use time.mktime() here unless your local timezone is always UTC.

There are two steps:

  1. Convert the input rfc 3339 time string into a datetime object that represents time in UTC

    from datetime import datetime
    
    utc_time = datetime.strptime("2015-09-15T17:13:29.380Z",
                                 "%Y-%m-%dT%H:%M:%S.%fZ")
    

    You've already done it. See also Convert an RFC 3339 time to a standard Python timestamp

  2. Convert UTC time to POSIX time expressed in milliseconds:

    from datetime import datetime, timedelta
    
    milliseconds = (utc_time - datetime(1970, 1, 1)) // timedelta(milliseconds=1)
    # -> 1442337209380
    

    For a version that works on Python 2.6-3+, see How can I convert a datetime object to milliseconds since epoch (unix time) in Python?

like image 181
jfs Avatar answered Sep 28 '22 10:09

jfs


Unfortunately, there is no miliseconds in timetuple. However, you don't need timetuple. For timestamp, just call

datetime.strptime(...).timestamp()

As for timezone, check out tzinfo argument of datetime.

EDIT: tzinfo

>>> d
datetime.datetime(2015, 9, 15, 17, 13, 29, 380000)
>>> d.timestamp()
1442330009.38
>>> import pytz
>>> d.replace(tzinfo=pytz.timezone("US/Eastern")).timestamp()
1442355209.38
like image 39
pacholik Avatar answered Sep 28 '22 11:09

pacholik