Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Get number of days between 2 dates

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 05-12-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 September 2017, 2:05 PM EDT
Location: Aachen, Germany
Posts: 11,374
Thanks: 299
Thanked 3,527 Times in 3,244 Posts
As I said it needs "a recent bash" - what be your version? Try date instead.
The Following User Says Thank You to RudiC For This Useful Post:
jiam912 (05-12-2017)
Sponsored Links
    #9  
Old Unix and Linux 05-12-2017
jiam912's Unix or Linux Image
jiam912 jiam912 is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 5 September 2017, 2:57 PM EDT
Location: EEUU
Posts: 353
Thanks: 269
Thanked 1 Time in 1 Post
RudiC

the bash version i am using is GNU bash, 4.2.37(1) - release (x86_64-pc-linux-gnu)
Sponsored Links
    #10  
Old Unix and Linux 05-16-2017
AbelLuis AbelLuis is offline
Registered User
 
Join Date: Jan 2014
Last Activity: 10 August 2017, 11:24 PM EDT
Posts: 28
Thanks: 9
Thanked 10 Times in 10 Posts
Hi all,
there is an ancient formula that convert quickly a "gregorian" date (month, day, year) to "julian" date (number of days from January 1 of year 1).

This is the implementation in awk.

File greg_to_jul.awk:


Code:
BEGIN {
    limes="31,59,90,120,151,181,212,243,273,304,334,365";
    split(limes, vlim, ",");
    vlim[0]=0;
}

function julian(d,m,a)
{
    bis=(a%4==0 && a%100 != 0 || a%400 == 0);
    da=(a-1)*365+int((a-1)/4)-int((a-1)/100)+int((a-1)/400);
    dm=vlim[m-1]+(m>2 && bis);
    return(da+dm+d);
}

{
 split ($3, v3, "/");
 split ($1, v1, "/");

 printf "%s, %s, %s, %d\n", $1, $2, $3, julian(v3[2], v3[1], v3[3]) - julian(v1[2], v1[1], v1[3]);
}

So, you can obtain your exact results (the second set of they):


Code:
awk -f greg_to_jul.awk file2

The user time was 1 ms, versus 60 ms of the previous version.

Regards.

Last edited by AbelLuis; 05-17-2017 at 12:16 AM..
The Following User Says Thank You to AbelLuis For This Useful Post:
jiam912 (05-21-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calculate the number of days between 2 dates - bash script kovacsakos Shell Programming and Scripting 3 09-13-2012 06:49 PM
Calculate the number of days between 2 dates - PHP kovacsakos Web Programming 3 03-23-2012 06:34 AM
Get number of days between given dates suryanarayana Shell Programming and Scripting 4 04-25-2010 08:43 AM
Number of days between two distinct dates radheymohan UNIX for Advanced & Expert Users 1 10-16-2007 08:39 AM
Find number of days and list out the dates in between Raynon Shell Programming and Scripting 2 02-12-2007 07:52 PM



All times are GMT -4. The time now is 02:50 PM.