Changeset fa18523 in mainline
- 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
- Location:
- uspace
- Files:
-
- 2 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) -
uspace/drv/time/cmos-rtc/cmos-rtc.c
r6a3808e rfa18523 90 90 ipc_callid_t callid, ipc_call_t *call); 91 91 static int rtc_dev_remove(ddf_dev_t *dev); 92 static int rtc_tm_sanity_check(struct tm *t, int epoch);93 92 static void rtc_register_write(rtc_t *rtc, int reg, int data); 94 static bool is_leap_year(int year);95 96 static int days_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};97 93 98 94 static ddf_dev_ops_t rtc_dev_ops; … … 287 283 bool bcd_mode; 288 284 bool pm_mode = false; 289 int epoch = 1900;290 285 rtc_t *rtc = RTC_FROM_FNODE(fun); 291 286 … … 352 347 * RTC epoch is 2000. 353 348 */ 354 epoch = 2000;355 349 t->tm_year += 100; 356 350 } … … 358 352 fibril_mutex_unlock(&rtc->mutex); 359 353 360 return rtc_tm_sanity_check(t, epoch); 354 time_t r = mktime(t); 355 if (r < 0) 356 return EINVAL; 357 358 return EOK; 361 359 } 362 360 … … 371 369 rtc_time_set(ddf_fun_t *fun, struct tm *t) 372 370 { 373 int rc;374 371 bool bcd_mode; 375 372 int reg_b; … … 378 375 rtc_t *rtc = RTC_FROM_FNODE(fun); 379 376 377 if (mktime(t) < 0) 378 return EINVAL; 379 380 380 fibril_mutex_lock(&rtc->mutex); 381 381 … … 386 386 epoch = 1900; 387 387 388 rc = rtc_tm_sanity_check(t, epoch);389 if (rc != EOK) {388 if (epoch == 2000 && t->tm_year < 100) { 389 /* Can't set a year before the epoch */ 390 390 fibril_mutex_unlock(&rtc->mutex); 391 return rc;391 return EINVAL; 392 392 } 393 393 … … 442 442 fibril_mutex_unlock(&rtc->mutex); 443 443 444 return rc;445 }446 447 /** Check if the tm structure contains valid values448 *449 * @param t The tm structure to check450 * @param epoch The RTC epoch year451 *452 * @return EOK on success or EINVAL453 */454 static int455 rtc_tm_sanity_check(struct tm *t, int epoch)456 {457 int ndays;458 459 if (t->tm_sec < 0 || t->tm_sec > 59)460 return EINVAL;461 else if (t->tm_min < 0 || t->tm_min > 59)462 return EINVAL;463 else if (t->tm_hour < 0 || t->tm_hour > 23)464 return EINVAL;465 else if (t->tm_mday < 1 || t->tm_mday > 31)466 return EINVAL;467 else if (t->tm_mon < 0 || t->tm_mon > 11)468 return EINVAL;469 else if (epoch == 2000 && t->tm_year < 100)470 return EINVAL;471 else if (t->tm_year < 0 || t->tm_year > 199)472 return EINVAL;473 474 if (t->tm_mon == 1/* FEB */ && is_leap_year(t->tm_year))475 ndays = 29;476 else477 ndays = days_month[t->tm_mon];478 479 if (t->tm_mday > ndays)480 return EINVAL;481 482 444 return EOK; 483 }484 485 /** Check if a year is a leap year486 *487 * @param year The year to check488 *489 * @return true if it is a leap year, false otherwise490 */491 static bool492 is_leap_year(int year)493 {494 bool r = false;495 496 if (year % 4 == 0) {497 if (year % 100 == 0)498 r = year % 400 == 0;499 else500 r = true;501 }502 503 return r;504 445 } 505 446
Note:
See TracChangeset
for help on using the changeset viewer.