Re: date +%s ignores TZ

2008-03-01 Thread Philip Rowlands

On Fri, 29 Feb 2008, Bob Proulx wrote:


Jan Engelhardt wrote:


I wanted to get the number of seconds since the start of the day.

echo $[`date +%s` % 86400];

unfortunately does not do the right thing ÿÿ it would show
82800 instead of 0 when it is (local) midnight.


I can't think of any totally race free way to do this without invoking
date multiple times.


I might be misunderstanding the problem, but it seems easy enough to do 
this calling date only once:


$ date +%T | awk -F: '{ print $1 * 3600 + $2 * 60 + $3 }'
67652

(corresponding to 18:47:32)


Cheers,
Phil




___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-03-01 Thread Andreas Schwab
Philip Rowlands [EMAIL PROTECTED] writes:

 I might be misunderstanding the problem, but it seems easy enough to do
 this calling date only once:

 $ date +%T | awk -F: '{ print $1 * 3600 + $2 * 60 + $3 }'
 67652

This will fail during the day after a DST transition.

Andreas.

-- 
Andreas Schwab, SuSE Labs, [EMAIL PROTECTED]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
And now for something completely different.


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-03-01 Thread Bob Proulx
Andreas Schwab wrote:
 This will fail during the day after a DST transition.

Which points out a terrible bug in my suggestion of how to map the
range of 0-(N-1) to the range of 1-N!

Bob wrote this buggy code:
  case $secondssincedaystart in (0) secondssincedaystart=86400 ;; esac

That will obviously fail miserably at midnight on the day after a dst
change.  Another reason not to do it!

Bob


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-03-01 Thread Jan Engelhardt

On Feb 29 2008 15:26, Bob Proulx wrote:

  echo $[`date +%s` % 86400];

Note that the $[expression] syntax is deprecated and is scheduled for
removal from a future version of the shell.  Please convert to using
the now standard $((expression)) syntax.

  echo $(( $(date +%s) % 86400 ));

$(()) is easily confusable with $(), I therefore ask $[] to be not removed,
more like the reverse actually.


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-03-01 Thread Bob Proulx
Jan Engelhardt wrote:
 $(()) is easily confusable with $(), I therefore ask $[] to be not removed,
 more like the reverse actually.

I am not able to influence the decision.  I am just reporting how it
is documented.

Bob


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


date +%s ignores TZ

2008-02-29 Thread Jan Engelhardt
Hi,


this is probably all correct behavior as it is right now (coreutils 6.9):

$ date +%s
120433
$ TZ=GMT date +%s
120433
$ TZ=PDT date +%s
120433

but is there actually a way to do

$ TZ=anything date +%s -d `date '+%Y-%m-%d %H:%M:%S'`;

without invoking date twice?


thanks,
Jan


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Bob Proulx
Jan Engelhardt wrote:
   $ date +%s
   120433

%s  seconds since 1970-01-01 00:00:00 UTC

   $ TZ=GMT date +%s
   120433
   $ TZ=PDT date +%s
   120433

Right.  I assume you were *very fast* typing in that data and that
seconds did not move on while you were doing it.  :-)  I get the point
though.  That value is a timezone independent value.

 but is there actually a way to do
 
   $ TZ=anything date +%s -d `date '+%Y-%m-%d %H:%M:%S'`;
 
 without invoking date twice?

I think something was lost in translation.

  date '+%Y-%m-%d %H:%M:%S'

That will always produce the current time.  That means that

  date +%s -d `date '+%Y-%m-%d %H:%M:%S'`

is always the same as

  date +%s

There is no need to call date twice to get that result.

Please say a few more words about what you are trying to do.  I think
with a little more understanding it will make better sense.  I see
that you are trying to produce a unix seconds epoch based upon some
time but creating it with date doesn't do it.  You would need some
other time.

  date +%s -d 'last thursday'
  1204182000

Bob


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Jan Engelhardt

On Feb 29 2008 14:20, Bob Proulx wrote:
Right.  I assume you were *very fast* typing in that data and that
seconds did not move on while you were doing it.  :-)  I get the point
though.  That value is a timezone independent value.

 but is there actually a way to do
 
  $ TZ=anything date +%s -d `date '+%Y-%m-%d %H:%M:%S'`;
 
 without invoking date twice?

I think something was lost in translation.

  date '+%Y-%m-%d %H:%M:%S'

That will always produce the current time.  That means that

  date +%s -d `date '+%Y-%m-%d %H:%M:%S'`

is always the same as

  date +%s

There is no need to call date twice to get that result.

There is (my default zone is /etc/localtime -
/usr/share/zoneinfo/Europe/Berlin):

$ TZ=GMT date +%s -d `date '+%Y-%m-%d %H:%M:%S'`
1204325194
$ date +%s
1204321595

(now with not-so-fast typing! :)

Please say a few more words about what you are trying to do.  I think
with a little more understanding it will make better sense.

I wanted to get the number of seconds since the start of the day.

echo $[`date +%s` % 86400];

unfortunately does not do the right thing — it would show
82800 instead of 0 when it is (local) midnight.


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Andreas Schwab
Jan Engelhardt [EMAIL PROTECTED] writes:

 this is probably all correct behavior as it is right now (coreutils 6.9):

   $ date +%s
   120433
   $ TZ=GMT date +%s
   120433
   $ TZ=PDT date +%s
   120433

%s is defined as seconds since 1970-01-01 00:00:00 UTC which obviously
is a constant at any given time throughout the world.

 but is there actually a way to do

   $ TZ=anything date +%s -d `date '+%Y-%m-%d %H:%M:%S'`;

 without invoking date twice?

Please explain what you are trying to achieve.

Andreas.

-- 
Andreas Schwab, SuSE Labs, [EMAIL PROTECTED]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
And now for something completely different.


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Brian Dessent
Jan Engelhardt wrote:

 I wanted to get the number of seconds since the start of the day.
 
 echo $[`date +%s` % 86400];

How about:

echo $[$(date +%s) - $(date -d '' +%s)]

Brian


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Bob Proulx
Jan Engelhardt wrote:
 There is (my default zone is /etc/localtime -
 /usr/share/zoneinfo/Europe/Berlin):
 
   $ TZ=GMT date +%s -d `date '+%Y-%m-%d %H:%M:%S'`
   1204325194
   $ date +%s
   1204321595
 
   (now with not-so-fast typing! :)

:-)

 I wanted to get the number of seconds since the start of the day.
 
   echo $[`date +%s` % 86400];

Note that the $[expression] syntax is deprecated and is scheduled for
removal from a future version of the shell.  Please convert to using
the now standard $((expression)) syntax.

  echo $(( $(date +%s) % 86400 ));

 unfortunately does not do the right thing — it would show
 82800 instead of 0 when it is (local) midnight.

Midnight and noon are neither a.m. nor p.m. but midnight is considered
the start of the day.  Therefore normal convention would use a seconds
range of 0-86399 seconds in any given day similar to 0-59 seconds in a
minute.  Do I understand that you want to use 1-86400?  That would be
like using 1-60 seconds in a minute, right?  In which case I would
simply special case the zero case and convert it to the max value
specially.  But I probably wouldn't do it.

I can't think of any totally race free way to do this without invoking
date multiple times.  The problem is right around midnight we want to
avoid having one invocation from before and one from after.  My
technique is to get the time once and then shape that single point in
time as I need it so as to avoid any possibility of problems at
midnight.  But I don't see that as being too inefficient so I would
simply invoke date several times and not worry about it.  Perhaps
someone else will have a better optimized strategy.

  nowseconds=$(date +%s)
  dateday=$(date -d $nowseconds +%F) # e.g. 2008-02-29
  secondsatdaystart=$(date -d $dateday +%s)
  secondssincedaystart=$(( $nowseconds - $secondsatdaystart ))
  echo $secondssincedaystart

That still treats midnight as 0 seconds since day start.  I think that
is the right thing to do but if you want it the other way then you
could add a case.

  nowseconds=$(date +%s)
  dateday=$(date -d $nowseconds +%F) # e.g. 2008-02-29
  secondsatdaystart=$(date -d $dateday +%s)
  secondssincedaystart=$(( $nowseconds - $secondsatdaystart ))
  case $secondssincedaystart in (0) secondssincedaystart=86400 ;; esac
  echo $secondssincedaystart

I am also sure there are ways to optimize this further but this seemed
good enough.  I didn't test that very much but the numbers seemed
reasonable to me at brief glance at them.

Bob


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils


Re: date +%s ignores TZ

2008-02-29 Thread Bob Proulx
Brian Dessent wrote:
 Jan Engelhardt wrote:
  I wanted to get the number of seconds since the start of the day.
  
  echo $[`date +%s` % 86400];
 
 How about:
 
 echo $[$(date +%s) - $(date -d '' +%s)]

That works most of the time and if I were never to run this at
midnight I would do just what you suggest here and wouldn't worry
about it further.  But Jan specifically mentioned wanting midnight.
That made me avoid suggesting that case.  If this needs to be robust
at midnight then it needs one date invocation which needs more work.

The problem is that the first date might execute at midnight minus one
second.  The second date might execute at midnight, which is the next
day.  If that happens then a negative value would result because the
start of the day subtracted off would be the start of the next day.
Therefore the need to get the time once only.

Bob


___
Bug-coreutils mailing list
Bug-coreutils@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-coreutils