简体   繁体   English

C# 将 UTC int 转换为 DateTime object

[英]C# convert UTC int to DateTime object

I don't know why this is so complicated!我不知道为什么这么复杂!

I have a plugin that is passing in a long int UTC.我有一个传入 long int UTC 的插件。 I need to convert that number into a DateTime to query my database (SQL Server).我需要将该数字转换为DateTime来查询我的数据库(SQL Server)。

I don't know why, but I can't find a workable answer from a basic google search.我不知道为什么,但我无法从基本的谷歌搜索中找到可行的答案。

(For extra credit, I need to turn my returned DateTime back into a UTC at the end of the day.) (为了获得额外的积分,我需要在一天结束时将返回的DateTime转换回 UTC。)

This is embarrassing to have to ask such a basic question: :)不得不问这样一个基本问题真是令人尴尬::)

My guess is it's going to be either milliseconds or seconds since a particular epoch - quite possibly the Unix epoch of January 1st 1970, midnight UTC.我的猜测是,自特定纪元以来将是毫秒或秒 - 很可能是 1970 年 1 月 1 日午夜 UTC 的 Unix 纪元。

So the code would look something like:所以代码看起来像:

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0,
                                                          DateTimeKind.Utc);

public static DateTime FromMillisecondsSinceUnixEpoch(long milliseconds)
{
    return UnixEpoch.AddMilliseconds(milliseconds);
}

Make the obvious changes for seconds, or from a different epoch:)几秒钟内做出明显的改变,或者从不同的时代开始:)

An alternative approach is to create a TimeSpan of the seconds/milliseconds since the epoch, and then add it to the epoch:另一种方法是创建自纪元以来的秒/毫秒的TimeSpan ,然后将其添加到纪元:

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0,
                                                          DateTimeKind.Utc);

public static DateTime FromMillisecondsSinceUnixEpoch(long milliseconds)
{
    return UnixEpoch + TimeSpan.FromMilliseconds(milliseconds);
}

I don't know of any significant difference between them - although the fact that AddMilliseconds takes a double instead of a long suggests that for very large values, the TimeSpan approach may be preferable.我不知道它们之间有任何显着差异 - 尽管AddMilliseconds采用double而不是long的事实表明,对于非常大的值, TimeSpan方法可能更可取。 I doubt that it'll make any difference though:)我怀疑它会有所作为:)

Is the int you get seconds, milliseconds, or what?你得到的 int 是秒、毫秒还是什么? After converting it to ticks, (one .NET tick is 100 nanoseconds) eg by long ticks = theDBDateNum*TimeSpan.TicksPerMillisecond;将其转换为滴答声后(一个 .NET 滴答声为 100 纳秒),例如long ticks = theDBDateNum*TimeSpan.TicksPerMillisecond; , try this: , 尝试这个:

DateTime theDate = new DateTime(ticks, DateTimeKind.Utc);

According to https://www.epochconverter.com/ ,根据https://www.epochconverter.com/

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds. Unix 纪元(或 Unix 时间或 POSIX 时间或 Unix 时间戳)是自 1970 年 1 月 1 日以来经过的秒数,不包括闰秒/格林威治标准时间午夜。

And later,然后,

 var epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;

Later still,再后来,

 private string epoch2string(int epoch) { return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch).ToShortDateString(); }

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

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