How to test if a given time-stamp is in seconds or milliseconds?

后端 未结 4 439
青春惊慌失措
青春惊慌失措 2021-02-03 19:45

Assume a given variable, it is containing a UNIX time-stamp, but whether it is in seconds or milliseconds format is unknown, I want to assign to a variable which i

4条回答
  •  时光说笑
    2021-02-03 20:13

    At the risk of attracting downvotes, I want to go on record as saying DON'T DO IT.

    Making assumptions about the units of a physical quantity is a terrible idea - it led to the destruction of the Mars Meteorological Orbiter (the calculated units of thrust were in pounds, the engines expected Newtons. Not enough thrust - and the orbiter burnt up in the atmosphere).

    If you have a pretty good idea of the magnitude of a number, you ought to be able to make an "educated guess" at the units. For example, if I tell you my weight is 230, you could look at me and think "I bet that is pounds, not kilos". Similarly, if you convert a time stamp of my birthday to a date on the assumption it was in seconds, and it turns out to be 200 years in the future, you can reasonably assume that "seconds" was the wrong unit.

    But it is a DANGEROUS thing to do.

    Still - if you have to do it, then in general, I would recommend the following approach:

    1. Given all the possible units, make the conversion to a "sensible" scale (for example, convert your time stamp to a year/month/day).
    2. Look at the values you obtained. Which of these is "most likely". In the case of dates, you might have a good idea (date this computer came into service: was it 1970 or 2012? etc)
    3. Choose the value that is most likely
    4. Record somewhere that this is what you did - so that if the consequences of this decision turn out to be wrong, you can trace the error

    That last thing is important - it will allow you to learn from your mistake. For example, if your time stamp tells you something about when the comet will next appear, you could print

    Comet's next expected appearance: December 29, 2546 (assumption: time stamp from X was given in seconds)

    In this way, there is some "come back". In general, "document your assumptions" is not just good advice - it is essential in any exact science.

    Finally - the conversion of a time stamp from ms to s is achieved by dividing the number by 1000. But I'm sure you know that.

提交回复
热议问题