zsh-users
 help / color / mirror / code / Atom feed
* Date prompt expansion
@ 2012-11-26 11:33 Mark van Dijk
  2012-11-26 11:46 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Mark van Dijk @ 2012-11-26 11:33 UTC (permalink / raw)
  To: zsh-users

Following up on my email sent yesterday where I asked a question about
pcre, the question also implied I'm doing stuff with dates.

Currently I am using two different methods of getting the today's
day/month and yesterday's day/month:

1) for today I'm using prompt expansion:
todaysDay=${(%):-%D{"%d"}}   # 26
todaysMonth=${(%):-%D{"%m"}} # 11

2) for yesterday I'm using FreeBSD's date command:
yesterdaysDay=$(/bin/date -v -1d "+%y")
yesterdaysMonth=$(/bin/date -v -1d "+%m")

I'm more fond of the first method. Can the second be done without
calling /bin/date?

Mark


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Date prompt expansion
  2012-11-26 11:33 Date prompt expansion Mark van Dijk
@ 2012-11-26 11:46 ` Peter Stephenson
  2012-11-26 12:38   ` Mark van Dijk
  2012-11-26 16:15   ` Aaron Schrab
  0 siblings, 2 replies; 5+ messages in thread
From: Peter Stephenson @ 2012-11-26 11:46 UTC (permalink / raw)
  To: zsh-users

On Mon, 26 Nov 2012 12:33:25 +0100
Mark van Dijk <lists+zsh@internecto.net> wrote:
> 2) for yesterday I'm using FreeBSD's date command:
> yesterdaysDay=$(/bin/date -v -1d "+%y")
> yesterdaysMonth=$(/bin/date -v -1d "+%m")

(You probably mean "+%d" rather "+%y" in the first one.)

Up to oddities with leap seconds, you can do it by

zmodload zsh/datetime
strftime -s yesterdaysDay "+%d" $(( EPOCHSECONDS - 24 * 60 * 60 ))

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Date prompt expansion
  2012-11-26 11:46 ` Peter Stephenson
@ 2012-11-26 12:38   ` Mark van Dijk
  2012-11-26 13:01     ` Peter Stephenson
  2012-11-26 16:15   ` Aaron Schrab
  1 sibling, 1 reply; 5+ messages in thread
From: Mark van Dijk @ 2012-11-26 12:38 UTC (permalink / raw)
  To: zsh-users

> zmodload zsh/datetime
> strftime -s yesterdaysDay "+%d" $(( EPOCHSECONDS - 24 * 60 * 60 ))

Great, I'll give this a go. Thanks!

I noticed something odd when I was trying to fill an associated array
with these prompt expansions and the behaviour replicates into non-array
variable assignments too. I suspect this is caused by a bug somewhere..

Kind regards,
Mark

=====
typeset -A datetime DATETIME DateTime dATEtIME
datetime=("month"     "${(%):-%D{"%m"}}"
          "day"       "${(%):-%D{"%d"}}"
          "ymdt"      "${(%):-%D{"%y%m%dT%H:%M"}}")

DATETIME=("month"     "${(%):-%D{%m}}"
          "day"       "${(%):-%D{%d}}"
          "ymdt"      "${(%):-%D{%y%m%dT%H:%M}}")

DateTime=("month"     ${(%):-%D{"%m"}}
          "day"       ${(%):-%D{"%d"}}
          "ymdt"      ${(%):-%D{"%y%m%dT%H:%M"}})

dATEtIME=("month"     ${(%):-%D{%m}}
          "day"       ${(%):-%D{%d}}
          "ymdt"      ${(%):-%D{%y%m%dT%H:%M}})


monthday="${(%):-%D{"%m%d"}}"
MONTHDAY="${(%):-%D{%m%d}}"
MonthDay=${(%):-%D{"%m%d"}}
mONTHdAY=${(%):-%D{%m%d}}

for i (${(k)datetime}) echo "datetime[$i]: ${datetime[$i]}"
for i (${(k)DATETIME}) echo "DATETIME[$i]: ${DATETIME[$i]}"
for i (${(k)DateTime}) echo "DateTime[$i]: ${DateTime[$i]}"
for i (${(k)dATEtIME}) echo "dATEtIME[$i]: ${dATEtIME[$i]}"
echo "monthday: $monthday"
echo "MONTHDAY: $MONTHDAY"
echo "MonthDay: $MonthDay"
echo "mONTHdAY: $mONTHdAY"
=====

Output:

datetime[ymdt]: 121126T13:35}
datetime[day]: 26}
datetime[month]: 11}
DATETIME[ymdt]: 121126T13:35}
DATETIME[day]: 26}
DATETIME[month]: 11}

^ notice the } at the end

DateTime[ymdt]: 121126T13:35
DateTime[day]: 26
DateTime[month]: 11
dATEtIME[ymdt]: 121126T13:35
dATEtIME[day]: 26
dATEtIME[month]: 11

^ correct

monthday: 1126}
MONTHDAY: 1126}
MonthDay: 1126
mONTHdAY: 1126

^ same thing for normal variable assignment


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Date prompt expansion
  2012-11-26 12:38   ` Mark van Dijk
@ 2012-11-26 13:01     ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2012-11-26 13:01 UTC (permalink / raw)
  To: zsh-users

On Mon, 26 Nov 2012 13:38:04 +0100
Mark van Dijk <lists+zsh@internecto.net> wrote:
> I noticed something odd when I was trying to fill an associated array
> with these prompt expansions and the behaviour replicates into
> non-array variable assignments too. I suspect this is caused by a bug
> somewhere..
> typeset -A datetime DATETIME DateTime dATEtIME
> datetime=("month"     "${(%):-%D{"%m"}}"
>           "day"       "${(%):-%D{"%d"}}"
>           "ymdt"      "${(%):-%D{"%y%m%dT%H:%M"}}")
>
> datetime[ymdt]: 121126T13:35}
> datetime[day]: 26}
> datetime[month]: 11}
> DATETIME[ymdt]: 121126T13:35}
> DATETIME[day]: 26}
> DATETIME[month]: 11}
> 
> ^ notice the } at the end

The parameter expansion is terminated by the first "}" it comes across.
The "%D{" is then not terminated but it works anyway since it runs to
the end of the expression.  The remaining "}" is then just a normal
part of the string.  The easiest fix is probably to move the quotes:

datetime=("month"     "${(%):-"%D{%m}"}"
          "day"       "${(%):-"%D{%d}"}"
          "ymdt"      "${(%):-"%D{%y%m%dT%H:%M}"}")

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Date prompt expansion
  2012-11-26 11:46 ` Peter Stephenson
  2012-11-26 12:38   ` Mark van Dijk
@ 2012-11-26 16:15   ` Aaron Schrab
  1 sibling, 0 replies; 5+ messages in thread
From: Aaron Schrab @ 2012-11-26 16:15 UTC (permalink / raw)
  To: zsh-users

At 11:46 +0000 26 Nov 2012, Peter Stephenson <p.stephenson@samsung.com> wrote:
>Up to oddities with leap seconds, you can do it by
>
>zmodload zsh/datetime
>strftime -s yesterdaysDay "+%d" $(( EPOCHSECONDS - 24 * 60 * 60 ))

Unless I'm missing something, that would also have problems around the 
beginning and end of daylight saving time, for a whole hour in each case 
rather than just for a single second as with leap seconds.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-11-26 16:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-26 11:33 Date prompt expansion Mark van Dijk
2012-11-26 11:46 ` Peter Stephenson
2012-11-26 12:38   ` Mark van Dijk
2012-11-26 13:01     ` Peter Stephenson
2012-11-26 16:15   ` Aaron Schrab

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).