A program to compute number of days between two dates?
Hi:
I mean, does slackware include one in his distributions? In the affirmative case I would like to know its name. |
You don't really need a program. In bash you can try something like:
Code:
echo $(( ($(date -ud 20130331 +%s) - $(date -ud 20130314 +%s))/86400 )) |
I'll try it, thanks. Though I do not think, if I want to compute difference between April 7, 1784 and today it will work.
|
Just another example. :)
Code:
echo $((($(date +%s)-$(date +%s -d "April 7, 1784"))/86400)) days but ("it's always something"))... Code:
echo $((($(date +%s -d "March 14, 2013")-$(date +%s -d "April 7, 1784"))/86400)) days. 1 day can make a difference! |
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.
|
I love date math and the awe inspiring feeling I get when it works. Giddy.
Anyway, anything significant about April 7, 1784, if I may ask? subscribed with interest... Code:
xmas () |
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.
Code:
echo "($(date +%s)-($(date -d "April 7 1784" +%s)))/86400" | bc -l Code:
echo "($(date -d "March 14" +%s)-($(date -d "April 7 1784" +%s)))/86400" | bc -l |
Quote:
Code:
$ cal 9 1752 Code:
$ echo $(($(date --date="1752-9-14" +%s)/-86400)) days ago |
I like playing with this sort of stuff too.
Instead of use date, I rolled my own. Code:
gazl@ws1:/var/tmp$ cat days.sh Code:
gazl@ws1:/var/tmp$ ./days.sh 2013 3 14 |
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 :-) Batch processing of Hollerith punched cards, I was allowed to test only once a day :^) |
Python's datetime module can also be used if you're sure both date are after the Gregorian reform.
Code:
$ python |
Quote:
https://github.com/rg3/cal_1582 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. http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/ncal/ |
|
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.
|
All times are GMT -5. The time now is 07:03 PM. |