Unix time
The Unix time is a time definition that was developed for the Unix operating system and established as the POSIX standard. Unix time counts the seconds since Thursday, January 1st, 1970, 00:00 UTC . The start date is also known as The Epoch (see Epoch ). The switch from one second to the next is synchronous to UTC. Leap seconds are ignored, a leap second has the same time stamp as the second before it. Before Unix version 6 (1975), the Unix clock counted in hundredths of a second, so the epoch had to be set anew every year.
properties
conversion
The conversion into a human-readable form, including the use of time zones , daylight saving time and leap years , is then carried out by additional functions of the standard library . The representation of the date as seconds since the Unix era is often used because it is much easier for computer programs to process than the "human" date format. With these values, periods of time can easily be calculated as differences of seconds. Summer or winter time, time zones and leap seconds then no longer play a role. For this reason, this value is also often used as a time stamp . The time stamp plays an important role in practically all server applications, for example in the field of PHP or MySQL applications when differentiating and generating time-related database entries. The predefined PHP functions date()
and mktime()
enable the simple conversion of a time stamp into a human-readable date format and vice versa , for example by inserting suitable function arguments.
Year 2038 problem
Since January 1, 1970, the POSIX standard has required at least a signed 32- bit number ( integer ) to specify the time in seconds . The Unix time is guaranteed to include the values −2,147,483,648 to +2,147,483,647. Converted into years, this corresponds to a little more than −68 to +68.
On January 19, 2038 at 3:14:08 a.m. UTC, computer systems that store the Unix time in a signed 32-bit variable will therefore overflow and thus jump back.
Unix times before December 13, 1901 20:45:52 UTC cannot be represented with a signed 32-bit number, since the time stamps would be less than −2,147,483,648.
Modern Unix systems, at least in their 64-bit variant, use a signed 64-bit number for which the risk of an overflow is of no practical relevance. Time spans of up to 292 billion years can be correctly represented here.
Embedded systems based on Unix are currently almost exclusively 32-bit systems. The ongoing development of equipping technical devices (e.g. routers, clock radios, automobiles and airplanes) with embedded systems based on Unix means that these devices will no longer function correctly from January 19, 2038.
If a developer uses the unsigned 32-bit type, he could use the Unix era beyond the year 2100. However, this does not apply if he is using the standard library. This is set to the minimum POSIX standard.
Leap seconds
Leap seconds are not counted in Unix time, since they do not occur regularly, but are only announced six months before their insertion , depending on the fluctuating earth rotation . All days are exactly seconds in Unix time, time differences in Unix time that go beyond a leap second are therefore too short by this second.
In recent years there have been several attempts to add a representation of the leap second to the POSIX Unix time definition as part of the POSIX standardization. However, the discussions on this topic have so far not led to a generally accepted result.
comparison
For human use, times broken down into sections, such as the number of years, months, days, and hours, with additional information for the time zone and daylight saving time, are clearer. Insofar as everyday time occurs without specifying daylight saving time and time zone, it remains incomplete for computer processing. The broken representation makes calculations more complex in any case.
Unix offers a conversion of Unix time into a fractional representation by means localtime()
of. Summer time and time zone are obtained from this function, usually invisible to the user, from the administrative settings of the system - therefore local-time.
In the DOS systems, the broken time, but without specifying the time zone, was the standard format and is saved in a compact representation on FAT file systems for time stamps of the files.
A time specification can also be saved as a floating point number . Time information that is very distant can also be saved, sometimes millions of years away, but then with reduced time accuracy. A resolution of less than a second is e.g. B. possible with a double value for the next 10,000 years.
Special values
Unix enthusiasts have made it a custom to organize so-called "time_t parties" - similar to the New Year celebrations at the turn of the year - at certain values of Unix time . Usually round decimal values like 1,000,000,000 or 2,000,000,000 are celebrated. However, some users also celebrate round binary values, for example +2 30 (1,073,741,824), which fell on January 10, 2004 13:37:04 UTC. On February 13th, 2009 at 23:31:30 UTC (February 14th, 2009 at 00:31:30 CET), the search engine operator Google switched a special doodle because the Unix time reached 1234567890 at that moment. The Heise Newsticker also dedicated an article to this value.
These times are usually celebrated as "n seconds since the Unix era". However, due to the introduction of leap seconds , this designation is not entirely correct.
value | hexadecimal | Time (UTC) |
---|---|---|
−2 31 = −2 147 483 648 | 80 00 00 00 | December 13, 1901 8:45:52 pm |
−2 30 = −1 073 741 824 | B0 00 00 00 | December 23, 1935 10:22:56 |
−1,000,000,000 | C4 65 36 00 | April 24, 1938 10:13:20 PM |
−2 29 = - | 536 870 912E0 00 00 00 | December 27, 1952 5:11:28 AM |
−2 28 = - | 268 435 456F0 00 00 00 | June 30, 1961 2:35:44 AM |
0 | 00 00 00 00 | January 1, 1970 00:00:00 |
2 16 = | 65 53600 01 00 00 | Jan 1, 1970 6:12:16 PM |
2 24 = | 16 777 21601 00 00 00 | July 14, 1970 4:20:16 am |
100,000,000 | 05 F5 E1 00 | March 3, 1973 9:46:40 AM |
2 28 = | 268 435 45610 00 00 00 | July 4, 1978 9:24:16 PM |
500,000,000 | 1D CD 65 00 | November 5, 1985 00:53:20 |
2 29 = | 536 870 91220 00 00 00 | Jan 5, 1987 6:48:32 PM |
805 306 368 | 30 00 00 00 | July 9, 1995 4:12:48 PM |
1,000,000,000 | 3B 9A CA 00 | September 9, 2001 1:46:40 |
2 30 = 1 073 741 824 | 40 00 00 00 | Jan 10, 2004 1:37:04 p.m. |
1 111 111 111 | 42 3A 35 C7 | March 18, 2005 01:58:31 |
1,234,567,890 | 49 96 02 D2 | February 13, 2009 11:31:30 PM |
1,300,000,000 | 4D 7C 6D 00 | March 13, 2011 7:06:40 am |
1 342 177 280 | 50 00 00 00 | July 13, 2012 11:01:20 am |
1,400,000,000 | 53 72 4E 00 | May 13, 2014 4:53:20 pm |
1,500,000,000 | 59 68 2F 00 | July 14, 2017 02:40:00 |
1,600,000,000 | 5F 5E 10 00 | September 13, 2020 12:26:40 PM |
1 610 612 736 | 60 00 00 00 | January 14, 2021 8:25:36 AM |
1,700,000,000 | 65 53 F1 00 | November 14, 2023 10:13:20 PM |
1,800,000,000 | 6B 49 D2 00 | January 15, 2027 8:00 a.m. |
1 879 048 192 | 70 00 00 00 | July 18, 2029 5:49:52 AM |
1 900 000 000 | 71 3F B3 00 | March 17, 2030 5:46:40 PM |
2,000,000,000 | 77 35 94 00 | May 18, 2033 3:33:20 AM |
2,100,000,000 | 7D 2B 75 00 | July 18, 2036 1:20 p.m. |
2 31 - 1 = 2 147 483 647 | 7F FF FF FF | January 19, 2038 3:14:07 AM |
When using the unsigned 32-bit type | ||
2 32 −1 = 4 294 967 295 | FF FF FF FF | February 7, 2106 6:28:15 am |
Unix commands
In some Unix-like systems, the following command can be used to convert a Unix time into the equivalent UTC time (the behavior of date from the example is not part of the POSIX standard).
date -u -d @UNIXTIME
Example:
date -u -d @1234567890
Fr 13. Feb 23:31:30 UTC 2009
Conversely, the current number of seconds since January 1, 1970 on some Unix / Linux systems can be checked using
date +%s
display (the behavior of date is not part of the POSIX standard here either).
Example implementation
If you want to calculate the Unix time at a given point in time, this can be done using the following calculation method. Unix time knows no time zones and uses a time that has been cleared of time zones as input. Leap seconds are not taken into account for the past or the future due to the lack of predictability.
Attention: The following source text is written in the programming language C and works with machine-dependent data types. The year 2038 problem does not occur with this program, however, because the data type "long long" used has at least 64 bits. Like every sample source text, it is used for illustration purposes only and should not be used in practice without checking.
/** Konvertiert gegliederte UTC-Angaben in Unix-Zeit.
* Parameter und ihre Werte-Bereiche:
* - jahr [1970..2038]
* - monat [1..12]
* - tag [1..31]
* - stunde [0..23]
* - minute [0..59]
* - sekunde [0..59]
*/
long long unixzeit(int jahr, int monat, int tag,
int stunde, int minute, int sekunde)
{
const short tage_seit_jahresanfang[12] = /* Anzahl der Tage seit Jahresanfang ohne Tage des aktuellen Monats und ohne Schalttag */
{0,31,59,90,120,151,181,212,243,273,304,334};
int schaltjahre = ((jahr-1)-1968)/4 /* Anzahl der Schaltjahre seit 1970 (ohne das evtl. laufende Schaltjahr) */
- ((jahr-1)-1900)/100
+ ((jahr-1)-1600)/400;
long long tage_seit_1970 = (jahr-1970)*365 + schaltjahre
+ tage_seit_jahresanfang[monat-1] + tag-1;
if ( (monat>2) && (jahr%4==0 && (jahr%100!=0 || jahr%400==0)) )
tage_seit_1970 += 1; /* +Schalttag, wenn jahr Schaltjahr ist */
return sekunde + 60 * ( minute + 60 * (stunde + 24*tage_seit_1970) );
}
A conversion from a given Unix time to our usual date and time representation is possible with the following function.
void UnixzeitNachDatumZeit(unsigned long int unixtime,
int *pJahr, int *pMonat, int *pTag,
int *pStunde, int *pMinute, int *pSekunde)
{
const unsigned long int SEKUNDEN_PRO_TAG = 86400ul; /* 24* 60 * 60 */
const unsigned long int TAGE_IM_GEMEINJAHR = 365ul; /* kein Schaltjahr */
const unsigned long int TAGE_IN_4_JAHREN = 1461ul; /* 4*365 + 1 */
const unsigned long int TAGE_IN_100_JAHREN = 36524ul; /* 100*365 + 25 - 1 */
const unsigned long int TAGE_IN_400_JAHREN = 146097ul; /* 400*365 + 100 - 4 + 1 */
const unsigned long int TAGN_AD_1970_01_01 = 719468ul; /* Tagnummer bezogen auf den 1. Maerz des Jahres "Null" */
unsigned long int TagN = TAGN_AD_1970_01_01 + unixtime/SEKUNDEN_PRO_TAG;
unsigned long int Sekunden_seit_Mitternacht = unixtime%SEKUNDEN_PRO_TAG;
unsigned long int temp;
/* Schaltjahrregel des Gregorianischen Kalenders:
Jedes durch 100 teilbare Jahr ist kein Schaltjahr, es sei denn, es ist durch 400 teilbar. */
temp = 4 * (TagN + TAGE_IN_100_JAHREN + 1) / TAGE_IN_400_JAHREN - 1;
*pJahr = 100 * temp;
TagN -= TAGE_IN_100_JAHREN * temp + temp / 4;
/* Schaltjahrregel des Julianischen Kalenders:
Jedes durch 4 teilbare Jahr ist ein Schaltjahr. */
temp = 4 * (TagN + TAGE_IM_GEMEINJAHR + 1) / TAGE_IN_4_JAHREN - 1;
*pJahr += temp;
TagN -= TAGE_IM_GEMEINJAHR * temp + temp / 4;
/* TagN enthaelt jetzt nur noch die Tage des errechneten Jahres bezogen auf den 1. Maerz. */
*pMonat = (5 * TagN + 2) / 153;
*pTag = TagN - (*pMonat * 153 + 2) / 5 + 1;
/* 153 = 31+30+31+30+31 Tage fuer die 5 Monate von Maerz bis Juli
153 = 31+30+31+30+31 Tage fuer die 5 Monate von August bis Dezember
31+28 Tage fuer Januar und Februar (siehe unten)
+2: Justierung der Rundung
+1: Der erste Tag im Monat ist 1 (und nicht 0).
*/
*pMonat += 3; /* vom Jahr, das am 1. Maerz beginnt auf unser normales Jahr umrechnen: */
if (*pMonat > 12)
{ /* Monate 13 und 14 entsprechen 1 (Januar) und 2 (Februar) des naechsten Jahres */
*pMonat -= 12;
++*pJahr;
}
*pStunde = Sekunden_seit_Mitternacht / 3600;
*pMinute = Sekunden_seit_Mitternacht % 3600 / 60;
*pSekunde = Sekunden_seit_Mitternacht % 60;
}
This function expects an unsigned integer as input parameter ("unsigned long int"). According to the C standard, this is at least 32 bits. The function therefore delivers correct results until at least January 2106.
Web links
Individual evidence
- ^ The Open Group Base Specifications Issue 7, Rationale, section 4.16 Seconds Since the Epoch . The OpenGroup. Retrieved January 22, 2017.
- ↑ General Concepts. Retrieved May 5, 2018 .
- ↑ Doodle at Unix time 1234567890
- ↑ Jürgen Schmidt: 1234567890 , Heise online, February 14, 2009