A program to compute number of days between two dates?
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Oh I see. It's all built into the date command. It has to give date in days, starting from some predetermined date! 2000 was a weird year. As it is divisible by 100, it should not have been leap year. However, it is divisible by 400 too. So an exception is made and the year is not a lear year! But I did not see this commented on the newspapers.
Yes, the satisfaction one gets when one sees the program working is unrivaled. The first job they gave me when I began working at a certain software firm was to write a program to compute number of days between dates. It was the epoch of minicomputers, PDP-11 for instance.
I chose 1784 not to go before 1600, because I was not sure about the year the Gregorian reform began and did not want to give a pre-Gregorian date as an example. I guess the bug is in the date command?
I don't believe it is a bug -- it is a rounding error. If you explicitly pass two dates then you will get the correct difference since it will assume 00:00, whereas if you just get the difference from the current time it might be half a day or more off, which would round improperly. I think though that bash rounds by cutting off the decimal point so it isn't the most accurate.
I chose 1784 not to go before 1600, because I was not sure about the year the Gregorian reform began and did not want to give a pre-Gregorian date as an example. I guess the bug is in the date command?
For Britain (and the U.S.), the switch took place in September of 1752. The 'cal' command gets this right:
Code:
$ cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
The 'date' command doesn't seem to account for this:
Code:
$ echo $(($(date --date="1752-9-14" +%s)/-86400)) days ago
79365 days ago
$ echo $(($(date --date="1752-9-2" +%s)/-86400)) days ago
79377 days ago
Still waiting for solution working since year 1 (Julian then Gregorian calendars).
I've done that as an exercise in Cobol long ago (circa 1977, IIRC :-)
Before the BSD ncal command accepted a flag allowing you to select the Gregorian reform date, I wrote a small Python script to print calendars assuming October 1582. Much of that code can be used to calculate the difference.
Also, the source of the ncal BSD command is interesting as it has an algorithm to count days too and calculate the day of the week for any date. It's pretty simple, actually.
Go with the Python method @rg3 suggests. If you're dipping your toes into what's really a programming problem, then you might as well go the whole hog and learn a decent programming language. And I cannot recommend anything better than Python. The simple example (s)he gave shows just how easy it is.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.