10

I initially learned that Unix time is the number of seconds that have elapsed since 00:00:00 (UTC) on 1 January 1970. With 24 hours in a day, that means that the unix timestamp grows by 86400 every day.

Then I heard about the concept of leap seconds, and thought that would mean that maybe on some days, the unix timestamp would grow by 86401 seconds in a day, but apparently this is not the case. From what I've read, every day is treated as if it contains exactly 86400 seconds. When you get a leap second, the operating system will 'fudge' it in some way to make sure there's still 86400 timestamps - either make every 'second' that day a little bit longer than a real SI second, or they'll report the same integer timestamp twice in a row.

So I think that this means that every date since 1 Jan 1970 can be mapped to a unique integer which is the timestamp at 00:00:00 (UTC) that day divided by 86400. (guaranteed to be an integer with no remainder because as discussed every day has to have 86400 timestamps). Alternatively you could take any timestamp during that day and calculate floor(timestamp / 86400).

For example, today, Fri 23rd April 2021 - timestamp at 00:00:00 UTC was 1619136000. As expected, this is a multiple of 86400, and 1619136000 / 86400 = 18740. There have been 18740 days since the unix epoch.

So my question is:

  1. Does this integer already have a well-known name? Is it already widely used in software for representing dates? I've not been able to find any reference online to this concept.
  2. Is my logic here correct - is there really a unique integer for each date, and you can easily calculate it in your code as timestamp_at_midnight_utc / 86400? Or is there some subtle problem that I've overlooked.

My motivation here is that I often have to do complicated calculations involving lots of dates without any time information (I work for a vacation rentals company where each unit has it's own availability calendar). I think I could make a lot of efficiency improvements in my code if I was working with integers uniquely representing a date, instead of DateTime objects, or strings like '2021-04-23'.

4
  • You don't call it, except as "days since Unix epoch"; be aware of leap seconds (some days had 86401 seconds). On Linux, see time(7) Apr 23, 2021 at 7:01
  • If allowed, trust the implementation of localtime_r(3). With GNU libc, it is free software whose source code you can download and study Apr 23, 2021 at 7:06
  • 2
    BTW, are you certain that computing dates is the bottleneck of your software? Did you profile your software? There is a small risk that some client will sue your employer if he/she has to pay one more day of rental, and such trials are expensive. Discuss that point with your client or boss. Take into account that leap years are rare! Apr 23, 2021 at 7:08
  • 1
    related: Unix time and leap seconds. Regarding optimization (see @BasileStarynkevitch's comment), keep in mind that Unix time doesn't help you with time zones, DST changes and the like. So this is very application- and language-specific if you can actually gain performance (or just make things more complicated) I think. Apr 23, 2021 at 7:10

3 Answers 3

7

Yes, your logic is correct. Where I still get worried is that it requires you to do your calculations in UTC. Holiday rentals happen in a time zone, and associating a date in that time zone with the start of the day in UTC instead could get confusing soon.

And yes, the concept of a count of days since 1970-01-01 is sometimes used, though not often that I have seen.

In the Java documentation the terms “epoch day” and “epoch day count” are used, but this doesn’t make these terms a standard.

I think that the first avenue for you to consider is whether either your programming language comes with a library for counting days without the need to convert to and from seconds, or there is a trustworthy third-party library that you may use for the purpose.

This Java snippet confirms your calculation:

    // A LocalDate in Java is a date without time zone or UTC offset
    LocalDate date = LocalDate.of(2021, Month.APRIL, 23);
    long epochDayCount = date.toEpochDay();
    System.out.println("Epoch day: " + epochDayCount);

Output agrees with the result you got:

Epoch day: 18740

Link: Epoch day count in the Java documentation.

3
  • 1
    Thanks for your feedback, this is helpful. Regarding the timezone stuff - I know what I am doing here. My intention is to have a mapping between dates and integers that deliberately ignores the timezone. eg 23rd April 2021 <=> 18740 regardless of where you are in the world. And so 25th April 2021 <=> 18742. The number of days between those dates is unambiguously 18742 - 18740 = 2. If I was using timestamps or some kind of DateTime object and I did that calculation and we happened to have a daylight saving time switch in between, I might get something like 2.01 days as my result. Apr 26, 2021 at 14:47
  • The whole point of all of this is for me to be able to deal with dates without any time component or timezones, because of all the difficulties around timezones and daylight saving time. I understand the important point that in practice, in order to get the integer associated with a date using standard software libraries that work with unix timestamps, then I must be very careful that I'm always specifying midnight UTC along with each date. If I were to accidentally omit the UTC part and use my system default timezone, then the timestamp won't be a multiple of 86400 and it all breaks. Apr 26, 2021 at 14:57
  • The Epoch Day count in that Java link appears to be exactly what I was describing, so thanks Ole V.V. I know know what to call this quantity! Apr 26, 2021 at 14:59
2

From my experience there is no official name for "days since epoch". Some nuances that can be detected about UNIX time (and its measurement units):

  1. It appears to be (relatively) officially defined as the number of seconds since the UNIX epoch.
  2. The main purpose of the UNIX time mechanism (regardless of measurement unit conventions) is to define a point in time.
  3. In the context of point #2, in practice, it has already become traditional that the UNIX timestamp is often returned in milliseconds.
  4. There are several factors that can influence the measurement unit that is available to you:
  • design decisions by APIs, libraries and programming languages
  • time resolution / clock frequency of the software & hardware that you are running on - e.g. some circuits, controllers or other entities aren't able to reach millisecond resolution or they don't have enough bits available in memory to represent big numbers.
  • performance reasons - offering a time service at millisecond or second resolution via HTTP might prove too much for networks / server CPUs. The next best thing would be a UNIX timestamp in minutes. This value can then be cached by intermediary caches for the duration of 1 minute.
  • use cases - there are epochs (e.g. in astronomy) where the day is the main measurement unit.

Here are a few examples of such day-based epochs:

If you have a look at one method of calculating the Julian Date, dividing by 86400 is an important step. So, given that the JD system seems to be widely used in astronomy, I think it would be safe to consider this division by 86400 as valid :)

0

This is a more complex question than you might initially realize. You want the days since 1970 to be the same for all times during the local day, and you also don't want daylight saving time changes in the local time zone and UTC date changes to affect the output.

The solution I found was to compute the seconds since 1970 in UTC but for the current local date at midnight, not the current UTC date. Here is a Linux shell script solution:

echo $(( $(date -u -d "$(date '+%Y-%m-%d') 00:00:00" '+%s') / 24 / 60 / 60 ))

date -u forces UTC time, while the second date returns the local year-month-day. This computation actually generates an integer result, even if you use a computation that supports non-integers. Computing the seconds since 1970 in local time, or using the current UTC date (and no the local day) will not work.

2
  • This shell script effectively computes the seconds since 1970 in UTC for the current local date at midnight, ensuring consistency across time zones and daylight saving time changes. For more tools and resources related to Unix time conversions, check out Epoch Unix Timestamp Converter. newfreetool.com/en/epoch-unix-timestamp-converter
    – NomanJaved
    Mar 10 at 15:15
  • I rethought what the script was doing and came up with better wording: Compute the seconds between 1970-01-01 00:00:00 UTC to the current local date at UTC midnight. This avoids problems with time zones and daylight saving time changes. Mar 11 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.