Changeset fa18523 in mainline for uspace/app/date/date.c
- Timestamp:
- 2012-04-25T11:31:10Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- af7e3d3
- Parents:
- 6a3808e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/date/date.c
r6a3808e rfa18523 43 43 static int read_time_from_arg(char *wdate, struct tm *t); 44 44 static int read_num_from_str(char *str, size_t len, int *n); 45 static int tm_sanity_check(struct tm *t); 46 static bool is_leap_year(int year); 47 45 48 static void usage(void); 49 50 static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 46 51 47 52 int … … 181 186 } 182 187 188 rc = tm_sanity_check(&t); 189 if (rc != EOK) { 190 printf(NAME ": error, invalid date/time\n"); 191 goto exit; 192 } 193 183 194 rc = clock_dev_time_set(sess, &t); 184 195 if (rc != EOK) { … … 278 289 } 279 290 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 */ 297 static int 298 tm_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 */ 332 static bool 333 is_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 280 347 static void 281 348 usage(void)
Note:
See TracChangeset
for help on using the changeset viewer.