Re: It it now a leap year?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-08-08 at 18:52 -0400, Andrew Parker wrote:
> On 8/8/07, Kevin J. Cummings <cummings@xxxxxxxxxxxxxxxxxx> wrote:
> > Andrew Parker wrote:
> > > On 8/8/07, Mike - EMAIL IGNORED <m_d_berger_1900@xxxxxxxxx> wrote:
> > >> Is there a system call or variable that will
> > >> tell me if it is now a leap year, or should I
> > >> do the arithmetic?
> > >
> > > For bash the following will display 061 if its currently a leap year,
> > > 060 otherwise
> > >
> > > date -d "$(date +%Y)/03/01" +%j
> >
> > It doesn't work with dates after 2037/03/01 ....
> 
> I may be being pedantic, but the requirement was to determine if the
> *current* year was a leap year.  I am willing to bet that this code
> will still work in 2038 as "date" will be using more than 31 bits by
> then.

Here's a C function that will do what you want.  I've even commented
it and written it as plainly as I can:

#include <time.h>

/* Function returns 1 if this is a leap year, 0 if not  */
int is_this_a_leap_year() {

    time_t      seconds;
    struct tm   *curtime;
    int         year;

    seconds = time(NULL);       /* Get current time in seconds  */
    curtime = localtime(seconds);  /* Convert to structure      */
    year = curtime->tm_year;    /* Grab year part               */

    if (year % 100 == 0) {      /* Is this a century?           */
        if (year % 400 == 0)    /* Yes, divisible by 400?       */
            return(1);          /* Yup, it's a leap year        */
        else                    /* No, not divisible, so...     */
            return(0);          /* ...not a leap year           */

    } else if (year % 4 == 0) { /* Is year divisible by 4?      */
        return(1);              /* Yes, it's a leap year        */
    }

    return(0);                  /* Not a leap year              */
}

Hope that helps.
----------------------------------------------------------------------
- Rick Stevens, Principal Engineer             rstevens@xxxxxxxxxxxx -
- CDN Systems, Internap, Inc.                http://www.internap.com -
-                                                                    -
-    Working with Linux is like wrestling with a worthy opponent.    -
-   Working with Windows is like picking on an annoyed child with a  -
-                            loaded handgun.                         -
----------------------------------------------------------------------

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux