System.currentTimeMillis()什么时候会溢出?
我有一个网络应用程序,它使用时间戳来订购东西,时间戳很长。我的网络应用程序后端恰好是用java编写的,所以我正在使用:(
long timestamp = System.currentTimeMillis();
大约)哪一年会失败?我的意思是在某个时候,long 的范围会溢出,对吧?我们可能都已经去世很久了,但我只是好奇。会像千年虫一样重来吗?我可以为此做哪些准备?可笑,我知道,只是好奇!
I have a web app which orders stuff using a timestamp, which is just a long. My web app backend happens to be written in java, so I am using:
long timestamp = System.currentTimeMillis();
what year (approximately) will this fail? I mean at some point, the range of a long is going to overflow, right? We may all be long-dead, but I'm just curious. Will it be like y2k all over again? What can I do to prepare for this? Ridiculous, I know, just curious!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它将
溢出
在 2.92 亿年之后 。我想说,同时还有充足的时间来发明解决方案。老实说,我不指望人类能够幸存下来。与以小时为单位的世界年龄相比,我们只存在几秒钟,而且它不会'不用花很长时间。
It will overflow at
which prints
That's thus after a bit more than 292 million years. I'd say, there's a plenty of time to invent a solution in the meanwhile. To be honest, I don't expect the humanhood to survive this. We exist only a few seconds as compared to the age of the world in hour scale and it won't take long.
尝试运行此代码:
根据您的区域设置,它会打印类似这样的内容:
未来很长一段时间,因此无需担心溢出。
Try running this code:
Which prints something like this depending on your locale:
Very long in the future, so no need to worry about overflow.
好吧,您可以在您的棺材上配备最新最好的 IT 设备/极客玩具。但不知何故,我认为它们在公元 292,278,994 年会有点“过时”。到那时你会对他们感到非常无聊。
请注意,您将有足够的时间从头开始重写操作系统以使用 128 位时钟。这听起来像是一个有趣的消磨时间的项目。 :-)
Well, you could have your coffin kitted out with the latest and greatest IT gear / geek toys. But somehow I think they will be a bit "outdated" in 292,278,994 AD. And you will be pretty bored with them by then.
Mind you, you will have enough time to rewrite the OS from scratch to use a 128 bit clock. That sounds like a fun project to wile away the time. :-)
您的网络应用程序似乎不太可能在 EST 292278994 Sun Aug 17 17:12:55 仍然存在
(由其他人计算)。那时您似乎更不可能仍然负责网络应用程序。 (如果你仍然对此负责,你将来可能会得到更高的报酬,所以现在就让它滑动,然后再收取大钱:)
很有可能是系统时钟设置不正确到一些奇怪的价值。您可以相对容易地为此做好准备 - 下面的伪代码
如果触发这些断言中的任何一个时您还活着,那么您可能会向您的客户收取大笔费用来修复系统时钟或更改断言(视情况而定)。
It seems unlikely that your web app will still be around on Sun Aug 17 17:12:55 EST 292278994
(as calculated by others). It seem even more unlikely that you will still be responsible for the web app then. (If you are still responsible for it, you will probably be paid at a higher rate in the future, so let it slide for now and collect the big bucks later:)
It is much, much more likely that the system clock is set incorrectly to some outlandish value. You can prepare for this relatively easily - pseudocode below
If you are alive when any one of those assertions is triggered, then you can probably charge your clients big bucks for either fixing the system clock or changing the assertion (as appropriate).
Java
long
的最大值为2^63 - 1
,如果将这么多毫秒转换为实际的时间单位,您会发现计数器大约会溢出2.9亿年。所以不用担心;-) 如果还有人在运行计算机,我相信到那时他们会切换到 128 位时间计数器(或选择一个新纪元)。The maximum value of a Java
long
is2^63 - 1
, and if you convert that many milliseconds into practical units of time, you find that the counter will overflow in approximately 290 million years. So don't worry about it ;-) If anyone's still around to run the computers, I'm sure they will have switched to 128-bit time counters by then (or picked a new epoch).这些答案中的错误是假设您正在运行的系统是 64 位,并返回自 1970 年以来的 millis 的 64 位表示形式。Linux 使用 32 位表示形式,溢出发生在 2038 年。
请参阅 2038年问题供参考
The mistake in these answers is assuming that the system you're running is 64 bit and returns a 64 bit representation of millis since 1970. Linux uses a 32 bit representation and overflow is in 2038.
See Year 2038 problem for reference