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
2 answers

Converted unix timestamp with a timezone addition in seconds gives a true local date time on Android emulator but not in real device?

I took a date from a web service in UNIX timestamp. I milltuplied it by 1000L then I added the timezone to it in seconds (also provided by the web service) milltiplied by 1000 to obtain the date according to the country in which the application will…
Delgado
  • 216
  • 3
  • 16
0
votes
1 answer

Converting UNIX time from Nanoseconds to Seconds

I am reading in time stamps as strings from a service that is UNIX time formatted in Nano seconds. This introduces an obvious problem in that I can't conduct standard operations to normalize the strings to seconds given how large they are. An…
StormsEdge
  • 854
  • 2
  • 10
  • 35
0
votes
2 answers

Convert timestamp coming from SQL database to String

I am saving the timestamp in SQL as bigint(20). The number is correct and in android or https://www.epochconverter.com it works fine. However I am not able to create a date-string based on the timestamp received from database. First of all, the…
Gerke
  • 926
  • 1
  • 10
  • 20
0
votes
1 answer

Converting a UNIXEPOCH timestamp stored in a database using SQLite3 in a bash script

I have an sqlite3 query SELECT DATETIME(ROUND(start_time / 1000), 'unixepoch') as Date FROM history When I open the database in dbbrowser,( https://sqlitebrowser.org/ )the query runs fine. I now try to run the command in a bash script. echo 'SELECT…
SqLITE
  • 35
  • 5
0
votes
0 answers

Add Hours to Timestamp in Python

How to add hours to a timestamp in python? I have a timestamp in UTC, and I want to add hours to it. If I convert it to DateTime, it changes to local time. So, I want to add hours directly to the timestamp without converting to datetime. import…
0
votes
1 answer

Why do a timezone-unaware and a timezone-aware datetime object with a different timezone yield the same unix-timestamp?

So, as far as I understand if you convert without a timezone from or to a unix-timestamp you will always get GMT/UTC, like this: import datetime import pytz datetime.datetime(2020,4,1,0,0,0).timestamp() The resulting timestamp is 1585692000. Now if…
Khris
  • 3,132
  • 3
  • 34
  • 54
0
votes
0 answers

Inserting received unix timestamp as right format in MSSQL Server with node.js

I want to import the unix timestamp (contained in an received Json body) into my MSSQL database as a human readable time using Node.js. The Json body looks like this: { "device":"887B53", "data":"4660000000000062b4a8", "station":"1B2C", …
Karol-Kahn
  • 31
  • 1
  • 5
0
votes
2 answers

Convert unix_timestamp column to string using pyspark

I have a column which represents unix_timestamp and want to convert it into string with this format, 'yyyy-MM-dd HH:mm:ss.SSS'. unix_timestamp | time_string 1578569683753 | 2020-01-09 11:34:43.753 1578569581793 | 2020-01-09…
0
votes
1 answer

Decoding a unix timestamp

First of all this is my first time posting here so I'm really excited! I recently got a task to decode a timestamp from one of our legacy systems, and I'm not sure what its encoding. From the binary data, it is printed by python as combination of…
0
votes
0 answers

error when change unix time into date-time via excel

i want to convert a timestamp column into time. Imagine that this column is extended in 30000 rows so when i put this formula =ROUND(DATEVALUE("1/1/1970")+A1/(60*60*24),0) i get this: VALUE! Why does this happen I am using Microsoft office…
redplanet
  • 35
  • 6
0
votes
1 answer

Upload dates in unix timestamp format . Mysql DB

I have data with date format 1577234966837. I uploaded this data in table via command : load data infile 'C:/file.tsv' into table table_1 fields terminated by '\t' lines terminated by'\n' ignore 1 lines (value, @timestamp) set timestamp =…
Riot
  • 15
  • 3
0
votes
1 answer

Mysql date condition shows incorrect results

SELECT * FROM memories WHERE date > unix_timestamp(DATE(NOW()) - INTERVAL 1 DAY) GROUP BY user_id ORDER BY date DESC I use this query to get memories from 1 day In the date column, it is all stored as UNIX timestamp, so I use 1583212980…
Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130
0
votes
1 answer

Crontab showing different time and Shell script when run manually shows different time stamp

#!/bin/sh string1=$(date +"%T") string2=$(date -r merchant.xml +"%T") StartDate=$(date -d "$string1" +"%s") FinalDate=$(date -d "$string2" +"%s") echo Since, $(date -d "0 $StartDate sec - $FinalDate sec" +"%H:%M") HOURS, mail has not been updated…
0
votes
1 answer

Get the average of values in every specific epoch ranges in unix timestamp which returns -1 in specific condition in MySQL

I have a MySQL table which has some records as follows: unix_timestamp value 1001 2 1003 3 1012 1 1025 5 1040 0 1101 …
0
votes
1 answer

Unix time but with leap seconds

Unix time is useful for measuring time, whereas other formats are more useful for telling the time. This is because (apart from time synchronization), it just ticks forward one second at a time. It doesn't change when our clock for telling the time…
David Callanan
  • 5,601
  • 7
  • 63
  • 105