Linux "date" command ignores leap-seconds?

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Jan 17 02:49:13 UTC 2013


On Wed, 16 Jan 2013, Walter Dnes wrote:

> On Wed, Jan 16, 2013 at 02:59:08PM -0500, D. Hugh Redelmeier wrote
>
>> Clearly all leap years before the Epoch don't count, but ones after do.
>> Years that are multiples of 4 are leap years (UNLESS they are multiples
>> of 100 (UNLESS they are multiples of 400)).  A leap year delays the
>> start of subsequent years by one day.
>> 1969 is the year after the last multiple of 4 before the epoch.
>> 1901 is the year after the last multiple of 100 before the epoch.
>> 1601 is the year after the last multiple of 400 before the epoch
>
>  My quick-n-dirty leap-year algorithm uses bash integer division.
> Here's pseudocode
>
> * LEAPYR = FALSE
> * if ( YYYY mod 100 = 0 ) then YYYY = ( YYYY / 100 )
> * if ( YYYY mod 4 = 0 ) then LEAPYR = TRUE
>
>  Note that the 2nd line converts 1700 to 17, 1800 to 18, 1900 to 19,
> and 2000 to 20.  Then it is safe to blindly prceed with the test for
> whole-number-multiple-of-4.
>
>  Here's bash code...
>
>   zyear=${xyear}
>   scratch=$(( ${xyear} / 100 ))
>   if [ $(( ${scratch} * 100 )) -eq ${zyear} ]; then
>       zyear=${scratch}
>   fi
>   leap_year=0
>   scratch=$(( ${zyear} / 4 ))
>   if [ $(( ${scratch} * 4 )) -eq ${zyear} ]; then
>      leap_year=1
>   fi

   I use either of these functions. The second one is faster and
   portable even to the Bourne shell:


is_leap_year() {
     [ $(( $1 % 4 ))   -eq 0 ] &&
     [ $(( $1 % 100 )) -ne 0 ] ||
     [ $(( $1 % 400 )) -eq 0 ]
}

is_leap_year() { #@ USAGE: is_leap_year [year]
     ily_year=${1:-`date +%Y`}
     case $ily_year in
         *0[48] |\
         *[2468][048] |\
         *[13579][26] |\
         *[02468][048]00 |\
         *[13579][26]00 ) _IS_LEAP_YEAR=1
                          return 0 ;;
         *) _IS_LEAP_YEAR=0
            return 1 ;;
     esac
}


$ is_leap_year 2012 && echo Yes || echo No
Yes
$ is_leap_year 2013 && echo Yes || echo No
No


-- 
    Chris F.A. Johnson, <http://cfajohnson.com/>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list