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

How to convert strings like "19-FEB-12" to epoch date in UNIX

In UNIX how to convert to epoch milliseconds date strings like: 19-FEB-12 16-FEB-12 05-AUG-09 I need this to compare these dates with the current time on the server.
hellish
  • 411
  • 2
  • 6
  • 11
24
votes
1 answer

Behind The Scenes: Core Data dates stored with 31 year offset?

I know, "no user-serviceable parts inside" ... but I'm curious: In a Core Data sqlite3 DB, it seems I can get at the date within a ZDATE like so: sqlite> select datetime(ZDATE,'unixepoch','31 years','localtime') from ZMYCLASS; 2003-12-11…
Joe D'Andrea
  • 5,141
  • 6
  • 49
  • 67
23
votes
4 answers

Convert date to unix timestamp in postgresql

I have a table with a column abc carrying the unix timestamp (eg. 13898161481435) and I want to run a between dates select. It would be not efficient to do a where TO_CHAR(TO_TIMESTAMP(abc / 1000), 'DD/MM/YYYY') > '14/01/2014 00:00:00' and…
javadude
  • 1,763
  • 1
  • 19
  • 38
22
votes
6 answers

How can I convert a Unix timestamp back to time?

I have the following Unix timestamps. 1301982430 1301982430 1301981474 1301981466 1301981466 1301981066 1301981058 1301981058 1301980388 1301980373 1301980373 1301979082 1301978478 1301978478 1301978478 How do I convert it back to time that's…
esafwan
  • 17,311
  • 33
  • 107
  • 166
22
votes
3 answers

Convert Datetime to Unix timestamp

In Microsoft SQL Server 2012 or above, is it possible to convert a datetime value to Unix time stamp in a single select statement? If so, how can it be done?
O A
  • 359
  • 1
  • 2
  • 7
22
votes
6 answers

Giving a file/directory the same modification date as another

How do I "copy" the modification date and time from one file/dir to another in Unix-based systems?
user2015453
  • 4,844
  • 5
  • 25
  • 27
21
votes
3 answers

Unix timestamp vs datetime

Possible Duplicate: Datetime vs Timestamp? I have a Mysql table, which has a column add_date. It tracks the date/time when the record was added to the database. Queries based on this table: Display when the record was added in the format: 2 hours…
Yeti
  • 5,628
  • 9
  • 45
  • 71
21
votes
3 answers

How to write bigint (timestamp in milliseconds) value as timestamp in postgresql

I'm trying to store in timestamp with timezone field my value. It is in milliseconds from 1970. select TO_CHAR(TO_TIMESTAMP(1401432881230), 'DD/MM/YYYY HH24:MI:SS.MS') Expected 30/5/2014 11:29:42 10:54:41.230, but get 22/08/46379 23:27:02.000
Clyde
  • 991
  • 2
  • 9
  • 17
21
votes
9 answers

Parsing unix time in C#

Is there a way to quickly / easily parse Unix time in C# ? I'm brand new at the language, so if this is a painfully obvious question, I apologize. IE I have a string in the format [seconds since Epoch].[milliseconds]. Is there an equivalent to…
Alex Marshall
  • 10,162
  • 15
  • 72
  • 117
21
votes
3 answers

How do I get a unix timestamp from PHP date time?

I'm trying to get a unix timestamp with PHP but it doesn't seem to be working. Here is the format I'm trying to convert to a unix timestamp: PHP $datetime = '2012-07-25 14:35:08'; $unix_time = date('Ymdhis', strtotime($datetime )); echo…
user1216398
  • 1,890
  • 13
  • 32
  • 40
21
votes
2 answers

How can I display a formatted date from a Unix timestamp in twig?

I would like to display a formatted date in twig by applying a filter to a Unix timestamp. Is such a feature available in twig?
Bogdan
  • 913
  • 2
  • 12
  • 29
20
votes
1 answer

Parse unix timestamp with decimal from float using time.Unix()?

This is pretty straightforward, but I couldn't find a response and figured that others might have the same question. I have a Unix timestamp as a float, that includes a decimal value for fractions of seconds. What's the conversion factor to pass…
Ben Guild
  • 4,881
  • 7
  • 34
  • 60
20
votes
6 answers

How do I find the unix timestamp for the start of the next day in php?

I have a unix timestamp for the current time. I want to get the unix timestamp for the start of the next day. $current_timestamp = time(); $allowable_start_date = strtotime('+1 day', $current_timestamp); As I am doing it now, I am simply adding 1…
zeckdude
  • 15,877
  • 43
  • 139
  • 187
19
votes
4 answers

Hive from_unixtime for milliseconds

We have a timestamp epoch column (BIGINT) stored in Hive. We want to get Date 'yyyy-MM-dd' for this epoch. Problem is my epoch is in milliseconds e.g. 1409535303522. So select timestamp, from_unixtime(timestamp,'yyyy-MM-dd') gives wrong results for…
Sourabh Potnis
  • 1,431
  • 1
  • 17
  • 26
19
votes
3 answers

Convert unix timestamp to javascript date Object

Am working with json api that represents dates like this "date" : "/Date(1356081900000)/" I want to turn this into regular javascript Date. The only way I can think of solving this problem is to do a replace on everything leaving the timestamp…
jamjam
  • 3,171
  • 7
  • 34
  • 39