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

How to convert Date and time into epoch in google sheet

I have a google sheet where i have a column date and time in the following format 14-08-2021 12:30:00 I want a result in Epoch which is in this format 1591259160 i tried this code in script editor but i am not getting result function…
0
votes
1 answer

How to convert epoch timestamp to unix hex timestamp in kotlin?

I am using the following code to get the current epoch time in kotlin. val epochTime = Instant.now().toEpochMilli() Output: 1628769521313 And now the question is how can I convert this timestamp to Unix hex timestamp…
AndroidDev
  • 888
  • 3
  • 13
  • 27
0
votes
1 answer

interpreting unix time in the context of hours

I am new to Javascript development and I recently came across the use case of UNIX time. I was wondering how I could use it for my purpose. My desired outcome is simple: get unix timestamp values from database, compare them to the current timestamp,…
Juliette
  • 4,309
  • 2
  • 14
  • 31
0
votes
0 answers

Month Format in Unix

I am trying to create a shell script for auditing purpose. In my source data the value of month contains like yyyym(20217) format. I need to extract the records based on this month type. is there any dateformat to print like this in Unix?
AVJ
  • 35
  • 4
0
votes
2 answers

Java: Unix Timestamp at midnight

I'm trying to retrieve the Unix timestamp for midnight of the current day. Timezone not relevant. I'm looking for something like: 1625716800 Every tutorial I've found is for retrieving formatted strings, such as: "Thu Jul 08 2021 04:00:00" It needs…
solitario
  • 15
  • 1
  • 3
0
votes
1 answer

hive datetime formats

I need to convert 2019-07-12 22:30:00.000 to 7/12/2019. I have used from_timestamp(dintervalstart,'M/d/yyyy') where dintervalstart is the col holding these values in impala and got succeeded. Whereas in hive I am getting the output if I use…
nvsk. avinash
  • 581
  • 1
  • 5
  • 10
0
votes
1 answer

R - Convert Unix to time AND date columns

I have a dataframe which has a column (expires) with unix time codes. I want to convert these to TWO separate columns, one for time (expires_time) and one for date (expires_date). I've started with a simple piece of code from this post: Convert UNIX…
Japes
  • 209
  • 1
  • 10
0
votes
1 answer

Is it safe to convert ISO datetime with strtotime

For example strtotime("2018-12-06T09:04:55"); strtotime("2021-07-09T14:09:47.529751-04:00"); I read in the php manual that ISO dates should be avoided when using strtotime, why ? Should I extract date time from the string before using…
0
votes
2 answers

Ordering retrieved information from a database

I'm creating a feed by retrieving information from my database using nested while loops (is there a better way to do this?). I have one table called users with all the names amongst other things. The other table is called messages which has…
Sebastian
  • 3,548
  • 18
  • 60
  • 95
0
votes
0 answers

Column date in multiple formats like "mm-dd-yyy", "yyy-mm-dd", with time zone, in unix and in unix with nano seconds how to format all in same format?

I am currently using the below code to convert the format from two formats and it take lots of time, looking for some library that can identify the date without explicitly mentioning the formats. For eg I have values like " 2018-05-19…
0
votes
0 answers

in a Pandas Dataframe Column, how to convert two datetime formats to one Datetime format

I have a pandas DataFrame with a datetime column containing two different formats. One format is “2013-03-07 05:29:47.890” Second format is unix-timestamp “1527051855673000000” How do i convert these two datetimes into one datetime format without…
Ali
  • 13
  • 4
0
votes
0 answers

Comparing unix timestamp dont return the expected result - JAVASCRIPT

I have a unix timestamp property in my database propertyName is closeDeal. closeDeal value in database - 1624699800 (unix timestamp). What I'm required to do is have two validations on the above timestamps the closeDeal timestamp is greater than…
The JOKER
  • 453
  • 8
  • 21
0
votes
1 answer

How to convert Unix timestamp to local date in Oracle?

Internet, Please help! I am using Oracle DB v 12c, where invoice issuing time is stored as unix timestamp in column of type NUMBER + there is also a VARCHAR2(128) column, which defines the timezone where the invoice was issued, eg…
Jansen
  • 152
  • 1
  • 8
0
votes
2 answers

Display date/time in readable and brief format

I stored my dates and times in unix timestamp format (d1=1387721302, d2=1311343703) and would like to view date differences(past, present and future) in say. 2 Weeks ago 15 Days ago 2 Minutes ago 3 Months ago 1 Year ago 9 Months From Now 4 Days From…
Frank Nwoko
  • 3,816
  • 5
  • 24
  • 33
0
votes
1 answer

TimescaleDB and Loopback4 Timestamp data type compatibility

I am currently using TimescaleDB for one of my projects and I am running into some issues when using the built in TIMESTAMP data field with TIMESCALE. I have been pulling my hair out over this for weeks. When getting data via the loopback4 api, the…