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

What is the +0000 that is at the end of Unix/Posix timestamp?

So, for example below is a timestamp generated by my Cassandra instance. What does the +0000 mean? 2015-06-26 17:02:04+0000 Thanks
vicg
  • 1,280
  • 14
  • 32
9
votes
2 answers

Convert Unix timestamp to Java Date, Spring RequestParam

Following is a request fullcalendar js send to the server. http://localhost:8080/NVB/rest/calendar/events?start=1425168000&end=1428796800 400 How to specify Date pattern (@DateTimeFormat) in Spring Request Param to convert this time to a Date…
Susitha Ravinda Senarath
  • 1,648
  • 2
  • 27
  • 49
9
votes
1 answer

I want to set date as infinite in form of timestamp

I want to set the date as an infinite date in form of timestamp? There are people saying we can use "0" and some are saying set big value like this 60*60*24*100. And I do not want to do this. Is there any suitable way of handling this?
Dinnu Buddy
  • 369
  • 1
  • 3
  • 13
9
votes
2 answers

How can I work with dates before 1900 in PHP?

I am using PHP and jQuery to build an interactive timeline which needs to display dates between 1500 and 2020. I usually use PHP's strtotime function when working with dates, but it does not work for dates pre-1900. The dates will come from a MySQL…
shipshape
  • 1,682
  • 2
  • 17
  • 33
9
votes
5 answers

What is the best way to find out if 2 dates are in same month in PHP?

I have 2 dates that are unix time stamps and I would have to figure out if the dates are in same month. For example if I have 1393624800 = 02 / 28 / 14 and 1391472000 = 2 / 4 / 2014. I figured I could convert the unix time stamps to format like…
Firze
  • 3,939
  • 6
  • 48
  • 61
9
votes
1 answer

Objective C Timestamp

I have found numerous timestamp conversions and they all work but the thing is, when I put my code to the text form of the date, I always come out 4 months ahead. This is pulling the current Day of Week, Date and Time. I have it set this way cause…
Duny
  • 215
  • 4
  • 11
9
votes
2 answers

MySQL convert datetime to unixtime?

I have a column with the DATETIME format and I would like to convert it to UNIXTIME in the database. What kind of query would that be? I know how to convert from UNIXTIME to DATETIME, but I've never done the reverse. I've used FROM_UNIXTIME, is…
user1708530
9
votes
4 answers

Convert string time to UNIX timestamp

I have a string like 2013-05-29T21:19:48Z. I'd like to convert it to the number of seconds since 1 January 1970 (the UNIX epoch), so that I can save it using just 4 bytes (or maybe 5 bytes, to avoid the year 2038 problem). How can I do that in a…
svick
  • 236,525
  • 50
  • 385
  • 514
9
votes
3 answers

bar graph using AChartEngine library

I have a bar graph using AChartEngine library as shown below :: public class MainActivity extends Activity { private String[] mMonth = new String[] { "Jan", "Feb" , "Mar", "Apr", "May", "Jun", "Jul", "Aug" , "Sep", "Oct",…
Kiran
  • 3,095
  • 5
  • 23
  • 38
9
votes
3 answers

Difference between UNIX_TIMESTAMP and NOW() in MySQL

I have a blog where users can comment. I insert the time at which they posted a comment using NOW() and then use date('j M Y', stored timestamp) to show the time at which they posted. I want to know does NOW() return the locatime of the end user or…
sanchitkhanna26
  • 2,143
  • 8
  • 28
  • 42
9
votes
1 answer

Dynamic MySQL partitioning based on UnixTime

My DB design includes multiple MYISAM tables with measurements collected online, Each row record contains auto-incremented id, some data and an integer representing unixtime. I am designing an aging mechanism, and i am interested to use MySQL…
Michael
  • 2,827
  • 4
  • 30
  • 47
9
votes
2 answers

How to set default value of a mysql column as unix_timestamp?

I was thinking at something like this: `post_modified` int(11) NOT NULL DEFAULT UNIX_TIMESTAMP (NOW ()) ON UPDATE UNIX_TIMESTAMP (NOW ()), But this code it is not working.
m3tsys
  • 3,939
  • 6
  • 31
  • 45
9
votes
5 answers

Get the UNIX timestamp from UUID version 1

In our Java application we are trying to get the UNIX time from the UUID version 1. But it's not giving the correct date time values. long time = uuid.timestamp(); time = time / 10000L; // Dividing by 10^4 as it's in 100 nanoseconds precision…
Thiyanesh
  • 519
  • 1
  • 6
  • 14
9
votes
2 answers

PHP DateTime object - Timestamp and Timezones conflict

From a DateTime object, I am interested in getting the time in different TimeZones. As explained in the DateTime::setTimezone doc, this works pretty well when the DateTime object is created from a string: $date = new DateTime('2000-01-01', new…
mika
  • 1,971
  • 3
  • 18
  • 32
9
votes
1 answer

new Date().getTime() not returning timestamp in milliseconds

I've a class which is using java.util.Date class to create a date object and using getTime() to get current milliseconds. I've seen in the Java documentation that getTime() returns the milliseconds, and the same case is on my machine. I've one other…
Chinmay
  • 180
  • 1
  • 2
  • 13