[svlug] Because dates in calendar are closer than they appear

Ivan Sergio Borgonovo mail at webthatworks.it
Wed Dec 14 18:04:38 PST 2016



On 12/15/2016 12:43 AM, Rick Moen wrote:
> Starting around the year 2000, I've published a little essay called
> 'Recipe for a Successful Linux User Group', which has subsequently been
> republished in Usenix Association's ;login magazine and elswhere.  It's
> at http://linuxmafia.com/faq/Linux_PR/newlug.html , and widely linked.
>
> For the first time in a long while, I've made a modest addition to it,
> under item 12, that is about /usr/bin/cal being your friend for event
> timing and planning.  I just added this bit:
>
>   As the old joke goes, "Dates In Calendar Are Closer Than They Appear."
>   _Use whatever works_ to ensure that announcements of upcoming events go
>   out _on time_, which for most groups means somewhere between 1 week and
>   24 hours before show time.  Personally, I find this simple script tool
>   handy as local utility /usr/local/bin/daysutil, to do quick checks from
>   my shell prompt:
>
>   #!/bin/sh
>   echo "Type the future date in ISO 8601 (YYYY-MM-DD) format."
>   read futuredate
>   echo $futuredate | tr -s '-' ' ' | awk '{dt=mktime($0 " 00-00-00")-systime(); print int(dt/86400+1) " days";}'
>
> Now, that's a sucky little perfunctory script[1].  Among other things,
> it's a bit haphazard about the definition of 'day' (but precise enough,

You can use date to get/convert dates to epoch and then subtract and 
divide. Of course dates are tricky and this method may end up returning 
wrong results. I don't even want to think how they could end up wrong 
because dates are tricky ;)

This is way too verbose but I think it is going to be a bit more 
accurate, anyway I won't bet on it since dates are tricky and I just 
finished to write it and here it is 3:00am

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import datetime

def days(d):
     d1 = datetime.datetime.today()
     d2 = datetime.datetime.strptime(d, '%Y-%m-%d')
     dd = (d1 - d2).days
     return dd

from datetime import date
if __name__ == "__main__":
     parser = argparse.ArgumentParser(
             description='Days between now and date',
             formatter_class=argparse.ArgumentDefaultsHelpFormatter)

     parser.add_argument(
                 dest='d',
                 metavar='DATE',
                 help='date')

     args = parser.parse_args()

     d = args.d

     print(days(d))

I'll get some sleep, try to understand better the use case and see if I 
can come up with something more useful and possibly more educative.
Possibly using posix stuff. awk is pretty powerful


-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it http://www.borgonovo.net




More information about the svlug mailing list