comp.unix.shell FAQ - Answers to Frequently Asked Questions Appendix A and B

From: Joe Halpin (j.p.h_at_comcast.net)
Date: 06/28/05


Date: Mon, 27 Jun 2005 21:47:50 -0500

Archive-name: unix-faq/shell/sh
Posting-Frequency: monthly
Version: $Id: cus-faq-appendix.html,v 1.2 2005/05/21 19:15:57 jhalpin Exp $
Maintainer: Joe Halpin

Please read the introduction in the first section of this document.
This section assumes you have read that introduction.

======================================================================

Appendix A: Examples

  Web sites:
  -------------------------------------------

    Heiner Steven's Shelldorado site has quite a few example scripts,
    tutorials, and links to other such places.

      http://www.shelldorado.com/

  Arbitrary Date Arithmetic
  -------------------------------------------

    From: Tapani Tarvainen (tt@it.jyu.fi)
    Subject: Re: yesterday's date under the shell
    View: Complete Thread (8 articles)
    Original Format
    Newsgroups: comp.unix.shell
    Date: 2002-02-12 07:45:05 PST
    
    "Jean-No?l" <Jean@freckles.de> writes:
    
> To determine the yesterday date i do it so:
> TZ=PST+24 date +%d
> it work well but my question is:
> does this work on all systems and all shells
> or should i do it otherwise ???
    
    No, it does not work on all systems at all times.
    In some it will work practically always,
    on others never, on most it works sometimes
    and sometimes not. I would recommend against it.
    
    Unfortunately there is no short and sweet portable
    solution. If you have or can install Gnu date it
    will do it cleanly, otherwise you can find a number
    of solutions posted in this group in the past.
    For a general solution you could try the following,
    which should work with POSIXy shells (I've only tested
    it with HP's which is essentially ksh88, though):
    
    #! /usr/bin/sh
    
    # Date calculations using POSIX shell
    # Tapani Tarvainen July 1998, February 2001 (POSIXified)
    # This code is in the public domain.
    
    # Julian Day Number from calendar date
    date2julian() # day month year
    {
      day=$1; month=$2; year=$3
      tmpmonth=$((12 * year + month - 3))
      tmpyear=$((tmpmonth / 12))
      print $(( (734 * tmpmonth + 15) / 24 - 2 * tmpyear + \
        tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
    }
    
    # Calendar date from Julian Day Number
    julian2date() # julianday
    {
      tmpday=$(($1 - 1721119))
      centuries=$(( (4 * tmpday - 1) / 146097))
      tmpday=$((tmpday + centuries - centuries/4))
      year=$(( (4 * tmpday - 1) / 1461))
      tmpday=$((tmpday - (1461 * year) / 4))
      month=$(( (10 * tmpday - 5) / 306))
      day=$((tmpday - (306 * month + 5) / 10))
      month=$((month + 2))
      year=$((year + month/12))
      month=$((month % 12 + 1))
      print $day $month $year
    }
    
    # Day of week, Monday=1...Sunday=7
    dow() # day month year
    {
      print $(( $(date2julian $1 $2 $3) % 7 + 1))
    }
    
    ##################### The End ########################
    
    Those allow rather arbitrary date computations.
    For example, yesterday's date can be computed like this:
    
    julian2date $(( $(date2julian $(date +"%d %m %Y") ) - 1 ))
    
    --
    Tapani Tarvainen

======================================================================

Appendix B: References



Relevant Pages

  • Re: Happy Birthday Julius Caesar!
    ... Clearly the disorders of the Roman calendar before the Julian reform, ... Romans from thinking of July 13 as Caesar's birthday long after his death (proof ... year in the Julian system but not the Gregorian, ...
    (alt.talk.royalty)
  • Re: Calendar Question (Julian and Gregorian)
    ... I have been familiar with the Julian and Gregorian Calendars for quite ... a few years and understand the usual conversions, ... at which the Julian calendar slips with respect to the actual solar ...
    (soc.history.medieval)
  • Re: calculating the julian day
    ... has Fortran 77 code to convert Gregorian calendar dates to Julian ... Last year I got an email from someone using my Windows astronomy DLL, ... SOFA routine. ... They convert between calendar date and Julian ...
    (comp.lang.fortran)
  • Re: Maya Long Count
    ... calendar. ... to use Julian days. ... the Zero Year was found missing in my thoughts. ... so SkyGlobe is not the only program with glitches. ...
    (sci.archaeology.mesoamerican)
  • Re: Calculate the string statement
    ... Jean Meeus states his formula to be invalid for negative JulianTime. ... the Gregorian calendar uses the 4, 100, 400 year rules for ever. ... Just like the Julian Calendar, starting in 45 BC, uses the 4 year rule ...
    (comp.lang.pascal.borland)