Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-29 Thread Johannes Wiedersich
On 2008-08-26 11:50, Ron Johnson wrote:
 On 08/25/08 20:34, s. keeling wrote:
 Ron Johnson [EMAIL PROTECTED]:
  On 08/24/08 11:32, Rick Pasotto wrote:
 Why pipe it to bc? Keep it in the shell:

 $ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
 396
  [snip]

  One stylistic reason for piping to bc is that some people think that
  bash's $[] syntax gets too hard to read if you nest it too much.

 I've been running *nix on my home boxes since '93, and I've never even
 seen that syntax.  That's a bashism, I hope?
 
 Yes.  And a relatively modern one at that.  Somewhere in the 3.x series.

Maybe not to modern. Just looking at man bash on lenny:


Arithmetic Expansion
   Arithmetic expansion allows the evaluation of an arithmetic
   expression and the substitution of  the  result.   The
   format for arithmetic expansion is:

  $((expression))

   The old format $[expression] is deprecated and will be removed in
   upcoming versions of bash.

--  *old format*

Johannes



signature.asc
Description: OpenPGP digital signature


Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-29 Thread [EMAIL PROTECTED]

 Arithmetic Expansion
   Arithmetic expansion allows the evaluation 
   of an arithmetic expression and the substitution 
   of  the  result.   

 The format for arithmetic expansion is:

   $((expression))

 The old format  $[expression]  is deprecated 
 and will be removed in  upcoming versions of bash.

  Thanks for bringing the syntax for the evaluation 
  of arithmetic expressions in bash to my attention 

echo ''  $[ (16/2 + 6) * 3 ]
 
  Previously, I had totally missed it  :-)


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-26 Thread Ron Johnson

On 08/25/08 20:34, s. keeling wrote:

Ron Johnson [EMAIL PROTECTED]:

 On 08/24/08 11:32, Rick Pasotto wrote:

Why pipe it to bc? Keep it in the shell:

$ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
396

 [snip]

 One stylistic reason for piping to bc is that some people think that 
 bash's $[] syntax gets too hard to read if you nest it too much.


I've been running *nix on my home boxes since '93, and I've never even
seen that syntax.  That's a bashism, I hope?


Yes.  And a relatively modern one at that.  Somewhere in the 3.x series.


I do own, and have studied, O'Really's Learning the bash shell.


--
Ron Johnson, Jr.
Jefferson LA  USA

Do not bite at the bait of pleasure till you know there is no
hook beneath it.  -- Thomas Jefferson


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-26 Thread Johann Spies
On Tue, Aug 26, 2008 at 04:50:16AM -0500, Ron Johnson wrote:
 On 08/25/08 20:34, s. keeling wrote:

 I've been running *nix on my home boxes since '93, and I've never even
 seen that syntax.  That's a bashism, I hope?

 Yes.  And a relatively modern one at that.  Somewhere in the 3.x series.


I see it also works in zsh.

Regards
Johann

-- 
Johann Spies  Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

 Seeing then that all these things shall be dissolved,
  what manner of persons ought ye to be in all holy
  conversation and godliness, Looking for and hasting unto
  the coming of the day of God, wherein the heavens being
  on fire shall be dissolved, and the elements shall melt
  with fervent heat? II Peter 3:11,12 


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-25 Thread s. keeling
Ron Johnson [EMAIL PROTECTED]:
  On 08/24/08 11:32, Rick Pasotto wrote:
  
  Why pipe it to bc? Keep it in the shell:
  
  $ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
  396
 
  [snip]

  One stylistic reason for piping to bc is that some people think that 
  bash's $[] syntax gets too hard to read if you nest it too much.

I've been running *nix on my home boxes since '93, and I've never even
seen that syntax.  That's a bashism, I hope?

I do own, and have studied, O'Really's Learning the bash shell.


-- 
Any technology distinguishable from magic is insufficiently advanced.
(*)http://blinkynet.net/comp/uip5.html  Linux Counter #80292
- -http://www.faqs.org/rfcs/rfc1855.htmlPlease, don't Cc: me.


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Edward J. Shornock
On Sunday 24 August 2008 18.07.59 j t wrote:
 Hi all.

 Does anyone have any suggestions for a command-line-interface (CLI)
 calculator that can work out the difference between 2 (gregorian)
 dates (i.e. that is calendar aware). My favourite cli calculator
 (bc) doesn't seem to have any knowledge of the gregorian calendar.

 Just to make it clear, I'd like to be able to type in:

 20080824-20080724 and it would work out the answer as 31


While I'm sure someone else will provide a much better way, I've used 
something like

$ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
31

..in the past.


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Ron Johnson

On 08/24/08 10:07, j t wrote:

Hi all.

Does anyone have any suggestions for a command-line-interface (CLI)
calculator that can work out the difference between 2 (gregorian)
dates (i.e. that is calendar aware). My favourite cli calculator
(bc) doesn't seem to have any knowledge of the gregorian calendar.

Just to make it clear, I'd like to be able to type in:

20080824-20080724 and it would work out the answer as 31

(I'm happy to use any date format for input - I've only used ISO8601
as an example)


I think I'd write a simple Python/Perl script: convert date1 and 
date2 to seconds past epoch, subtract, and divide by 86400.


--
Ron Johnson, Jr.
Jefferson LA  USA

Do not bite at the bait of pleasure till you know there is no
hook beneath it.  -- Thomas Jefferson


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread gary turner

j t wrote:

Hi all.

Does anyone have any suggestions for a command-line-interface (CLI)
calculator that can work out the difference between 2 (gregorian)
dates (i.e. that is calendar aware). My favourite cli calculator
(bc) doesn't seem to have any knowledge of the gregorian calendar.

Just to make it clear, I'd like to be able to type in:

20080824-20080724 and it would work out the answer as 31

(I'm happy to use any date format for input - I've only used ISO8601
as an example)


I Googled date arithmetic, and found this, 
http://www.walkernews.net/2007/06/03/date-arithmetic-in-linux-shell-scripts/


Can you adapt from that?

cheers,

gary
--
Anyone can make a usable web site. It takes a graphic
designer to make it slow, confusing and painful to use.
begin:vcard
fn:Gary Turner
n:Turner;Gary
org:Gary Turner, Web Developer
adr:;;USA
email;internet:[EMAIL PROTECTED]
title:Czar
x-mozilla-html:FALSE
url:http://gtwebdev.com/
version:2.1
end:vcard



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Ron Johnson

On 08/24/08 10:54, Edward J. Shornock wrote:
[snip]


While I'm sure someone else will provide a much better way, I've used 
something like


$ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
31


That a good idea.  I never knew date(1) could do that.  The problem, 
though, is that it doesn't span years.


$ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
30

Use delta from epoch, instead.
$ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
396

--
Ron Johnson, Jr.
Jefferson LA  USA

Do not bite at the bait of pleasure till you know there is no
hook beneath it.  -- Thomas Jefferson


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Rick Pasotto
On Sun, Aug 24, 2008 at 11:25:31AM -0500, Ron Johnson wrote:
 On 08/24/08 10:54, Edward J. Shornock wrote:
 [snip]

 While I'm sure someone else will provide a much better way, I've used  
 something like

 $ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
 31

 That a good idea.  I never knew date(1) could do that.  The problem,  
 though, is that it doesn't span years.

 $ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
 30

 Use delta from epoch, instead.
 $ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
 396

Why pipe it to bc? Keep it in the shell:

$ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
396

-- 
Curiosity is the very basis of education and if you tell me that curiosity
killed the cat, I say only the cat died nobly.
-- Arnold Edinborough
Rick Pasotto[EMAIL PROTECTED]http://www.niof.net


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Ron Johnson

On 08/24/08 11:32, Rick Pasotto wrote:

On Sun, Aug 24, 2008 at 11:25:31AM -0500, Ron Johnson wrote:

On 08/24/08 10:54, Edward J. Shornock wrote:
[snip]
While I'm sure someone else will provide a much better way, I've used  
something like


$ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
31
That a good idea.  I never knew date(1) could do that.  The problem,  
though, is that it doesn't span years.


$ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
30

Use delta from epoch, instead.
$ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
396


Why pipe it to bc? Keep it in the shell:

$ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
396


I was thinking that integer division might result in the occasional 
rounding error.  But now that I think about it, that wouldn't 
matter, since bc does integer math unless you pass it -l.


One stylistic reason for piping to bc is that some people think that 
bash's $[] syntax gets too hard to read if you nest it too much.


--
Ron Johnson, Jr.
Jefferson LA  USA

Do not bite at the bait of pleasure till you know there is no
hook beneath it.  -- Thomas Jefferson


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Damon L. Chesser
On Sun, 2008-08-24 at 11:25 -0500, Ron Johnson wrote:
 On 08/24/08 10:54, Edward J. Shornock wrote:
 [snip]
  
  While I'm sure someone else will provide a much better way, I've used 
  something like
  
  $ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
  31
 
 That a good idea.  I never knew date(1) could do that.  The problem, 
 though, is that it doesn't span years.
 
 $ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
 30
 
 Use delta from epoch, instead.
 $ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
 396

Why the /86400?  I follow all of the above but that operation and number.

 -- 
 Ron Johnson, Jr.
 Jefferson LA  USA
 
 Do not bite at the bait of pleasure till you know there is no
 hook beneath it.  -- Thomas Jefferson
 
 
-- 
Damon L. Chesser [EMAIL PROTECTED]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Sergio Perticone
On 24 Ago, 17:10, j t [EMAIL PROTECTED] wrote:
 [snip]

 (I'm happy to use any date format for input - I've only used ISO8601
 as an example)

Maybe something like that:

#!/usr/bin/python

from time import mktime, strptime
from sys import argv

class DummyDate:
def __init__(self, strdate, format):
self.ticks = mktime(strptime(strdate, format));

def __sub__(self, obj):
return int(self.ticks - obj.ticks) / 86400;

(prg, a, b, fmt) = argv
print DummyDate(a, fmt) - DummyDate(b, fmt)
## end

Or using a sh script with date(1)


 J :-)

s.


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Ron Johnson

On 08/24/08 12:41, Damon L. Chesser wrote:

On Sun, 2008-08-24 at 11:25 -0500, Ron Johnson wrote:

On 08/24/08 10:54, Edward J. Shornock wrote:
[snip]
While I'm sure someone else will provide a much better way, I've used 
something like


$ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
31
That a good idea.  I never knew date(1) could do that.  The problem, 
though, is that it doesn't span years.


$ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
30

Use delta from epoch, instead.
$ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
396


Why the /86400?  I follow all of the above but that operation and number.


60 seconds * 60 minutes * 24 hours = 86400 seconds
   ---  ---  - ---
   minute   hour day   day

--
Ron Johnson, Jr.
Jefferson LA  USA

Do not bite at the bait of pleasure till you know there is no
hook beneath it.  -- Thomas Jefferson


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Vincent Lefevre
On 2008-08-24 11:13:43 -0500, Ron Johnson wrote:
 I think I'd write a simple Python/Perl script: convert date1 and date2 to 
 seconds past epoch, subtract, and divide by 86400.

In Perl, you can also use the Date::Manip module:

   4.  The amount of time between two dates.

 $date1 = ParseDate($string1);
 $date2 = ParseDate($string2);
 $delta = DateCalc($date1,$date2,\$err);
   = 0:0:WK:DD:HH:MM:SS   the weeks, days, hours, minutes,
   and seconds between the two
 $delta = DateCalc($date1,$date2,\$err,1);
   = YY:MM:WK:DD:HH:MM:SS  the years, months, etc. between
the two

-- 
Vincent Lefèvre [EMAIL PROTECTED] - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]



Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates

2008-08-24 Thread Vincent Lefevre
On 2008-08-24 12:25:00 -0500, Ron Johnson wrote:
 I was thinking that integer division might result in the occasional  
 rounding error.

This is the contrary: AFAIK, you really want an integer division.
So, integer arithmetic is OK. But a floating-point division can
be a problem since when the floating-point result is displayed,
it may be rounded to the next integer if at most 4 digits are
displayed after the decimal point, the worst case being:

$ echo 86399/86400 | bc -l
.8842592592592592

For instance, under a shell that supports FP arithmetic (e.g. zsh):

$ printf %.4f\n $((86399./86400))
1.

Now, assuming you want to use floating-point arithmetic, before
displaying the result, you may want to take the floor of the result:

vin% zmodload zsh/mathfunc
vin% printf %.0f\n $((floor(86399./86400)))
0
vin% printf %.0f\n $((floor(86400./86400)))
1

And I proved that under some conditions (which are satisfied here),
one gets the same result as the integer division:

  http://www.vinc17.org/research/papers/rr_intdiv.pdf

-- 
Vincent Lefèvre [EMAIL PROTECTED] - Web: http://www.vinc17.org/
100% accessible validated (X)HTML - Blog: http://www.vinc17.org/blog/
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]