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!
This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Depending on the system, it can take more than 100 cpu cycles to execute.
System. currentTimeMillis() takes about 29 nanoseconds per call while System.
currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
So in short, this is pretty cheap on its own.
It will overflow at
System.out.println(new Date(Long.MAX_VALUE));
which prints
Sun Aug 17 03:12:55 GMT-04:00 292278994
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:
System.out.println(new Date(Long.MAX_VALUE));
Which prints something like this depending on your locale:
Sun Aug 17 17:12:55 EST 292278994
Very long in the future, so no need to worry about overflow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With