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.
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?
not in the date command....the week after but before New Year's Day, it's spits out negative integers.
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.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Here, just for fun, is ndays.
Save below as ndays.c, compile it with
Code:
cc -o ndays ndays.c -lm
Use it as
Code:
ndays
Enter Lower Date (MM/DD/YYYY): 04/07/1784
Enter Upper Date (MM/DD/YYYY): 03/14/2013
Number of Days: 83616
This uses Julian Day Numbers; i.e., add one per day (Julian Day Numbers start at Julian Day 0: 01/01/-4713 -- that's a long, long time ago); today is Julian Day Number 24,56,366.
Here's the source (do what you wish with it):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
static time_t julday (int, int, int);
void main (void)
{
int ldd, lmm, lyy;
int udd, umm, uyy;
(void) fprintf (stdout, "Enter Lower Date (MM/DD/YYYY): ");
(void) scanf ("%d/%d/%d", &lmm, &ldd, &lyy);
(void) fprintf (stdout, "Enter Upper Date (MM/DD/YYYY): ");
(void) scanf ("%d/%d/%d", &umm, &udd, &uyy);
(void) fprintf (stdout, "Number of Days: %ld\n",
julday (umm, udd, uyy) - julday (lmm, ldd, lyy));
}
/* Gregorian Calendar adopted 15 Oct 1582 */
#define IGREG (15+31L*(10+12L*1582))
static time_t julday (int mm, int id, int iyyy)
{
time_t jul;
int ja, jy, jm;
if (iyyy == 0) {
(void) fprintf (stderr, "julday:\tthere is no year zero\n");
exit (1);
}
if (iyyy < 0)
++iyyy;
if (mm > 2) {
jy = iyyy;
jm = mm + 1;
} else {
jy = iyyy - 1;
jm = mm + 13;
}
jul = (time_t) (floor (365.25 * jy) +
floor (30.6001 * jm) + id + 1720995);
if (id + 31L * (mm + 12L * iyyy) >= IGREG) {
ja = 0.01 * jy;
jul += 2 - ja + (int) (0.25 * ja);
}
return (jul);
}
#undef IGREG
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 :^)
I instead had to pay for the cards getting punched and for computer time. I had a program that was correct but would have completed execution by about year 10**40. But I left the computer facilities thinking the program had some error and having spent good money by the way.
Hi @Didier : Well, beauty may be in the eye of the beholder, but I think we all agree that the Mona Lisa is a beautiful painting, and that Leonard Cohen's Hallelujah is beautiful music .... My history is 33 years of programming, from Fortran, Pascal, Basic, Assembler (Various), Cobol (the worst), "c", "c++", "sql", and then some smatterings of other languages which I've had the misfortune to look into, like Perl and Java. But I've worked professionally for the last 10 years in Python, and my life is better for it. Sometimes I have so much fun and pleasure writing in this language that I feel guilty pulling a pay-cheque at the end of the month :-)
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Keep in mind that a lot of the adoption of the Gregorian calendar depended upon religion -- Britain didn't mandate the change until 1752 (when the US was still a British colony). When you're calculating this stuff, you really need to know "where" you're calculating for; there's a nice chart of Gregorian adoption dates at http://en.wikipedia.org/wiki/Gregorian_calendar if you want to be picky.
If you're trying to figure out Gregorian dates for old records (prior to adoption)...
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.
I think we all agree that the Mona Lisa is a beautiful painting
I do. I have he chance to live 35 min by feet from the Musée du Louvre. BTW there is another woman's portrait from Léonard that I saw at the National Gallery of Art in Washington back in 1987. I believe this is the only painting by Vinci permanently exposed in the USA.
Quote:
Originally Posted by Mark Pettit
and that Leonard Cohen's Hallelujah is beautiful music
hem, well... Maybe I do not consider myself as included in all, then
Last edited by Didier Spaier; 03-14-2013 at 03:19 PM.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
Here’s the file including Slackware releases dates (the data got from Wikipedia):
slackware-releases
Code:
1.0 July 17, 1993
1.1.0 November 5, 1993
1.1.2 February 5, 1994
2.0 July 2, 1994
2.1 October 31, 1994
2.2 March 30, 1995
2.3 May 24, 1995
3.0 November 30, 1995
3.1 June 3, 1996
3.2 February 17, 1997
3.3 June 11, 1997
3.4 October 14, 1997
3.5 June 9, 1998
3.6 October 28, 1998
3.9 May 10, 1999
4.0 May 17, 1999
7.0 October 25, 1999
7.1 June 22, 2000
8.0 July 1, 2001
8.1 June 18, 2002
9.0 March 19, 2003
9.1 September 26, 2003
10.0 June 23, 2004
10.1 February 2, 2005
10.2 September 14, 2005
11.0 October 2, 2006
12.0 July 1, 2007
12.1 May 2, 2008
12.2 December 10, 2008
13.0 August 26, 2009
13.1 May 24, 2010
13.37 April 27, 2011
14.0 September 28, 2012
Here’s the script that processes the above file using from-to script from the post #17:
slacky
Code:
#!/bin/bash
if [ "$1" == "" ] ; then echo "$0 file" ; exit ; fi
> $1.new
grep -n '' $1 | sed 's/:/ /;s/ /_/g' | \
while read line junk
do
line=`echo $line | sed 's/_/ /g'`
number=`echo $line | awk '{print $1}'`
version=`echo $line | awk '{print $2}'`
date=`echo $line | awk '{print $3,$4,$5}'`
date=`date --date="$date" +"%Y-%m-%d"`
echo $number:$version:$date >> $1.new
done
cat $1.new | \
while read line junk
do
line=`echo $line | sed 's/:/ /g'`
number=`echo $line | awk '{print $1}'`
version[$number]=`echo $line | awk '{print $2}'`
date[$number]=`echo $line | awk '{print $3}'`
if [ $number -ge 2 ]
then
prev_number=`expr $number - 1`
period=`from-to ${date[$prev_number]} ${date[$number]}`
echo "${version[$prev_number]} -> ${version[$number]} = $period"
fi
done
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
Quote:
Originally Posted by Didier Spaier
Some of us will be tempted to forecast next Slackware release's date from the average delay between two releases, I guess
That’s rather natural idea in such a case. To “predict” the date one can use different methods. The more methods she’ll try the more results she’ll gain. As a result one of these “predictions” will be really close to the actual date of the next release. That’s the whole truth about the predictions and the numerology.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.