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

Formatting date to human readable format

Lets say in my mysql database I have a timestamp 2013-09-30 01:16:06 and lets say this variable is $ts. How can I output this to show more like September 30th, 2013?
soniccool
  • 5,790
  • 22
  • 60
  • 98
32
votes
11 answers

How to round unix timestamp up and down to nearest half hour?

Ok so I am working on a calendar application within my CRM system and I need to find the upper and lower bounds of the half an hour surrorunding the timestamp at which somebody entered an event in the calendar in order to run some SQL on the DB to…
Ash
  • 397
  • 1
  • 3
  • 10
32
votes
3 answers

How to convert a 13 digit Unix Timestamp to Date and time?

I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes: echo date('Y-m-d h:i:s',$item->timestamp); doesnt work for me and also this $unix_time = date('Ymdhis',…
30
votes
1 answer

How to Insert custom date into mysql timestamp field?

I have tried to insert date and time string formatted into mysql timestamp field by using following two methods but both shows me 0000-00-00 00:00:00 INSERT INTO test VALUES ( UNIX_TIMESTAMP('2013-08-05 18:19:03') ) INSERT INTO test VALUES (…
Maximus
  • 2,906
  • 4
  • 35
  • 55
29
votes
5 answers

PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa

im using php 5.4.6 and MySQL 5.5.29 and I get trouble by converting a UNIX TIMESTAMP to MYSQL DATETIME and vice versa. Mysql and php wun on the same local machine. I have a simple mysql table CREATE TABLE Entry( id SERIAL PRIMARY KEY, created…
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
28
votes
1 answer

How to convert Unix timestamp into Swift NSDate object?

I'm getting dates as Unix timestamps through my API and I want to convert them into NSDate objects in Swift. How can I do that?
dead_man
  • 409
  • 1
  • 6
  • 10
28
votes
3 answers

python get time stamp on file in mm/dd/yyyy format

I'm trying to get the datestamp on the file in mm/dd/yyyy format time.ctime(os.path.getmtime(file)) gives me detailed time stamp Fri Jun 07 16:54:31 2013 How can I display the output as 06/07/2013
Ank
  • 6,040
  • 22
  • 67
  • 100
27
votes
6 answers

unix timestamp round to midnight

If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight. For example if the timestamp is…
user962449
  • 3,743
  • 9
  • 38
  • 53
27
votes
10 answers

How do you get the unix timestamp for the start of today in javascript?

I realize that the current timestamp can be generated with the following... var timestamp = Math.round((new Date()).getTime() / 1000); What I'd like is the timestamp at the beginning of the current day. For example the current timestamp is roughly…
Anthony Jack
  • 5,333
  • 7
  • 28
  • 47
27
votes
1 answer

Dart - Converting Milliseconds Since Epoch (UNIX timestamp) into human readable time

Is there a good way to parse milliseconds since epoch (ex. 1486252500000 13 digits) formatted time into a human readable format?
Arthur Daniel
  • 331
  • 1
  • 3
  • 7
27
votes
5 answers

Converting TIMESTAMP to unix time in PHP?

Currently I store the time in my database like so: 2010-05-17 19:13:37 However, I need to compare two times, and I feel it would be easier to do if it were a unix timestamp such as 1274119041. (These two times are different) So how could I convert…
Rob
  • 7,980
  • 30
  • 75
  • 115
26
votes
6 answers

Can unix_timestamp() return unix time in milliseconds in Apache Spark?

I'm trying to get the unix time from a timestamp field in milliseconds (13 digits) but currently it returns in seconds (10 digits). scala> var df = Seq("2017-01-18 11:00:00.000", "2017-01-18 11:00:00.123", "2017-01-18 11:00:00.882", "2017-01-18…
van_d39
  • 725
  • 2
  • 14
  • 28
26
votes
1 answer

Timestamps: iso8601 vs unix timestamp

I know this is a pretty common question, but I don't feel like the answers I found really solve the problem. I will outline my specific use case and present summaries to the information from other SO answers and around the web. For the service I'm…
qingu
  • 2,101
  • 2
  • 16
  • 20
26
votes
7 answers

Best practice for storing the date in MySQL from PHP

I've been using the unix timestamp all my life. I like it because it's easy to compare, it's fast because I store it as an integer. And since I'm using PHP, I can get any date/time format with date() function from the unixtimestamp. Now, some people…
treznik
  • 7,955
  • 13
  • 47
  • 59
25
votes
6 answers

How to get Unix timestamp in php based on timezone

Code first echo time() . '
'; echo date('Y-m-d H:i:s') . '
'; date_default_timezone_set('America/New_York'); echo time() . '
'; print_r($timezones[$timezone] . '
'); echo date('Y-m-d H:i:s') . '
'; In the above code the…
jimy
  • 4,848
  • 3
  • 35
  • 52