简体   繁体   English

GroupWise 2014日期格式为DateTime

[英]GroupWise 2014 Date Format to DateTime

I'm working with the GroupWise 2014 Rest API and I have a problem parsing their date format. 我正在使用GroupWise 2014 Rest API,但在解析其日期格式时遇到问题。

When you fetch a user you receive a json object with "timeCreated": 1419951016000 , 当您获取用户时,您会收到一个带有“ timeCreated”的json对象: 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 1419951016000的值应为2014-12-30 15:50左右的时间

Looks like unix time in milliseconds since January 1st, 1970 at UTC. 自1970年1月1日在UTC以来,以毫秒为单位的unix时间看起来像。 Current unix time in seconds is shown here as 1419964283 . 当前的以秒单位的 unix时间 此处显示为1419964283

To convert to a DateTime to unix time, see here: How to convert UNIX timestamp to DateTime and vice versa? 要将DateTime时间转换为Unix时间,请参见此处: 如何将UNIX时间戳转换为日期时间,反之亦然? . That code works for unix time in seconds ; 该代码可在Unix时间( 以秒单位)中工作 the following works for unix time in milliseconds, represented as a long : 以下内容以毫秒为单位的unix时间工作,表示为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". UnixTimeHelper.ToDateTime(1419951016000, DateTimeKind.Utc).ToString()给出值“ 12/30/2014 2:50:16 PM”。 Is your desired value of "2014-12-30 15:50" in UTC or your local time? 您是以UTC还是当地时间为“ 2014-12-30 15:50”的理想值?

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 . 如果您使用Json.NET序列化JSON,则可以编写一个自定义JsonConverter以按照以下说明从DateTime属性自动进行转换: 编写一个自定义Json.NET DateTime Converter That code also works for unix time in seconds and so will need to be tweaked. 该代码还可以在Unix时间(以秒为单位)内工作,因此需要进行调整。

(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.) (最后,引用Plutonix的建议仔细检查文档。特别是,您需要阅读文档中关于返回时间所在时区的内容。这可能是UTC,但需要花些时间才能确保。)

Update 更新

After a quick search the online doc looks pretty bad, but this page makes mention of 快速搜索后, 在线文档看起来很糟糕,但是此页面提到了

expiretime expiretime
long
Optional. 可选的。 Use an explicit expiration cut-off time. 使用明确的到期截止时间。 The time is specified as a java long time. 该时间指定为Java长时间。

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 . java.util.Date表示历元“即1970年1月1日00:00:00 GMT‘为’自称为标准基准时间指定的毫秒数的” long

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM