LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 03-14-2013, 08:31 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
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.
 
Old 03-14-2013, 08:37 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 ))
The code might be transformed to a shell function that accepts the two dates as arguments. Just my .
 
4 members found this post helpful.
Old 03-14-2013, 09:32 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.
 
Old 03-14-2013, 10:13 AM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Just another example.
Code:
echo $((($(date +%s)-$(date +%s -d "April 7, 1784"))/86400)) days
83616 days.

but ("it's always something"))...
Code:
echo $((($(date +%s -d "March 14, 2013")-$(date +%s -d "April 7, 1784"))/86400)) days.
83615 days.

1 day can make a difference!

Last edited by Habitual; 03-14-2013 at 10:18 AM.
 
Old 03-14-2013, 10:19 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.

Last edited by stf92; 03-14-2013 at 10:25 AM.
 
Old 03-14-2013, 11:00 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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 () 
{ 
    Christmas=$(date --date="2013-12-25" +%j);
    dayofyear=$(date +%j);
    echo "There are $(($Christmas-$dayofyear)) days until Christmas."
}
but there's a bug.
 
Old 03-14-2013, 11:30 AM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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?
 
Old 03-14-2013, 11:53 AM   #8
T3slider
Senior Member
 
Registered: Jul 2007
Distribution: Slackware64-14.1
Posts: 2,367

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
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
would give you an unrounded value from the current day/time, though
Code:
echo "($(date -d "March 14" +%s)-($(date -d "April 7 1784" +%s)))/86400" | bc -l
is probably still better.
 
Old 03-14-2013, 01:02 PM   #9
saulgoode
Member
 
Registered: May 2007
Distribution: Slackware
Posts: 288

Rep: Reputation: 155Reputation: 155
Quote:
Originally Posted by stf92 View Post
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
 
Old 03-14-2013, 01:11 PM   #10
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
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 
#/bin/bash

YEAR=$1
MONTH=$2
DAY=$3

function leapyear()
{
   local leapyear
   local year="$1"

   if ! ((year % 400)); then
     leapyear=0
   elif ! ((year % 100)); then
     leapyear=1
   elif ! ((year % 4)); then
     leapyear=0
   else
     leapyear=1
   fi   
 
   return $leapyear
}


if leapyear $YEAR ; then
   MONTHDAYS=( 0 31 60 91 121 152 182 213 244 274 305 335 )
else
   MONTHDAYS=( 0 31 59 90 120 151 181 212 243 273 304 334 )
fi


DAYS=$(( ((YEAR-1)*365) + (YEAR-1)/4 - (YEAR-1)/100 + (YEAR-1)/400 )) 
DAYS=$(( DAYS + MONTHDAYS[MONTH-1] )) 
DAYS=$(( DAYS + DAY - 1)) 


echo "$DAYS complete since the beginning (start of 1AD)"
Note: it's zero not one based (i.e. Jan 1st 1AD is day 0).

Code:
gazl@ws1:/var/tmp$ ./days.sh 2013 3 14
734940 complete since the beginning (start of 1AD)
gazl@ws1:/var/tmp$ ./days.sh 1784 4 7
651324 complete since the beginning (start of 1AD)
gazl@ws1:/var/tmp$ echo $(( 734940 - 651324 ))
83616
Glad to see I got the same number of days.
 
Old 03-14-2013, 01:27 PM   #11
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,055

Rep: Reputation: Disabled
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 :^)

Last edited by Didier Spaier; 03-14-2013 at 01:30 PM.
 
Old 03-14-2013, 01:27 PM   #12
rg3
Member
 
Registered: Jul 2007
Distribution: Fedora
Posts: 527

Rep: Reputation: Disabled
Python's datetime module can also be used if you're sure both date are after the Gregorian reform.

Code:
$ python
Python 2.7.3 (default, Jul  3 2012, 19:58:39) 
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import *
>>> a = date(2013, 3, 14)
>>> b = date(1784, 4, 7)
>>> print a -b
83616 days, 0:00:00
 
Old 03-14-2013, 01:32 PM   #13
rg3
Member
 
Registered: Jul 2007
Distribution: Fedora
Posts: 527

Rep: Reputation: Disabled
Quote:
Originally Posted by Didier Spaier View Post
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.

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/
 
Old 03-14-2013, 01:36 PM   #14
imitis
Member
 
Registered: Oct 2007
Location: Latvia, Liepaja
Distribution: Slackware
Posts: 92

Rep: Reputation: 29
I'm using this one :

http://kde-apps.org/content/show.php...?content=37238
 
Old 03-14-2013, 01:45 PM   #15
Mark Pettit
Member
 
Registered: Dec 2008
Location: Cape Town, South Africa
Distribution: Slackware 15.0
Posts: 619

Rep: Reputation: 299Reputation: 299Reputation: 299
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Manipulate Date based on number of days? pinga123 Solaris / OpenSolaris 1 03-02-2012 02:05 PM
no of days between two dates in yyyymmdd format csegau Programming 1 09-18-2011 02:15 PM
[SOLVED] looping thru input files with different number of days. btacuso Programming 8 11-03-2010 11:54 PM
[SOLVED] How to subtract two dates in a file to get number of days? btacuso Linux - Newbie 2 07-13-2010 11:33 AM
How the complier compute the number after the kernel symbol? lawrencelee Linux - Software 3 01-14-2003 03:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 09:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration