时间戳字符串长度

发布于 2024-10-07 06:44:41 字数 292 浏览 7 评论 0原文

如果我这样做,

// Default implementation of UNIX time of the current UTC time
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string myResult = "";
myResult = Convert.ToInt64(ts.TotalSeconds).ToString();

myResult 的最大字符串长度是多少?它的大小总是相同吗?

If I did this

// Default implementation of UNIX time of the current UTC time
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string myResult = "";
myResult = Convert.ToInt64(ts.TotalSeconds).ToString();

What is the maximum string length of myResult and is it always the same size?

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

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

发布评论

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

评论(4

情泪▽动烟 2024-10-14 06:44:41

Int64 是有符号的 64 位整数,这意味着它的值范围从 −9,223,372,036,854,775,8089,223,372,036,854,775,807

由于 toString 不使用逗号格式化其输出,因此字符串的最长可能值将为 −9223372036854775808,即 20 个字符长。

现在,由于这代表 UNIX 时间戳,我们需要考虑什么被认为是返回的“合理”日期。当我写这篇文章时,当前的 UNIX 时间戳将接近 1292051460,这是一个 10 位数字。

假设最大长度为 10 个字符,则为您提供从 -999999999999999999 的时间戳范围。这将为您提供从“Mon, 31 Oct 1966 14:13:21 GMT”到“Sat, 20 Nov 2286 17:46:39 GMT”的日期范围。请注意,我将否定符号作为下界中的字符包含在内,这就是为什么下界比上限更接近纪元的原因。

如果您预计的日期不是 1966 年万圣节之前或 2286 年 11 月下旬之后,您可以合理地假设字符串的长度不会超过 10 个字符。如果您期望的日期超出此范围(很可能是 1966 年之前而不是 2286 年之后),您可能会看到 11 个字符的字符串。我不会期望更多。

这是预期的最大字符数;它可以更短。

An Int64 is a signed 64-bit integer, which means it has a range of values from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Since toString doesn't format its output with commas, the longest possible value of the string would be −9223372036854775808 which is 20 characters long.

Now, since this is representing a UNIX timestamp we need to take into consideration what would be considered a "reasonable" date to return. As I write this, a current UNIX timestamp would be something close to 1292051460, which is a 10-digit number.

Assuming a maximum length of 10 characters gives you a range of timestamps from -99999999 to 9999999999. This gives you a range of dates from "Mon, 31 Oct 1966 14:13:21 GMT" to "Sat, 20 Nov 2286 17:46:39 GMT". Note that I'm including the negation symbol as a character in the lower bound, which is why the lower bound is so much closer to the epoch than the upper bound.

If you're not expecting dates before Halloween 1966 or after late November 2286, you can reasonably assume that the length of the string won't exceed 10 characters. If you are expecting dates outside of this range (most likely pre-1966 rather than post-2286), you can expect to see an 11 character string. I wouldn't expect any more than that.

That's the maximum number of characters to expect; it could be shorter.

秋心╮凉 2024-10-14 06:44:41

假设该代码在遥远的将来使用,它将是 Int64 的最大长度。

例如,现在该值为 1292022273,因此长度将为 10。

您可以在此处找到包含秒数的计算器 http://www.timeanddate.com/date/duration.html

Assuming the code is used far into the future it would be the maximum length of an Int64.

For example, right now that value is 1292022273 so the length would be 10.

You can find a calculator that includes the seconds here http://www.timeanddate.com/date/duration.html

雄赳赳气昂昂 2024-10-14 06:44:41

如果您坚持使用 Convert.ToInt64() 且不带格式,则最大长度将为 20,因为最小 Int64-9223372036854775808(负号需要额外的字符)。但实际上,由于 TimeSpanDateTime 的限制,它不会利用 Int64 提供的整个范围。

而且,不,myResult 的长度并不总是相同,而是可以在 1 到 20 之间。它仅取决于 Convert.ToInt64(ts.TotalSeconds)< 的当前值/代码>。

If you stick with Convert.ToInt64() with no formatting, then your maximum length will be 20, because the minimum Int64 is -9223372036854775808 (the negative sign requires an extra character). In practice, however, it will not utilize the entire range afforded by Int64 due to limitations in TimeSpan and DateTime.

And, no, the length of myResult will not always be the same, but can range from 1 to 20. It just depends on the current value of Convert.ToInt64(ts.TotalSeconds).

江城子 2024-10-14 06:44:41

要获得 TimeSpan 的最大大小,请尝试使用此代码:

var maxValue = Convert.ToInt64(TimeSpan.MaxValue).ToString();

希望它能帮助您解决问题!祝你好运!

最好的问候,迪玛。

To get max size of the TimeSpan try to use this code:

var maxValue = Convert.ToInt64(TimeSpan.MaxValue).ToString();

Hope it will help you with your question! Good luck!

Best regards, Dima.

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