Changeset 58775d30 in mainline for uspace/lib/c/generic/time.c
- Timestamp:
- 2015-03-16T16:07:21Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 2003739
- Parents:
- 6069061 (diff), 795e2bf (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/time.c
r6069061 r58775d30 59 59 #define MINS_PER_HOUR 60 60 60 #define SECS_PER_MIN 60 61 #define USECS_PER_SEC 1000000 61 62 #define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY) 62 63 #define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR) … … 252 253 * Optionally add specified amount of seconds. 253 254 * 254 * @param tm 255 * @param sec_add Secondsto add.255 * @param tm Broken-down time to normalize. 256 * @param tv Timeval to add. 256 257 * 257 258 * @return 0 on success, -1 on overflow 258 259 * 259 260 */ 260 static int normalize_t ime(struct tm *tm, time_t sec_add)261 static int normalize_tm_tv(struct tm *tm, const struct timeval *tv) 261 262 { 262 263 // TODO: DST correction 263 264 264 265 /* Set initial values. */ 265 time_t sec = tm->tm_sec + sec_add; 266 time_t usec = tm->tm_usec + tv->tv_usec; 267 time_t sec = tm->tm_sec + tv->tv_sec; 266 268 time_t min = tm->tm_min; 267 269 time_t hour = tm->tm_hour; … … 271 273 272 274 /* Adjust time. */ 275 sec += floor_div(usec, USECS_PER_SEC); 276 usec = floor_mod(usec, USECS_PER_SEC); 273 277 min += floor_div(sec, SECS_PER_MIN); 274 278 sec = floor_mod(sec, SECS_PER_MIN); … … 319 323 320 324 /* And put the values back to the struct. */ 325 tm->tm_usec = (int) usec; 321 326 tm->tm_sec = (int) sec; 322 327 tm->tm_min = (int) min; … … 335 340 } 336 341 342 static int normalize_tm_time(struct tm *tm, time_t time) 343 { 344 struct timeval tv = { 345 .tv_sec = time, 346 .tv_usec = 0 347 }; 348 349 return normalize_tm_tv(tm, &tv); 350 } 351 352 337 353 /** Which day the week-based year starts on. 338 354 * … … 442 458 } 443 459 460 static void tv_normalize(struct timeval *tv) 461 { 462 while (tv->tv_usec > USECS_PER_SEC) { 463 tv->tv_sec++; 464 tv->tv_usec -= USECS_PER_SEC; 465 } 466 while (tv->tv_usec < 0) { 467 tv->tv_sec--; 468 tv->tv_usec += USECS_PER_SEC; 469 } 470 } 471 444 472 /** Add microseconds to given timeval. 445 473 * … … 448 476 * 449 477 */ 450 void tv_add(struct timeval *tv, suseconds_t usecs) 451 { 452 tv->tv_sec += usecs / 1000000; 453 tv->tv_usec += usecs % 1000000; 454 455 if (tv->tv_usec > 1000000) { 456 tv->tv_sec++; 457 tv->tv_usec -= 1000000; 458 } 459 } 460 461 /** Subtract two timevals. 478 void tv_add_diff(struct timeval *tv, suseconds_t usecs) 479 { 480 tv->tv_sec += usecs / USECS_PER_SEC; 481 tv->tv_usec += usecs % USECS_PER_SEC; 482 tv_normalize(tv); 483 } 484 485 /** Add two timevals. 462 486 * 463 487 * @param tv1 First timeval. 464 488 * @param tv2 Second timeval. 489 */ 490 void tv_add(struct timeval *tv1, struct timeval *tv2) 491 { 492 tv1->tv_sec += tv2->tv_sec; 493 tv1->tv_usec += tv2->tv_usec; 494 tv_normalize(tv1); 495 } 496 497 /** Subtract two timevals. 498 * 499 * @param tv1 First timeval. 500 * @param tv2 Second timeval. 465 501 * 466 502 * @return Difference between tv1 and tv2 (tv1 - tv2) in … … 468 504 * 469 505 */ 470 suseconds_t tv_sub (struct timeval *tv1, struct timeval *tv2)506 suseconds_t tv_sub_diff(struct timeval *tv1, struct timeval *tv2) 471 507 { 472 508 return (tv1->tv_usec - tv2->tv_usec) + 473 ((tv1->tv_sec - tv2->tv_sec) * 1000000); 509 ((tv1->tv_sec - tv2->tv_sec) * USECS_PER_SEC); 510 } 511 512 /** Subtract two timevals. 513 * 514 * @param tv1 First timeval. 515 * @param tv2 Second timeval. 516 * 517 */ 518 void tv_sub(struct timeval *tv1, struct timeval *tv2) 519 { 520 tv1->tv_sec -= tv2->tv_sec; 521 tv1->tv_usec -= tv2->tv_usec; 522 tv_normalize(tv1); 474 523 } 475 524 … … 573 622 goto fallback; 574 623 575 tv->tv_usec = 0;624 tv->tv_usec = time.tm_usec; 576 625 tv->tv_sec = mktime(&time); 577 626 … … 689 738 // TODO: detect overflow 690 739 691 normalize_t ime(tm, 0);740 normalize_tm_time(tm, 0); 692 741 return secs_since_epoch(tm); 693 742 } … … 944 993 945 994 /* Set result to epoch. */ 995 result->tm_usec = 0; 946 996 result->tm_sec = 0; 947 997 result->tm_min = 0; … … 951 1001 result->tm_year = 70; /* 1970 */ 952 1002 953 if (normalize_t ime(result, time) == -1)1003 if (normalize_tm_time(result, time) == -1) 954 1004 return EOVERFLOW; 955 1005 … … 1014 1064 * Time is expressed relative to the user's specified timezone. 1015 1065 * 1016 * @param t imer Timeto convert.1066 * @param tv Timeval to convert. 1017 1067 * @param result Structure to store the result to. 1018 1068 * … … 1020 1070 * 1021 1071 */ 1022 int time_ local2tm(const time_t time, struct tm *restrict result)1072 int time_tv2tm(const struct timeval *tv, struct tm *restrict result) 1023 1073 { 1024 1074 // TODO: Deal with timezones. … … 1026 1076 1027 1077 /* Set result to epoch. */ 1078 result->tm_usec = 0; 1028 1079 result->tm_sec = 0; 1029 1080 result->tm_min = 0; … … 1033 1084 result->tm_year = 70; /* 1970 */ 1034 1085 1035 if (normalize_t ime(result, time) == -1)1086 if (normalize_tm_tv(result, tv) == -1) 1036 1087 return EOVERFLOW; 1037 1088 1038 1089 return EOK; 1090 } 1091 1092 /** Converts a time value to a broken-down local time. 1093 * 1094 * Time is expressed relative to the user's specified timezone. 1095 * 1096 * @param timer Time to convert. 1097 * @param result Structure to store the result to. 1098 * 1099 * @return EOK on success or a negative error code. 1100 * 1101 */ 1102 int time_local2tm(const time_t time, struct tm *restrict result) 1103 { 1104 struct timeval tv = { 1105 .tv_sec = time, 1106 .tv_usec = 0 1107 }; 1108 1109 return time_tv2tm(&tv, result); 1039 1110 } 1040 1111
Note:
See TracChangeset
for help on using the changeset viewer.