Questions tagged [unix-timestamp]

The number of seconds between a particular date and the Unix Epoch on January 1st, 1970

POSIX definition

The POSIX.1 definition of Unix time is a number which is zero at the Unix epoch (1970-01-01T00:00:00Z), and increases by exactly 86 400 per day. Epoch and day ordinals are based on UTC.

The subtlety in this definition comes from the fact that days aren't exactly 86 400 seconds long. POSIX timestamps grow at 1Hz during the day, then end the day with small jumps to adjust for the duration of the UTC day.

For example, 2004-09-16T00:00:00Z, 12 677 days after the epoch, is represented by the Unix time number 12 677 × 86 400 = 1 095 292 800. The time interval between the epoch and 2004-09-16T00:00:00Z actually lasted 12 677 × 86 400 + 22 seconds.

This definition can be extended to represent instants before the epoch using negative numbers. 1957-10-04T00:00:00Z, 4 472 days before the epoch, is represented by the Unix time number -4 472 × 86 400 = -386 380 800. UTC is not defined for these instants, but universal time (any time standard that counts days from midnight at the reference meridian, such as the Julian Day) can be used, and the reduced accuracy is unlikely to matter.

POSIX provides for sub-second resolution with struct timespec, a fixed point format with a tv_nsec struct member for nanoseconds. This format is useful for system interfaces, but unsuitable for serialisation (naive range-checking could leave holes).

POSIX timestamps are ambiguous, discontinuous, and non-monotonic across leap seconds. When a leap second is inserted, a 1s range of Unix timestamps is repeated, first representing the leap second, then representing the first second of the next day (some implementations repeat the timestamp range immediately before the leap second instead). In the theoretical case of negative leap seconds, there would be 1s ranges of Unix time that do not represent any instant in time. The rest of the time, these Unix timestamps are continuous, unambiguous, and grow monotonically by 1s every second. The ambiguity isn't introduced by UTC, which measures time broken down in components and not as a single number.

System timestamps

On Unix systems, the CLOCK_REALTIME clock represents Unix time on a best-effort basis, based on hardware and network support. It may jump if the system clock is too far from reference time. Different clocks, representing different notions of system time, are exposed through clock_gettime. On Linux, CLOCK_MONOTONIC is monotonic and continuous (with no time elapsing when the system is suspended). It may speed up or slow down when adjtime is called, typically through NTP steering (clock slew). CLOCK_BOOTTIME is also monotonic, but will continue growing when the system is suspended. CLOCK_MONOTONIC_RAW is like CLOCK_MONOTONIC, but matches the speed of the hardware clock and ignores adjtime adjustments to clock speed. CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID count CPU time consumed by the process and thread, respectively. Linux also provides coarse variants that may provide better performance.

Timestamps recorded by the kernel (for example, modification times on filesystem inodes) follow the CLOCK_REALTIME clock.

Assuming CLOCK_REALTIME follows POSIX time, getting unambiguous time (UTC or TAI) from the kernel is an unsolved problem; adjtimex might expose enough internal state but it is highly implementation dependent. Breaking from the standard brings its own tradeoffs.

Alternative timestamps

POSIX.1b-1993 switched the definition of Unix timestamps away from a simple second count from the epoch. This introduced a few drawbacks: timestamps do not represent instants unambiguously, and Unix time is discontinuous and jumps backwards. The jumps are rare, thus hard to test for. Bugs can be subtle and are most likely to be discovered in production, after developers have moved on.

TAI-10 (TAI minus ten seconds) hits midnight at the Unix epoch. TAI is an ideal timestamp format; it grows perfectly linearly at 1/s.

Redefining CLOCK_REALTIME to follow an alternative to POSIX time is doable, but not advisable unless you control the system entirely. Setting the clock to TAI-10, applications that use localtime will still work, with /etc/localtime pointing to the Olson "right" timezones, but many applications expect to compute UTC days from timestamp / 86_400. Redefining CLOCK_REALTIME indirectly, through a tweaked NTP server, is more feasible; many applications will survive slightly varying clock speeds. This is the leap smear technique, which silently replaces UTC with UTC-SLS (smoothed leap seconds).

Other proposals aim to extend the clock_gettime interface instead of replacing the default clock. One is CLOCK_UTC, which encodes the leap second by growing tv_nsec beyond the [0, NSEC_PER_SEC] range, removing the ambiguity of CLOCK_REALTIME. The other is CLOCK_TAI, which simply encodes TAI.

time_t binary representation

ABIs where time_t is 32 bits are unable to represent times beyond January 2038; their timestamps will jump into the early twentieth century instead. This will prove a problem for some embedded systems that are being deployed now. clock_gettime/timespec_get, 64 bit integers, or other fixed-point formats like TAI64 should be used instead.

Use in protocols and serialisation

Unix timestamps are sometimes persisted, for example through serialisation or archive formats. Most filesystems use them for inode metadata. Internet protocols and formats systematically prefer RFC 3339/ISO 8601 datetimes. The SQL timestamp type is a Unix timestamp; when (fixed-offset) timezones are used, naive datetimes are translated to UTC at the storage boundary. TAI64 has been proposed to address the interoperability shortcomings of POSIX timestamps (and of time_t). When the extra compactness of integers isn't required, RFC 3339 UTC datetimes are self-describing and provide better portability, readability and widespread support.

2326 questions
44
votes
5 answers

Convert std::chrono::time_point to unix timestamp

How can I get an std::chrono::duration since a fixed date? I need this to convert a std::chrono::time_point to an unix timestamp. Insert code into XXX auto unix_epoch_start = XXX; auto time = std::chrono::system_clock::now(); auto delta = time -…
Daniel
  • 2,993
  • 2
  • 23
  • 40
42
votes
5 answers

What's the point of a timestamp in OAuth if a Nonce can only be used one time?

I had at first misinterpreted the timestamp implementation of OAuth into thinking that it meant a timestamp that was not within 30 seconds past the current time would be denied, it turned out this was wrong for a few reasons including the fact that…
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
42
votes
4 answers

MySQL: What's the best to use, Unix TimeStamp Or DATETIME

Probably many coders want to ask this question. it is What's the adventages of each one of those MySQL time formats. and which one you will prefer to use it in your apps. For me i use Unix timestamp because maybe i find it easy to convert & order…
CodeOverload
  • 47,274
  • 54
  • 131
  • 219
42
votes
1 answer

Ruby: convert unix timestamp to date

irb> dt = Time.now.utc => 2012-04-26 10:47:01 UTC irb> dti = dt.to_i => 1335437221 Now, how to convert dti back to Date/Time ?
Paul
  • 25,812
  • 38
  • 124
  • 247
40
votes
4 answers

How do I deserialize timestamps that are in seconds with Jackson?

I've got some JSON that has timestamps in seconds (i.e. a Unix timestamp): {"foo":"bar","timestamp":1386280997} Asking Jackson to deserialize this into an object with a DateTime field for the timestamp results in 1970-01-17T01:11:25.983Z, a time…
Drew Stephens
  • 17,207
  • 15
  • 66
  • 82
39
votes
4 answers

Date string conversion to unix timestamp in JavaScript / jQuery

I have date string in this format: 2009-07-15 00:00:00 - 2009-07-15 08:16:23 Could anyone help me to tweak the date string to unix timestamp into something like this: 1247634000 - 1247663783 I am using GMT-5 Timezone. What JavaScript or jQuery…
sozhen
  • 7,627
  • 14
  • 36
  • 53
37
votes
2 answers

Unix time conversions in C#

I am trying to get the GMT in unix time. I use the following code: public static long GetGMTInMS() { var unixTime = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); …
user489041
  • 27,916
  • 55
  • 135
  • 204
37
votes
3 answers

How to convert date string to timestamp in Kotlin?

I want to convert a date string to unix timestamp from a date string e.g. 14-02-2018 Can someone help?
Aefits
  • 3,399
  • 6
  • 28
  • 46
37
votes
4 answers

Ticks between Unix epoch and GPS epoch

What is the number of one second ticks between Unix time epoch (01 Jan 1970) and GPS time epoch (06 Jan 1980)? I have seen multiple answers from several sources on the web. One camp claims the answer is 315964800, the other claims it is 315964819. I…
kmort
  • 2,848
  • 2
  • 32
  • 54
37
votes
3 answers

How do I get a Unix Timestamp in Clojure?

I want to know what the current timestamp is. The best I can do thus far is define my own function that does this: (int (/ (.getTime (java.util.Date.)) 1000)) Is there a standard function for fetching the Unix timestamp with clojure?
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
37
votes
6 answers

django Datefield to Unix timestamp

In a model I have a such field: mydate = models.DateField() now a javascript graph function requires unix timestamp such as "1196550000000", how can I return the unix timestamp of my mydate input. Thanks
Hellnar
36
votes
3 answers

Joda DateTime to Unix DateTime

It's odd enough, but I didn't find any result about converting Joda(Time) DateTime to Unix DateTime (or timestamp, whichever is the correct name). How can I do this?
Incerteza
  • 32,326
  • 47
  • 154
  • 261
35
votes
9 answers

UNIX_TIMESTAMP in SQL Server

I need to create a function in SQL Server 2008 that will mimic mysql's UNIX_TIMESTAMP().
TheZver
  • 1,502
  • 2
  • 14
  • 19
35
votes
3 answers

Moment.js: Date.now() javaScript analogue

Is there any simple way to get a number of milliseconds elapsed since 1 January 1970 00:00:00 UTC simular the Date.now() javaScript function?
Roman
  • 2,145
  • 4
  • 26
  • 33
34
votes
3 answers

Why do I need to multiply unix timestamps by 1000 in JavaScript?

I'm sure there's a reason I have to add three zeros to every Unix timestamp in JavaScript in order to get the correct date. Can you tell me why? Is it as simple as milliseconds since the epoch vs. seconds?
buley
  • 28,032
  • 17
  • 85
  • 106