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
10
votes
6 answers

How can we get weekday based on given date in unix

From a given date in %m-%d-%Y format we should determine what day it is. Example: for the date 09-01-2017 output should be Friday
Divya S
  • 141
  • 1
  • 2
  • 5
10
votes
2 answers

Using gson dateformat to serialize and deserialize unix-timestamps

I am using Gson to serialize/deserialize my pojos and currently looking for a clean way to tell Gson to parse/output date attributes as unix-timestamps. Here's my attempt: Gson gson = new GsonBuilder().setDateFormat("U").create(); Comming from PHP…
10
votes
1 answer

Convert Joda LocalDateTime to Unix timestamp

How do I convert a org.joda.time.LocalDateTime to an Unix timestamp, given that local time is in UTC timezone? Example: new LocalDateTime(2015, 10, 02, 11, 31, 40) > 1443785500.
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
10
votes
2 answers

Timestamp in ISO 8601 - the last 6 digits yyyy-MM-dd'T'HH:mm:ss.?

I have timestamps looking like this: 2015-03-21T11:08:14.859831 2015-03-21T11:07:22.956087 I read a Wiki article on ISO 8601, but did not get the meaning of the last 6 digits here. I tried getting it down to milliseconds using…
mel
  • 1,566
  • 5
  • 17
  • 29
10
votes
3 answers

How to convert Epoch time to date?

Hi I have a column with number datatype the data like 1310112000 this is a date, but I don't know how to make it in an understandable format: ex: 10-mar-2013 12:00:00 pm Can any one please help me.
pradeep kumar
  • 107
  • 1
  • 1
  • 7
10
votes
2 answers

How to convert Unix TimeStamp into day:month:date:year format in C?

How do we convert a Unix time stamp in C to day:month:date:year? For eg. if my unix time stamp is 1230728833(int), how we convert this value into this-> Thu Aug 21 2008? Thanks,
user2430771
  • 1,326
  • 4
  • 17
  • 33
10
votes
2 answers

Convert a unix timestamp to a human readable date in PHP?

I have a timestamp value from PHP: 1188604800000 When I format the time to human readable like this: date("m/d/Y", 1188604800000) It prints: 05/21/39635 If I put the number into an online Unix Timestamp converter I get: Sat, 01 Sep 2007 00:00:00…
Joseph U.
  • 4,457
  • 10
  • 41
  • 47
9
votes
1 answer

How to convert a fractional epoch timestamp (double) to an std::chrono::time_point?

I have a fractional epoch timestamp, represented as double, that I would like to convert to an appropriate std::chrono::time_point. The epoch is the usual UNIX epoch since 1/1/1970. I know that there exists std::chrono::system_clock::from_time_t,…
mavam
  • 12,242
  • 10
  • 53
  • 87
9
votes
4 answers

How to convert UNIX time before 1970 to date format in MySQL?

I have a database using unix time for its dates ( i am using mySQL). I want to retrieve the dates in daily date format. This is my query: SELECT FROM_UNIXTIME(time_created) FROM member This works fine with dates after 1970 (for example, 1314162229)…
why.you.and.i
  • 497
  • 8
  • 20
9
votes
1 answer

How to normalize unix timestamps to sum to discrete numeric values?

I have an "ideal" formula in which I have a sum of values like score[i] = SUM(properties[i]) * frequency[i] + recency[i] with properties being a vector of values, frequency and recency scalar values, taken from a given dataset of N items. While…
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
9
votes
5 answers

How to convert date in format "YYYY-MM-DD hh:mm:ss" to UNIX timestamp

How can I convert a time in the format "YYYY-MM-DD hh:mm:ss" (e.g. "2011-07-15 13:18:52") to a UNIX timestamp? I tried this piece of Javascript code: date = new Date("2011-07-15").getTime() / 1000 alert(date) And it works, but it results in NaN…
Amal Kumar S
  • 15,555
  • 19
  • 56
  • 88
9
votes
2 answers

How do I convert timestamp to unix format with pyspark

I have a dataframe with timestamp values, like this one: 2018-02-15T11:39:13.000Z I want to have it in UNIX format, using Pyspark. I tried something like data = datasample.withColumn('timestamp_cast', datasample['timestamp'].cast('date')) but I lose…
Ticoincoin
  • 157
  • 1
  • 2
  • 15
9
votes
1 answer

Commandline to convert ISO8601 time to unix timestamp

What is the most efficient way to get a unix timestamp for a given ISO 8601 date and vice versa? There are several third party websites to do this. However I am looking for a simple command that can be executed at the Linux prompt
Amol
  • 1,084
  • 10
  • 20
9
votes
1 answer

Cookies expiration time format

I created DB from google chrome's Cookies file and one of the columns in the cookies table is expires_utc. The format of the column is like - 13169399213145330, which is more like unix time. But when i'm trying to use unix time converter, it gives…
Src
  • 5,252
  • 5
  • 28
  • 56
9
votes
4 answers

Python: How to convert unixtimestamp and timezone into datetime object?

I have a csv file with the datetime in unixtimestamp format with milliseconds and timezone information in milliseconds as well. I want to convert this into a more usable datetime format for further processing. For example, the time is 1437323953822…
sfactor
  • 12,592
  • 32
  • 102
  • 152