1
$\begingroup$

UnixTime@DateObject@"2017-05-05T01:02:30.5Z" gives the seconds, rounded to an integer, so I can't simply multiply it by 1000 to get the milliseconds. How can I get milliseconds without doing date arithmetic?

$\endgroup$
  • $\begingroup$ "...gives the seconds, as an integer, so I can't multiply it by 1000." - I'm having trouble understanding this. Why wouldn't you be able to multiply an integer by 1000? $\endgroup$ – J. M. will be back soon Nov 3 '17 at 18:20
  • 1
    $\begingroup$ Why not use AbsoluteTime instead of UnixTime? $\endgroup$ – Carl Woll Nov 3 '17 at 18:23
  • $\begingroup$ Sorry, "...gives the seconds, as an integer, so I can't multiply it by 1000." was unclear. I've updated it. I meant that, since the fractional section is gone, I can't multiply it by 1000 to get the millisecond value. $\endgroup$ – Ashley Nov 3 '17 at 18:27
  • $\begingroup$ I'm using UnixTime instead of AbsoluteTime because I want to compute in GMT. $\endgroup$ – Ashley Nov 3 '17 at 18:28
  • $\begingroup$ Try Block[{$TimeZone = 0}, UnitConvert[DateDifference[DateObject["1970-01-01T00:00:00Z"], DateObject["2017-05-05T01:02:30.5Z"], "Seconds"], "Milliseconds"]]. $\endgroup$ – J. M. will be back soon Nov 3 '17 at 18:37
1
$\begingroup$

There is a fixed offset between AbsoluteTime and UnixTime, so you can use AbsoluteTime instead of UnixTime if you want subsecond resolution. For example:

ut = (#-2208963600&)@*AbsoluteTime;

Then:

UnixTime[Today]
ut[Today]

1509692400

1509692400

and:

UnixTime[Now]
ut[Now]

1509740426

1.509740426703258*10^9

For your example:

UnixTime @ DateObject @ "2017-05-05T01:02:30.5Z"
ut @ DateObject @ "2017-05-05T01:02:30.5Z" //InputForm

1493971350

1.4939713505*^9

$\endgroup$
  • 1
    $\begingroup$ DateObject @ "2017-05-05T01:02:30.5Z" is not what you think it is. mathematica.stackexchange.com/q/144764/5478 $\endgroup$ – Kuba Nov 3 '17 at 20:48
  • $\begingroup$ @Kuba I just copied what the OP had, you should probably be commenting on the OP use of that idiom instead. My point was that my version ut provides the fractional seconds they wanted. $\endgroup$ – Carl Woll Nov 3 '17 at 21:20
  • $\begingroup$ That was fyi, nothing more. And yes I could comment on OP but it already has more comments so here it can get more attention. $\endgroup$ – Kuba Nov 3 '17 at 21:29
  • $\begingroup$ Yes, I noticed that it doesn't parse the Z at the end. Thanks for pointing it out. $\endgroup$ – Ashley Nov 6 '17 at 15:30
0
$\begingroup$

The best I could come up with is f[d_String] := Module[{do = DateObject[d, TimeZone -> "GMT"]}, 1000 * UnixTime@DateObject@do + DateValue[DateObject@do, "Millisecond"]].

$\endgroup$
0
$\begingroup$

If you want the current time in milliseconds since Epoch,

ToExpression[ToString[UnixTime[]] <> DateString["Millisecond"]]

I don't know how accurate it is given there are two functions called at different times so I can only assume there is a slight variation between the "true" time with milliseconds and this solution.

UnixTime[]

1509763110

ToExpression[ToString[UnixTime[]] <> DateString["Millisecond"]]

1509763110487
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.