C# 将 UTC int 转换为 DateTime 对象

发布于 2024-11-18 04:41:40 字数 238 浏览 2 评论 0原文

我不知道为什么这么复杂!

我有一个传递 long int UTC 的插件。我需要将该数字转换为 DateTime 来查询我的数据库 (SQL Server)。

我不知道为什么,但我无法从基本的谷歌搜索中找到可行的答案。

(为了额外加分,我需要在一天结束时将返回的 DateTime 转回 UTC。)

不得不问这样一个基本问题真是令人尴尬! :)

I don't know why this is so complicated!

I have a plugin that is passing in a long int UTC. I need to convert that number into a DateTime to query my database (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.)

This is embarrassing to have to ask such a basic question! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑,眼淚并存 2024-11-25 04:41:40

我的猜测是,这将是自特定纪元以来的毫秒或秒——很可能是 1970 年 1 月 1 日午夜 UTC 的 Unix 纪元。

因此,代码看起来像这样:

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);
}

对秒进行明显的更改,或者从不同的纪元开始:)

另一种方法是创建自纪元以来的秒/毫秒的 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);
}

我不知道它们之间有什么显着差异 - 尽管 AddMilliseconds 采用 double 而不是 long 的事实表明对于非常大的值,TimeSpan 方法可能更可取。我怀疑这会有什么不同:)

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.

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:

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. I doubt that it'll make any difference though :)

吾性傲以野 2024-11-25 04:41:40

你得到的 int 是秒、毫秒还是什么?将其转换为刻度后(一个 .NET 刻度为 100 纳秒),例如通过 longticks = theDBDateNum*TimeSpan.TicksPerMillisecond;,尝试以下操作:

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

Is the int you get seconds, milliseconds, or what? After converting it to ticks, (one .NET tick is 100 nanoseconds) e.g. by long ticks = theDBDateNum*TimeSpan.TicksPerMillisecond;, try this:

DateTime theDate = new DateTime(ticks, DateTimeKind.Utc);
把梦留给海 2024-11-25 04:41:40

根据 https://www.epochconverter.com/

Unix 纪元(或 Unix 时间或 POSIX 时间或 Unix 时间戳)是自 1970 年 1 月 1 日(UTC/GMT 午夜)以来经过的秒数,不包括闰秒。

后来,

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

再后来,

私有字符串 epoch2string(int epoch) {
返回新的 DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch).ToShortDateString(); }

According to 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.

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(); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文