[sf-lug] days until (GNU date + expr) ... Re: SF-LUG meeting notes for Sunday June 2, 2019
Michael Paoli
Michael.Paoli at cal.berkeley.edu
Tue Jun 4 23:08:21 PDT 2019
For better and/or worse, GNU date has extensions way beyond POSIX,
that allow it to be used for various types of data calculations, getting
seconds since the epoch, etc.
So, if one puts in the date, can do something like (or, heck, write
a program where you give the date as argument), and ... nothin'
beyond lowly expr. Heck, bash even has a bunch of built-in
arithmetic capabilities, but, forgoing those entirely, and only
using expr's basic integer math capabilities ...
$ expr \( $(date -d '2019-06-17 12:00' +%s) - $(date -d 'today 12:00'
+%s) + \( 3600 \* 12 \) \) / 3600 / 24
13
$
Yep, 13 days from today (in current timezone).
And, how's that do its thing? I'll leave it as an exercise if one
wants to put it into a script with lots 'o nice comments 'n all that
... and maybe even add input validation ...
So - it uses two dates, one given (2019-06-17), and today,
for each of those, it gives seconds since the epoch for noon
that day. Why noon? Well, remember, not all days are 60*60*24 seconds
long. There's +-summer/daylight time changes, leap seconds, etc.
So ... to work around those possible gottchas,
first, calculate seconds between the two noons of the two days,
then add the number of seconds in 1/2 of a 60*60*24 day,
then take that sum and do an integer divide, where we drop any fractional
part ("round down"), where we divide by the typical 60*60*24 seconds in
a day. That'll give us number of days between today, in whatever timezone
we're using, to the other date we supply - likewise in whatever timezone
we're using. And that's exactly correct ... unless I or GNU screwed it up.
;-)
I'm sure one could do similar with awk ... if one's using an awk that
has time capabilities in it such as mktime ... otherwise one would
need some bits external to awk.
> From: "Rick Moen" <rick at linuxmafia.com>
> Subject: Re: [sf-lug] SF-LUG meeting notes for Sunday June 2, 2019
> Date: Tue, 4 Jun 2019 09:15:05 -0700
> Quoting Akkana Peck (akkana at shallowsky.com):
>
>> Rick Moen writes:
>> > Quoting Alex Kleider (akleider at sonic.net):
>> >
>> > > I tried your script:
>> >
>> > Short version: It turns out I assumed GNU awk, but failed to say so.
>>
>> How's this? (Add to your .bashrc or wherever.)
>>
>> dayofweek() {
>> echo -n "Date as YYYY-MM-DD: "
>> read d
>> date --date=$d
>> }
>>
>> If you really only want the day, change the last line to
>>
>> date --date=$d +%a
>>
>> ...Akkana
>
> This is probably a stupid question, but: How would that bash function
> be used in a daysuntil script such as the one I included in
> http://linuxmafia.com/faq/Linux_PR/newlug.html item #12? And are you
> saying it somehow averts any need for specifically _GNU_ awk (which I
> invoked for its mktime & systime functions)?
>
> Strictly speaking, my daysuntil script claims to be a Bourne (as opposed
> to bash) script (per the shebang I less-than-mindfully employed), but
> the real thing is I'm unclear on how the above dayofweek bash shell
> function is related to the use-case. (Could be just lack of coffee.)
>
> Context of my daysutil script: I'd frequently found myself wanting to
> know how many calendar days in the future a projected LUG event date is,
> and therefore scribbled down that modest shell / awk recipe as a tool to
> fill in the gap -- saving time over getting a calendar and counting
> date boxes. And then it occurred to me that it's a thing LUG planners
> everywhere might also need, so I threw it into my 19-year-old essay
> 'Recipe for a Successful Linux User Group'.
>
> Of course, if you and other coders want to just play with the concept
> and improve it, great! Personally, I find date handling and arithmetic a
> nightmare, and was glad I could leverage GNU awk's mktime & systime
> functions so as to make the whole headache go away.
More information about the sf-lug
mailing list