Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 80d3f846 in mainline


Ignore:
Timestamp:
2012-04-15T15:24:03Z (10 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master
Children:
f6af126
Parents:
a8a0d43
Message:

rtc: check for leap years

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/time/cmos-rtc/cmos-rtc.c

    ra8a0d43 r80d3f846  
    9292static int rtc_tm_sanity_check(struct tm *t);
    9393static void rtc_register_write(rtc_t *rtc, int reg, int data);
    94 
     94static bool is_leap_year(int year);
     95
     96static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    9597
    9698static ddf_dev_ops_t rtc_dev_ops;
     
    429431rtc_tm_sanity_check(struct tm *t)
    430432{
     433        int ndays;
     434
    431435        if (t->tm_sec < 0 || t->tm_sec > 59)
    432436                return EINVAL;
     
    442446                return EINVAL;
    443447
    444         /* XXX Some months have less than 31 days... */
     448        if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))
     449                ndays = 29;
     450        else
     451                ndays = days_month[t->tm_mon];
     452
     453        if (t->tm_mday > ndays)
     454                return EINVAL;
     455
    445456        return EOK;
     457}
     458
     459/** Check if a year is a leap year
     460 *
     461 * @param year   The year to check
     462 *
     463 * @return       true if it is a leap year, false otherwise
     464 */
     465static bool
     466is_leap_year(int year)
     467{
     468        bool r = false;
     469
     470        if (year % 4 == 0) {
     471                if (year % 100 == 0)
     472                        r = year % 400 == 0;
     473                else
     474                        r = true;
     475        }
     476
     477        return r;
    446478}
    447479
Note: See TracChangeset for help on using the changeset viewer.