Changeset fa18523 in mainline for uspace/app/date/date.c


Ignore:
Timestamp:
2012-04-25T11:31:10Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
af7e3d3
Parents:
6a3808e
Message:

rtc: move tm_sanity check() from the rtc driver to the date application and make use of mktime() in the
rtc driver to normalize the content of the tm structure.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/date/date.c

    r6a3808e rfa18523  
    4343static int read_time_from_arg(char *wdate, struct tm *t);
    4444static int read_num_from_str(char *str, size_t len, int *n);
     45static int tm_sanity_check(struct tm *t);
     46static bool is_leap_year(int year);
     47
    4548static void usage(void);
     49
     50static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    4651
    4752int
     
    181186                }
    182187
     188                rc = tm_sanity_check(&t);
     189                if (rc != EOK) {
     190                        printf(NAME ": error, invalid date/time\n");
     191                        goto exit;
     192                }
     193
    183194                rc = clock_dev_time_set(sess, &t);
    184195                if (rc != EOK) {
     
    278289}
    279290
     291/** Check if the tm structure contains valid values
     292 *
     293 * @param t     The tm structure to check
     294 *
     295 * @return      EOK on success or EINVAL
     296 */
     297static int
     298tm_sanity_check(struct tm *t)
     299{
     300        int ndays;
     301
     302        if (t->tm_sec < 0 || t->tm_sec > 59)
     303                return EINVAL;
     304        else if (t->tm_min < 0 || t->tm_min > 59)
     305                return EINVAL;
     306        else if (t->tm_hour < 0 || t->tm_hour > 23)
     307                return EINVAL;
     308        else if (t->tm_mday < 1 || t->tm_mday > 31)
     309                return EINVAL;
     310        else if (t->tm_mon < 0 || t->tm_mon > 11)
     311                return EINVAL;
     312        else if (t->tm_year < 0 || t->tm_year > 199)
     313                return EINVAL;
     314
     315        if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))
     316                ndays = 29;
     317        else
     318                ndays = days_month[t->tm_mon];
     319
     320        if (t->tm_mday > ndays)
     321                return EINVAL;
     322
     323        return EOK;
     324}
     325
     326/** Check if a year is a leap year
     327 *
     328 * @param year   The year to check
     329 *
     330 * @return       true if it is a leap year, false otherwise
     331 */
     332static bool
     333is_leap_year(int year)
     334{
     335        bool r = false;
     336
     337        if (year % 4 == 0) {
     338                if (year % 100 == 0)
     339                        r = year % 400 == 0;
     340                else
     341                        r = true;
     342        }
     343
     344        return r;
     345}
     346
    280347static void
    281348usage(void)
Note: See TracChangeset for help on using the changeset viewer.