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

MongoDb timestamp

i have created and want to now import a dummy collection. one of the fields in each item are "created" and "updated" fields. what can i put in the source/json file so that MongoDb will use the current date and time as the value on import? this wont…
Simba
  • 522
  • 1
  • 5
  • 9
9
votes
1 answer

jQuery UI - Convert jQuery UI Datepicker to UNIX time

I'm using jQuery UI datepicker with time plugin. This is how I display time on the page in datepicker itself: 21.06.2012 08:00 I'd like to convert this time to UNIX timestamp before sending to backend. How can I do this? I don't need to dispay UNIX…
f1nn
  • 6,989
  • 24
  • 69
  • 92
8
votes
2 answers

AS3 Timestamp isn't correct

I'm trying to get the current unix timestamp in AS3 via: var date:Date = new Date(); var unix:int = date.time; trace(unix); For some reason I get: 2775219874 But when I use time() in PHP around the same time, I get: 1321330282 I don't…
Marty
  • 39,033
  • 19
  • 93
  • 162
8
votes
3 answers

What do you call the number of *days* since the unix epoch?

I initially learned that Unix time is the number of seconds that have elapsed since 00:00:00 (UTC) on 1 January 1970. With 24 hours in a day, that means that the unix timestamp grows by 86400 every day. Then I heard about the concept of leap…
Daniel Howard
  • 4,330
  • 3
  • 30
  • 23
8
votes
2 answers

Ordering WP Posts by Custom Meta Key

I created a WordPress custom post type to be able to create events, select the event's date, and display the date on the frontend. I added a new meta_key in the postmeta of WP's database to store the event's date in a UNIX timestamp. I've had no…
cqde
  • 695
  • 5
  • 13
  • 22
8
votes
3 answers

php - getting date time using DateTime return different value from date() function

I am trying to get the current timestamp using Carbon or DateTime Class I get wrong date but when I use date() function it return the correct date I run the code on win server 2012 this is my code dd([ 'Carbon::now()->format("Y-m-d H:i:s P")' =>…
8
votes
1 answer

Convert Unix timestamp to timestamp without time zone

How do I convert a Unix timestamp (or epoch time) to a PostgreSQL timestamp without time zone? For example, 1481294792 should convert to 2016-12-09 14:46:32. I tried SELECT to_timestamp(1481294792)::timestamp, but that gives me 2016-12-09 09:46:32.
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
8
votes
8 answers

how convert unix timestamp to datetime

I'm trying to convert this unix timestamp 1415115303410 in DateTime, in this way: private static DateTime UnixTimeStampToDateTime(long unixTimeStamp) { System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0,…
DevT
  • 1,411
  • 3
  • 16
  • 32
8
votes
4 answers

DateTime to UnixTime Stamp in .net

Is there any function in vb dot net to convert datetime to unix time stamp If I google I get only the vice versa but not vb.net to unix time stamp Any help is appreciated
Ramji
  • 2,536
  • 7
  • 34
  • 54
8
votes
2 answers

Overhead of time command in unix

i was wondering what is the overhead of using the time command in unix. i know how to use it, but i want to know how much longer the command $ time java HelloWorld takes on a terminal, than the command $ java HelloWorld I am specifically…
vijay
  • 2,646
  • 2
  • 23
  • 37
8
votes
1 answer

When was Unix epoch time revised from 1971 to current 1970 value?

According to the first edition Unix Programmer's Manual, Unix time is defined as "the time since 00:00:00, January 1, 1971, measured in sixtieths of a second" When did this change to its current value of midnight (UTC), January 1, 1970?
Andrew
  • 121
  • 6
7
votes
3 answers

Minimal implementation of gmtime algorithm?

Does anyone know of a simple gmtime or ctime implementation without consideration for timezone, no external dependencies, and a non-copyleft license (BSD/MIT/anything proprietary-safe)? Preferably in C, but basically anything that gives me the…
Brian McFarland
  • 9,052
  • 6
  • 38
  • 56
7
votes
2 answers

Unix script to compare a timestamp with current time

This is my first journey into the realm of Unix scripting and I'm not sure how to go about this. Ill be querying a DB and pulling out a timestamp. What I need to do is take that timestamp (in the awesome format of YYYYMMDDHHMMSS) and if its more…
Hershizer33
  • 1,206
  • 2
  • 23
  • 46
7
votes
3 answers

Get current timestamp from specific timezone

is there an easy way to get the unix timestamp in javascript from a specific timezone? for example i want the client send me an unix timestamp but i want to get it to match my timezone. thanks!
Toby
  • 2,720
  • 5
  • 29
  • 46
7
votes
1 answer

Mysql Unix-Timestamp Date Format

In mysql database I store date column as unix-timestamp format (for example:1264105904). I want to convert this date to datetime like "Oct 11, 2011 6:25 am PDT" in select statement. How can I achieve this?
Umut Derbentoğlu
  • 1,146
  • 5
  • 18
  • 39