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
0
votes
0 answers

Generate python UNIX timestamp

I am working with the kraken and binance APIs. The binance API returns a time like this: 1612722603026 inside of the API response. The kraken API does not return a time for the API response so I am trying to generate a timestamp to match the one…
Alfie Danger
  • 377
  • 5
  • 23
0
votes
1 answer

Convert String epoch code from unix timestamp to date using sql

I need your help in converting this event_time to an ASCII SQL formated datatype. A little background: This data was retrieved from a Hadoop Hive data source using Tableau. Also, I was told it's a unix timestamp. I have some extensive research done…
0
votes
0 answers

Groovy: Export Data - How to set unix start time from now to a certain point in the past?

I am currently exporting data from Grafana. I do that using a script which looks like this one: JsonSlurper slurper = new JsonSlurper() println "Collect list of servers:" def result = slurper.parse(new URL('http://xyz')) def servers =…
Tobitor
  • 1,388
  • 1
  • 23
  • 58
0
votes
2 answers

How to manually modify Unix TimeStamp so that its 5 hours behind UTC?

How can I modify a raw Unix timestamp so that it shows that it is 5 hours behind (as an example). I'm looking to do this with a javascript or python. The more lightweight the better. I'm basically looking for how to manually decode a given unix…
Jesse G
  • 488
  • 6
  • 13
0
votes
0 answers

Comparing mysql result to current time in PHP

$currentTime = date("h:i a"); Im storing time in my db like this... $timeEnd = date("h:i a"); which outputs "07:15 pm". I need to compare $expireTime to $currentTime. $sql2 = "UPDATE id SET expired = '1' WHERE timeEnd <= '".$currentTime."' AND date…
0
votes
1 answer

Dart/Flutter - How to get second since epoch : Twitter API error code 135 ( Timestamp out of bounds)

Hello I hope you are well. Recently, I have a problem using the Twitter API precisely at the time of publishing a tweet. According to the documentation here, my request must contain in its header a field oauth_timestamp which is the number of…
Steeven Delucis
  • 293
  • 2
  • 14
0
votes
0 answers

converting unixtime to date in reactjs not working properly

I am trying the below code , but my Date is not printing properly. I am getting some different number for year. const date= = new Date(1607056200000 * 1000); let convDate = date.toLocaleDateString("en-US"); console.log("Date is " + convDate +…
Nida_100
  • 1
  • 1
0
votes
2 answers

Mixing operator with unix_timestamp yields unexpected null

My table has some TIMESTAMP columns, for which I am running a query to get the most recent one as a Unix timestamp. Suppose all the columns are zeroed (they are default '0'), i.e. I didn't set the value yet. If I select each column with…
ranieri
  • 2,030
  • 2
  • 21
  • 39
0
votes
1 answer

Convert Unix timestamp in human readable

I have below script size=$1 big_files_list=$(find . -name \*.bytes -type f -printf "%b %h/%f\n" | awk -v size="$size" ' function basename(file) { …
Samurai
  • 121
  • 1
  • 4
  • 15
0
votes
2 answers

Invalid date when using a unix timestamp with letters in it in JavaScript

This is the timestamp I have: 5fb6995 When I do new Date('5fb6995') Invalid Date gets returned. But when I try converting it online in an online converter, everything works. Why doesn't this work and how can I make it work?
Filip
  • 855
  • 2
  • 18
  • 38
0
votes
1 answer

Unix timestamp is not correct in JavaScript, but is correct according to other sources

This timestamp 1604978063 is not correct according to new Date(1604978063) It returns the following: Date Mon Jan 19 1970 05:49:38 GMT-0800 (Pacific Standard Time) When I look it up on DuckDuckGo, it looks correct. What's happening here?
Chen
  • 107
  • 1
  • 12
0
votes
1 answer

How to convert unix timestamp in ms to readable timestamp in a pandas array?

I have a pandas array with a column which contains unix timestamp times, but I think it's in milliseconds because each time as 3 extra 0's at the end. For example, the first data point is 1546300800000, when it should be just 1546300800. I need to…
mbohde
  • 109
  • 1
  • 7
0
votes
0 answers

Wrong timestamps in 1890-09-30? 22:43:59 is followed by 22:43:40 instead of 22:44:00

Does anybody know why following happens with the timestamp integers using JavaScript Date? If I use integer -2500938981 to transpose to YYYY-MM-DD hh:mm:ss, I get 1890-09-30 22:43:59. Then for -2500938980 I would expect a timestamp 1890-09-30…
0
votes
1 answer

Unix file Sort rows on the basis of timestamp column

Unix file Sort rows on the basis of timestamp column A 00:00:01 K 01:02:00 C 01:02:03 N 01:03:02 Excepted output should be sort basis on last column HH:MM:SS N 01:03:02 C 01:02:03 K 01:02:00 A 00:00:01 I tried command sort -k 4 -t " " -n -r txt.…
0
votes
2 answers

Find difference between 2 unix timestamps in php

I am trying to find the time difference between 2 timestamps. Timestamp "startTime" have 1601096400 Timestamp "expireTime" have 1601094600 I want to output the difference between both time like Expire in : 4hr 30min How do I do it in php ?