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
1 answer

DynamoDB UpdateItem failed

I am using DynamoDB Go SDK for CRUD operations. I verified PutItem and GetItem calls are working fine. However, when I use UpdateItem which updates some attributes, it fails. I narrowed it down to specific to an attribute that stores the current…
Korba
  • 435
  • 1
  • 4
  • 18
0
votes
0 answers

How to convert year,month,day,time to UNIX time in Visual studio c++

I have searched all over Google without success for a way to convert my list of year,month,day and time to unix time. I need a simple way to find out if one time is smaller or bigger than another day. I have found this solution. I have this…
User1953
  • 171
  • 1
  • 7
0
votes
2 answers

Oracle - Convert Unixtime to local datetime including DST (Daylight Savings Time) and vice versa

I have a NUMBER field (UTCSTAMP) with UnixTimestamps and I would like to have a (custom) function to easily return local datetime (Europe/Amsterdam) including DST (Daylight Savings Time). The output should be this format: 'yyyy-mm-dd hh24:mi:ss' For…
Matt
  • 179
  • 4
  • 11
0
votes
0 answers

How to Extract Unix Timestamp of every frame in video and save to csv file in Python?

I have video file and want to create one csv file which content every frame unix timestamp of video as every row of csv file?? I am using cv2 and moviepy package how can I get it. import moviepy.editor as mpy vid =…
0
votes
1 answer

javascript convert date in the future to timestamp

I want to get the timestamp of one-year from now: var oneYr = new Date(); oneYr.setYear((new Date()).getYear() + 1); When I try to get the timestamp: oneYr.getTime() /1000 I get -58351759111000 (not correct) It's only working for date in the…
cheziHoyzer
  • 4,803
  • 12
  • 54
  • 81
0
votes
1 answer

Modify a timestamp to another timestamp

I have a unix timestamp as follows: 1561420800 which results in Monday, June 24, 2019 8:00:00 PM. What I want is to use the timestamp so i can recreate a new timestamp and obtain the month and year and then results in Monday, June 24, 2019 00:00:00…
somejkuser
  • 8,856
  • 20
  • 64
  • 130
0
votes
2 answers

Subtract 2 minutes from a unix timestamp

Could someone assist me on a timestamp issue.. how can I subtract 2 minutes from this timestamp? echo 'Settings from database (octopus_import_employees):'; $settings = get_settings('octopus_import_employees'); var_dump($settings); …
Sema
  • 95
  • 9
0
votes
1 answer

Converting Unix timestamp in Google App scripts

I am new to Google App Script and am currently working on a project to help myself get familiar with it. My project has a part where I have to convert Unix Timestamp objects in nested JSON to human-readable time. As I don't know about converting…
user12297498
  • 5
  • 1
  • 4
0
votes
2 answers

Using standart sql, how to convert timestamp to unix time?

In mysql, one can do this in order to compare 2 timestamp : where UNIX_TIMESTAMP(transaction.closed_on)
user2284570
  • 2,891
  • 3
  • 26
  • 74
0
votes
1 answer

Sunrise and sunset time are equal after converting

I am trying to convert the millisecond format of time provided by openweathermap but when I convert them the times are just 1minute apart. I have tried converting using Simpledateformat. fun formatTIme(sun:Date):String{ val…
0
votes
1 answer

Convert Oracle DATE to Unix-style time (seconds since 1906?)

I need to convert an Oracle DATE value to a Unix style seconds-since-epoch-start value. I've tried various combinations of Oracle's conversions such as: select to_number(to_date('10/05/2019','mm/dd/yyyy')) from dual; select…
user1071914
  • 3,295
  • 11
  • 50
  • 76
0
votes
3 answers

How do I expire rows based on a lookup table of expiry times?

If I have two tables: items Id VARCHAR(26) CreateAt bigint(20) Type VARCHAR(26) expiry Id VARCHAR(26) Expiry bigint(20) The items table contains when the item was created, and what type it is. Then another table, expiry, is a…
GCHQ77703
  • 23
  • 5
0
votes
2 answers

matching dates-birthdays

I am doing a mysql query and trying to get birthdays that match the current day and month. Here is what I was trying but realized that it wont work properly: $birthdays = $wpdb->get_results(" SELECT * FROM {$wpdb->usermeta} WHERE meta_key =…
0
votes
0 answers

Convert unix time to Date in JFreechart?

Using the JFreechart library: I have a two dimensional double array and I want to display all timestamps (unix time) as a Date format (like dd/MM/yyyy). I can generate the chart but it shows the X-Axis (time) as unix time obviously. I tried to…
FractalStorm
  • 1
  • 1
  • 5
0
votes
1 answer

grab UNIX timestamp down to the last second

How would I grab the exact Unix time in python3? import time t = (time.strftime("%d/%m/%Y")) For example I can use the code above and plug that into a converter. But this does not work for what I need because there is no output to hours and seconds
John Tobey
  • 23
  • 6
1 2 3
99
100