DateTime in Unity as a very large number - datetime

I'm trying to understand a saved game file format.
The game is written with Unity.(not sure that it's related)
The file is a JSON file and the timestamp is:
"Timestamp": 637015624965194930,
This translates to Aug 16th 2019 at 14:28
another example is:
"Timestamp": 637014628610503310,
This translates to Aug 15th 2019 at 10:47
I figured that subtracting the 2 numbers will give me the difference and it kind of works.
It seems that the time is measured in 1/10000000 of a second.
Can anyone figure out how to convert this to EPOCH time?

This time is 1/10000000 of a second since Jan 1st 0001
It is the standard time format used by .NET
so you can subtract 621355968000000000 (Jan 1st 1970) and divide by 10000000 to get seconds since EPOCH.

Related

What's the difference between Epoch Timestamp and Unix time?

Calling an API, I need to specify time in milliseconds. I use DateTime in Python 3 to convert from and to human readable dates and times. But when debugging, I get different results depending on what website I use to convert, so I'm having a hard time debugging when the timestamps are (maybe) wrong. The API doesn't combine time and date in their predicates, but uses milliseconds for both.
Let's look at the date predicate 1656547200000
If I go to https://currentmillis.com it says it's June 30th. All good.
If I go to https://www.epochconverter.com it says it's June 30th. All good.
Let's look at the time predicate from 12600000 to 26700000.
12600000, 26700000
If I go to https://currentmillis.com it says it's UTC (24h) 03:30 - 07:25
If I go to https://www.epochconverter.com it says it's UTC (24h) 20:00 - 00:40
Why that different results?
Historically, UNIX systems reckon time as temporal units (seconds, millis, nanos, etc.) elapsed since an absolute origin instant termed "the Epoch": January 1st, 1970 00:00:00 UTC. This is formally recognized in POSIX.
So "UNIX time" is that system of reckoning, and "Epoch timestamps" are points in time in that system.
Now, you appear to me to be conflating temporal units in your use of Epoch timestamps.
In the case of your "short" timestamp, 12600000 seconds since the Epoch is a different point in time than 12600000 milliseconds since the Epoch. That's why you see them resolve to different times of day, as your converters are interpreting them differently. If you'd included the date in your output, you'd have seen the two points in time are almost six months apart.

SQLite timestamp export

I have a SQLite 3 database on MacOS with timestamp data (typically something like 279020203.539467).
The documentation says that dates can be stored as
REAL - as Julian day numbers, the number of days since noon in Greenwich
on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
And it looks like this is what I've got.
I want to export this and import it into other databases.
I am assuming the timestamp datatype is not compatible between database engines, so some conversion has to happen somewhere, and I am working under the assumption that it'd be best to do that while exporting the data from the SQLite database.
But I can't figure out how to do this conversion.
I've looked at this answer which refers to this forum post which indicates that in SQLite you could do something like
select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime');
(Maybe 2415018 is the number of days between November 24, 4714 B.C. and some other magical date...)
However, replacing the timestamp string in this example with what I have results in null. Presumably because '279020203.539467' is some other kind of timestamp. It is also some magnitudes larger than the example.
But how to convert this to a usable date? I know it should be around 2011/2012.
Interpreting the data as an "integer" (seconds since 1970-01-01) gives 1978 so that is not correct either.
UPDATE: I've found that
288773834.371606 should be 2010-02-25 07.57
296636121.950064 should be 2010-05-27 08.55
(CET if that matters).
The good news is: To convert a Julian date to "regular" date format you could use datetime(strftime('%J',jtime)). FYI Here's the doc for sqlite date and time functions. But there's bad news.
A NASA Calculator computes the Julian date of 2010-02-25 to be 2455246. It computes the civil date of 288773834 as Sept 2, 785907 A.D. sqlite doesn't give that same result using the above notation, but it doesn't give a "date".
Even though the numbers look like Julian date notation, they are not any dates in our lifetimes.
DATEDIF("2010-02-25 07.57"; "2010-05-27 08.55"; "d")
This gives 91 days which works out to be 7862400 seconds, almost exactly the same as the difference between the two timestamps (296636121.950064 - 288773834.371606 = 7862287.578458).
On MacOS native timestamps are seconds from 2001-01-01 (Jan 1 2001).
And
DATEDIF("2001-01-01 00:00"; "2010-02-25 07.57"; "d")
gives 3342 days which is 288748800 seconds, near, but not exactly the timestamp but that difference is caused by
DATEDIF only caters for whole days
CET is one hour off
Correcting for that we get around 25000 seconds more to add, which takes us almost exactly to the timestamp for 2010-02-25 07.57.
So the gist of this is that SQLite on MacOS stores timestamps as MacOS native timestamps which are seconds since start of MacOS epoch (2001-01-01 00.00). This is probably caused by the application that created the data was not using SQLite timestamps but MacOS native dates and storing them in the database as any other data.
Converting this to some other format should be trivial, either during the export, a conversion of the exported file or during imports.
Possibly it would be easiest to convert the date on export from the original database using
select ...
datetime(table.timestamp_field, 'unixepoch', '+31 years')
from ...

why Unix Time Stamp for same time is different in different timezone

Why 7/18/2013 11:33 is different in GMT timezone and in my local Time Zone (Asia/kolkata)?
As Unix time-stamp are the ticks being calculated since epoch time 1/1/1970 00:00:00 GMT so i know that there the epoch time had occurred at different interval in different timezone but still. the number of second elapsed should have been same
For example if I(+5:30 GMT) and My friend(+5:00 GMT) starts counting the ticks from 00:00 Hrs respectively so at 18:00 Hrs in both timezone number of ticks should be same. So why same thing is not true with the Unix time-stamp.
Need to Understand the concept fully.
For example if I(+5:30 GMT) and My friend(+5:00 GMT) starts counting the ticks from 00:00 Hrs respectively so at 18:00 Hrs in both timezone number of ticks should be same.
No, because both of you start counting from 00:00 UTC. That's the definition. So for you, that will mean the number of ticks since 18:30, and for your friend it will mean the number of ticks since 19:00.
The idea is that a single instant in time has the same timestamp value everywhere. So if I were calling you now (and ignoring phone delays) we could both agree that "now" is a Unix timestamp of 1374130418. You may have a different local time to me, but we can express "now" in a common format.
See the "core concepts" part of the Noda Time user guide for more discussion of local time vs "global" time.
When your system is configured correctly, the unix time stamp shows you the ticks since 1.1.1970 in Greenwich. So when you an your friend read the timestamp at 18:00 at your local time, you do it with 30 minutes delay and therefore have 1800 seconds difference.
If this isn't the case, one or both of the system clocks isn't set correctly to use GMT as base. Usually this kind of problem doesn't have negative impact as long as you're just working on one system or all systems are set incorrectly the same way.
The Unix time is given as seconds since the epoch: the number of seconds (not counting leap seconds) that have passed since 00:00:00 Coordinated Universal Time (UTC), or Thursday, January 1st, 1970,
The GNU date command has some very nice features that allow you to translate between different time formats. These are explained very nicely in man date so I will only give you some examples here:
### "Normal" format
$ date
Thu Jun 12 11:44:23 CEST 2014
### Unix time
$ date +%s
1402566271
To convert, you can give date a specific date using the -d flag. However, to get a Unix date, this needs to be a full date. You can't convert 3:00PM to Unix time since Unix time refers to an entire date (year,month,day,time). So, for example, to get the Unix date for the 12th of September 1987, you would do:
$ date -d "3 PM 12 September 1987" +%s
558450000
And to convert that back to a "normal" date:
$ date -d "#558450000"
Sat Sep 12 15:00:00 CEST 1987

What does this number mean?

I have one number that has something related to the currentdate :
634101448539930000
634101448627430000 (this information was took 9 seconds later than the first one)
I have many codes like this and I need to know what those number means. It's something related to the current time, because the new information has always a bigger number than the older ones.
Please if anybody could help me, thanks
It's a Tick Count.
There are 10,000 ticks in 1 ms. That number is the number of milliseconds that have passed since January 1st, 0001 at midnight.
This particular tick count represents the date/time 2010-05-22 17:07:33 (makes sense, since that's today).
Well, the second number is 87500000 greater than the first, and since they're about 9 seconds apart, I'd guess it was number of 100 nanoseconds since some epoch.
If you divide the number by 1x10^7, you get approximately the number of seconds since 1 Jan 0001 (ignoring the calendar reforms and all that stuff).
Dates and times are generally stored as a single number in computer languages. The number usually represents an offset from a specific data. For example it might be an offset from 1 Jan 1970. Usually you don't deal with these numbers. I suspect that if you look at your APIs you will find a function to convert these numbers into more meaningful representations.
It appears that those numbers represent your system's internal system time with precision down to the decimicrosecond (one ten-millionth of a second), as measured from the date January 1st, 1 A.D.
634101448627430000 - 634101448539930000 = 87500000
87500000 / 10,000,000 = 8.75
And, using Perl's Time::Duration module:
&duration_exact(634101448627430000/10_000_000);
2010 years, 263 days, 17 hours, 7 minutes, and 42 seconds
So from that we know that 63410144862 seconds ago was 2010 years ago, so the timestamp is based at the year 1 A.D.
It could be a variation of Unix Time ( a converter for unix time is available here ).
Unix time is a way of representing a date/time as a number that is easy to store and compare. Your numbers may be a substring of unix time, or may be a format designed by the author. I would examine your ASP.NET code to check how it is used, and you'll find out what it means.

What's the best format to represent one day?

How to represent the start and end times for one day?
Using October 23, 2008 as an example, is it start 2008-10-23 12:00:00 AM and end 2008-10-23 11:59:59 PM?
I would like to cite a site, http://www.cl.cam.ac.uk/~mgk25/iso-time.html
The international standard notation for the time of day is
hh:mm:ss
where hh is the number of complete hours that have passed since midnight (00-24), mm is the number of complete minutes that have passed since the start of the hour (00-59), and ss is the number of complete seconds since the start of the minute (00-60). If the hour value is 24, then the minute and second values must be zero.
Note: The value 60 for ss might sometimes be needed during an inserted leap second in an atomic time scale like Coordinated Universal Time (UTC). A single leap second 23:59:60 is inserted into the UTC time scale every few years as announced by the International Earth Rotation Service in Paris, to keep UTC from wandering away more than 0.9 s from the less constant astronomical time scale UT1, which is defined by the actual rotation of the earth. In practice you are not very likely to see a clock showing 23:59:60. Most synchronized clocks resynchronize again to UTC some time after a leap second has happened, or they temporarily slow down near the time of a leap seconds, to avoid any disruption that an out-of-range timestamp might otherwise cause.
An example time is
23:59:59
which represents the time one second before midnight.
As with the date notation, the separating colons can also be omitted as in
235959
and the precision can be reduced by omitting the seconds or both the seconds and minutes as in
23:59, 2359, or 23
It is also possible to add fractions of a second after a decimal dot or comma, for instance the time 5.8 ms before midnight can be written as
23:59:59.9942 or 235959.9942
As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time:
1995-02-04 24:00 = 1995-02-05 00:00
In case an unambiguous representation of time is required, 00:00 is usually the preferred notation for midnight and not 24:00. Digital clocks display 00:00 and not 24:00.
ISO 8601 does not specify, whether its notations specify a point in time or a time period. This means for example that ISO 8601 does not define whether 09:00 refers to the exact end of the ninth hour of the day or the period from 09:00 to 09:01 or anything else. The users of the standard must somehow agree on the exact interpretation of the time notation if this should be of any concern.
If a date and a time are displayed on the same line, then always write the date in front of the time. If a date and a time value are stored together in a single data field, then ISO 8601 suggests that they should be separated by a latin capital letter T, as in 19951231T235959.
A remark for readers from the U.S.:
The 24h time notation specified here has already been the de-facto standard all over the world in written language for decades. The only exception are a few English speaking countries, where still notations with hours between 1 and 12 and additions like “a.m.” and “p.m.” are in wide use. The common 24h international standard notation is widely used now even in England (e.g. at airports, cinemas, bus/train timetables, etc.). Most other languages do not even have abbreviations like “a.m.” and “p.m.” and the 12h notation is certainly hardly ever used on Continental Europe to write or display a time. Even in the U.S., the military and computer programmers have been using the 24h notation for a long time.
The old English 12h notation has many disadvantages like:
It is longer than the normal 24h notation.
It takes somewhat more time for humans to compare two times in 12h notation.
It is not clear, how 00:00, 12:00 and 24:00 are represented. Even encyclopedias and style manuals contain contradicting descriptions and a common quick fix seems to be to avoid “12:00 a.m./p.m.” altogether and write “noon”, “midnight”, or “12:01 a.m./p.m.” instead, although the word “midnight” still does not distinguish between 00:00 and 24:00 (midnight at the start or end of a given day).
It makes people often believe that the next day starts at the overflow from “12:59 a.m.” to “1:00 a.m.”, which is a common problem not only when people try to program the timer of VCRs shortly after midnight.
It is not easily comparable with a string compare operation.
It is not immediately clear for the unaware, whether the time between “12:00 a.m./p.m.” and “1:00 a.m./p.m.” starts at 00:00 or at 12:00, i.e. the English 12h notation is more difficult to understand.
Please consider the 12h time to be a relic from the dark ages when Roman numerals were used, the number zero had not yet been invented and analog clocks were the only known form of displaying a time. Please avoid using it today, especially in technical applications! Even in the U.S., the widely respected Chicago Manual of Style now recommends using the international standard time notation in publications.
Surely, if you just want to represent 1 day, you don't need to include the time at all - especially as this raises such a level of discussion about when a day starts or ends. In my experience, date handling is usually complicated enough, without introducing any extra complexity.
Today is Oct 20th 2008 - no more information is necessary.
Or am I missing something?
Oct. 23 starts at 2008-10-23 12AM and finishes at 2008-10-24 12AM—a day ends at the exact same point the next one begins. The very last second begins at 11:59:59 PM but you still have a whole second before the day is over.
Your notation is strange. I'd suggest the standard way of specifying a full day range is:
2008-10-23 00:00:00 and finish 2008-10-23 23:59:59
The definition of the day you mention in the question is any time that is >= 2008-10-23 00:00:00 and < 2008-10-24 00:00:00 .
If you were to use mathematical interval notation, you would write it as
[2008-10-23 00:00:00 , 2008-10-24 00:00:00)
The [ means inclusion, and ) means up to but not including.
In this way you make it easier for users and for yourself.
It depends what you mean by a day. Date handling is, sadly, always more complex than it looks.
To convert your example into 24h ISO date format, you'd say:
2008-10-23 00:00:00 - 2008-10-23 23:59:59
However depending on whether you interpret a range as inclusive or exclusive that could omit the last second of the day*. Programmers normally prefer to keep the upper bound exclusive, so you'd go for:
2008-10-23 00:00:00 - 2008-10-24 00:00:00
Or, as a slightly nicer way of stating the same, if supported (or for human readability):
2008-10-23 00:00:00 - 2008-10-23 24:00:00
*: actually possibly more than one. If the span represents a day in the UTC timezone (or another timezone aligned to it, which is quite likely), there can occasionally be an extra 'leap second' at the end of the day, 23:59:60, in a pointless attempt to keep UTC in line with sidereal time. The next leap second is planned for the end of this year. However, many systems ignore leap seconds because they're silly and annoying.
If you want to represent a 'calendar day' independently of timezones your best bet is the Julian day number. Today is 2454760.

Resources