GroupWise 2014 Date Format to DateTime

186 views Asked by At

I'm working with the GroupWise 2014 Rest API and I have a problem parsing their date format.

When you fetch a user you receive a json object with "timeCreated": 1419951016000,

But I can't figure out what format that date is.

I've tried

DateTime.Parse
DateTime.FromFileTime
DateTime.FromFileTimeUtc

The value 1419951016000 should be around the time 2014-12-30 15:50

1

There are 1 answers

0
dbc On BEST ANSWER

Looks like unix time in milliseconds since January 1st, 1970 at UTC. Current unix time in seconds is shown here as 1419964283.

To convert to a DateTime to unix time, see here: How to convert UNIX timestamp to DateTime and vice versa?. That code works for unix time in seconds; the following works for unix time in milliseconds, represented as a long:

public static class UnixTimeHelper
{
    const long MillisecondsToTicks = 10000;
    static readonly DateTime utcEpochStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

    static DateTime UtcEpochStart { get { return utcEpochStart; }}

    public static DateTime ToDateTime(long unixTimeInMs, DateTimeKind kind)
    {
        var dateTime = UtcEpochStart + new TimeSpan(MillisecondsToTicks * unixTimeInMs);
        if (kind == DateTimeKind.Local)
            dateTime = dateTime.ToLocalTime();
        return dateTime;
    }

    public static long ToUnixTimeInMs(DateTime dateTime)
    {
        if (dateTime.Kind == DateTimeKind.Local)
            dateTime = dateTime.ToUniversalTime();
        var span = dateTime - UtcEpochStart;
        return (long)(span.Ticks / MillisecondsToTicks);
    }
}

With this code. UnixTimeHelper.ToDateTime(1419951016000, DateTimeKind.Utc).ToString() gives the value "12/30/2014 2:50:16 PM". Is your desired value of "2014-12-30 15:50" in UTC or your local time?

If you are using Json.NET to serialize your JSON, you can write a custom JsonConverter to do the conversion automatically from a DateTime property using the instructions here: Writing a custom Json.NET DateTime Converter . That code also works for unix time in seconds and so will need to be tweaked.

(Finally, seconding Plutonix's suggestion to double-check the documentation. In particular you need to read what the documentation says about the time zone in which the times are returned. It's probably UTC but it pays to make sure.)

Update

After a quick search the online doc looks pretty bad, but this page makes mention of

expiretime
long
Optional. Use an explicit expiration cut-off time. The time is specified as a java long time.

java.util.Date represents "the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT" as a long.