0

I want to ask how to convert the number of days from 0000-00-00 to date in Linux?

(Note: I saw from epochconverter that it can be converted. For example, 737887 is converted to April 7, 2020, that is There are 737887 days between 0000-00-00 and today (Tuesday, Apr 07, 2020).)

7
  • 3
    "0000-00-00" is not a date, so there can't be a number of days between that and whatever date you choose. Apr 7, 2020 at 10:16
  • As suggested by @GerardH.Pille , best to suggest a precise and repeatable (and ideally simple) example. Are you happy to make use of the Python interpreter for this task?
    – moo
    Apr 7, 2020 at 10:42
  • So, MySQL seems to have a year 0, if I counted right, corresponding to "0001-01-01 BC", commonly known as year -1, jan 1st. Apr 7, 2020 at 10:56
  • 2
    @sun There is no "year 0", and no month and day 0 either.
    – Kusalananda
    Apr 7, 2020 at 11:49
  • 1
    There really is no Year 0. The year flips from 1BC to 1AD without going through 0. Also, there have been several different mandated changes in the calendar, even in Europe (Julian to Gregorian, for example). England dropped 13 days in Sept 1752. Dates based on year 0 are often meaningless when referred to previous eras, or countries that were isolated from the Catholic church. Apr 7, 2020 at 11:52

2 Answers 2

1

On many systems, it is best to work with days since epoch (1970-01-01).

So you can keep a constant number of days between 0000-00-00 and 1970-01-01 which is exactly 719528. Then sum it up with days since epoch:

echo $(( 719528 + $(date --utc --date "$1" +%s)/86400 ))
8
  • The shell is capable of handling this, no need for bc: "expr 719528 + $(date --date='2020-04-06 00:00:00' '+%s') / 86400" Apr 7, 2020 at 11:03
  • Thank you sir, what should I do if I want to convert 737887 to date?
    – sun
    Apr 7, 2020 at 13:50
  • echo "719528 + $ (($ (date --utc --date" $ 1 "+% s) / 86400))" | bc execution result is 737887, the result is correct. But expr 719528 + $ (date --date = '2020-04-06 00:00:00' '+% s') / 86400 execution result is 737885, the result is a bit biased.
    – sun
    Apr 7, 2020 at 14:03
  • date -d @seconds should convert "seconds" since epoch to date. So you should subtract 719528 from 737887 then multiply by 86400 for seconds then use the first command. Apr 7, 2020 at 14:47
  • also that'd be great if you can accept it as solution Apr 7, 2020 at 14:47
1

The perl DateTime module is good for this stuff:

$ perl -MDateTime -E '
    $base = DateTime->new(year=>0, month=>1, day=>1);
    $now = DateTime->now;
    say $now->delta_days($base)->in_units("days");
'
737887

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .