Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between two dates

1,081 views
Skip to first unread message

S.F.

unread,
May 17, 2011, 11:08:01 AM5/17/11
to
Hi,

I would like to calculate the difference between two dates (D1-D2).
D1 is always a given date. In the data, D2 could be multiple records.

data_file (the first five rows):
2011-05-09
2009-04-16
2010-05-05
2008-09-26
2011-01-10

I would like to have the output as:
5/9/2011 7
4/16/2009 760
5/5/2010 376
9/26/2008 962
1/10/2011 126

I have tried the following script, but it could only work if only one
date in the data_file.

D1=`date +%s -d "2011-05-16"`
D=`awk '{print$1}' data_file`
D2=`date +%s -d $D`
((diff=D1-D2))
awk -v sec=$diff '{printf "%d",sec/(60*60*24)}' data_file > out

Can anyone help me with this script?
Thanks!!

Grant

unread,
May 17, 2011, 7:01:04 PM5/17/11
to
On Tue, 17 May 2011 08:08:01 -0700 (PDT), "S.F." <lotus....@gmail.com> wrote:

>Hi,
>
>I would like to calculate the difference between two dates (D1-D2).
>D1 is always a given date. In the data, D2 could be multiple records.
>
>data_file (the first five rows):
>2011-05-09
>2009-04-16
>2010-05-05
>2008-09-26
>2011-01-10
>
>I would like to have the output as:
>5/9/2011 7
>4/16/2009 760
>5/5/2010 376
>9/26/2008 962
>1/10/2011 126

How did you five differences from five input lines?


>
>I have tried the following script, but it could only work if only one
>date in the data_file.
>
>D1=`date +%s -d "2011-05-16"`
>D=`awk '{print$1}' data_file`
>D2=`date +%s -d $D`
>((diff=D1-D2))
>awk -v sec=$diff '{printf "%d",sec/(60*60*24)}' data_file > out
>
>Can anyone help me with this script?

That looks like a shell script?

Try converting to epoch seconds, doing the subtract, then print using
the date formatter -- I forget the details, but it's on the gawk page,
assuming you're on Linux, something like:

grant@pooh:~$ cat dates


2011-05-09
2009-04-16
2010-05-05
2008-09-26
2011-01-10

grant@pooh:~$ gawk '{split($0,a,"-");td=strftime("%s",mktime(a[1]" "a[2]" "a[3]" 12 0 0"))};FNR==1{ld=td};{printf"%-16s %6d\n",strftime("%m/%d/%Y",td),(ld-td)/(60*60*24)}' dates
05/09/2011 0
04/16/2009 753
05/05/2010 369
09/26/2008 955
01/10/2011 119

I leave it to others to play golf with that lot! I'm sure there's
an easier way.

Grant.

Hermann Peifer

unread,
May 18, 2011, 2:27:32 PM5/18/11
to
On 18/05/2011 01:01, Grant wrote:
>
> grant@pooh:~$ gawk '{split($0,a,"-");td=strftime("%s",mktime(a[1]" "a[2]" "a[3]" 12 0 0"))};FNR==1{ld=td};{printf"%-16s %6d\n",strftime("%m/%d/%Y",td),(ld-td)/(60*60*24)}' dates
> 05/09/2011 0
> 04/16/2009 753
> 05/05/2010 369
> 09/26/2008 955
> 01/10/2011 119
>
> I leave it to others to play golf with that lot! I'm sure there's
> an easier way.

Just to add that by default, gawk's mktime attempts to determine whether
daylight savings time is in effect for the specified time. In my time
zone, DST started on 27 March, at 2am. So the difference between 2am and
3am evaluates to 0.

$ echo "2011 3 27 2 0 0,2011 3 27 3 0 0" |
gawk -F, '{print mktime($2)-mktime($1)}'
0

In time zone UTC, there was a difference of 1 hour.

$ echo "2011 3 27 2 0 0,2011 3 27 3 0 0" |
TZ=UTC gawk -F, '{print mktime($2)-mktime($1)}'
3600

Hermann

Grant

unread,
May 18, 2011, 11:31:08 PM5/18/11
to
On Wed, 18 May 2011 20:27:32 +0200, Hermann Peifer <pei...@gmx.eu> wrote:

>On 18/05/2011 01:01, Grant wrote:
>>
>> grant@pooh:~$ gawk '{split($0,a,"-");td=strftime("%s",mktime(a[1]" "a[2]" "a[3]" 12 0 0"))};FNR==1{ld=td};{printf"%-16s %6d\n",strftime("%m/%d/%Y",td),(ld-td)/(60*60*24)}' dates
>> 05/09/2011 0
>> 04/16/2009 753
>> 05/05/2010 369
>> 09/26/2008 955
>> 01/10/2011 119
>>
>> I leave it to others to play golf with that lot! I'm sure there's
>> an easier way.
>
>Just to add that by default, gawk's mktime attempts to determine whether
>daylight savings time is in effect for the specified time. In my time
>zone, DST started on 27 March, at 2am. So the difference between 2am and
>3am evaluates to 0.

Does daylight savings time matter if you're counting whole days?

Hermann Peifer

unread,
May 19, 2011, 12:52:38 AM5/19/11
to
On 19/05/2011 05:31, Grant wrote:
> On Wed, 18 May 2011 20:27:32 +0200, Hermann Peifer<pei...@gmx.eu> wrote:
>
>
> Does daylight savings time matter if you're counting whole days?


$ echo "2011 3 26 12 0 0,2011 3 27 12 0 0" |
gawk -F, '{print (mktime($2)-mktime($1))/(60*60*24) }'
0.958333


$ echo "2011 3 26 12 0 0,2011 3 27 12 0 0" |
TZ=UTC gawk -F, '{print (mktime($2)-mktime($1))/(60*60*24) }'
1


Hermann

0 new messages