Changeset 3f466c33 in mainline for uspace/lib/posix/time.c
- Timestamp:
- 2011-07-07T22:59:42Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ec18957a
- Parents:
- 324d46b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/time.c
r324d46b r3f466c33 44 44 #include "ctype.h" 45 45 #include "errno.h" 46 #include "signal.h" 46 47 47 48 #include "libc/malloc.h" 48 49 #include "libc/task.h" 49 50 #include "libc/stats.h" 51 #include "libc/sys/time.h" 50 52 51 53 // TODO: documentation … … 212 214 /* Now the difficult part - days of month. */ 213 215 /* Slow, but simple. */ 214 // TODO: do this faster216 // FIXME: do this faster 215 217 216 218 while (tm->tm_mday < 1) { … … 253 255 static int _wbyear(const struct posix_tm *tm) 254 256 { 255 if (tm->tm_yday < _wbyear_offset(tm->tm_year)) { 257 int day = tm->tm_yday - _wbyear_offset(tm->tm_year); 258 if (day < 0) { 259 /* Last week of previous year. */ 256 260 return tm->tm_year - 1; 257 261 } 258 if ( tm->tm_yday > (364 + _is_leap_year(tm->tm_year) +259 _wbyear_offset(tm->tm_year + 1))) {262 if (day > 364 + _is_leap_year(tm->tm_year)){ 263 /* First week of next year. */ 260 264 return tm->tm_year + 1; 261 265 } 266 /* All the other days are in the calendar year. */ 262 267 return tm->tm_year; 263 268 } 264 269 265 /* Number of week in year, when week starts on sunday. 270 /** Week number of the year, assuming weeks start on sunday. 271 * The first Sunday of January is the first day of week 1; 272 * days in the new year before this are in week 0. 273 * 274 * @param tm Normalized broken-down time. 275 * @return The week number (0 - 53). 266 276 */ 267 277 static int _sun_week_number(const struct posix_tm *tm) 268 278 { 269 // TODO 270 not_implemented(); 271 } 272 273 /* Number of week in week-based year. 279 int first_day = (7 - _day_of_week(tm->tm_year, 0, 1)) % 7; 280 return (tm->tm_yday - first_day + 7) / 7; 281 } 282 283 /** Week number of the year, assuming weeks start on monday. 284 * If the week containing January 1st has four or more days in the new year, 285 * then it is considered week 1. Otherwise, it is the last week of the previous 286 * year, and the next week is week 1. Both January 4th and the first Thursday 287 * of January are always in week 1. 288 * 289 * @param tm Normalized broken-down time. 290 * @return The week number (1 - 53). 274 291 */ 275 292 static int _iso_week_number(const struct posix_tm *tm) 276 293 { 277 // TODO 278 not_implemented(); 279 } 280 281 /* Number of week in year, when week starts on monday. 294 int day = tm->tm_yday - _wbyear_offset(tm->tm_year); 295 if (day < 0) { 296 /* Last week of previous year. */ 297 return 53; 298 } 299 if (day > 364 + _is_leap_year(tm->tm_year)){ 300 /* First week of next year. */ 301 return 1; 302 } 303 /* All the other days give correct answer. */ 304 return (day / 7 + 1); 305 } 306 307 /** Week number of the year, assuming weeks start on monday. 308 * The first Monday of January is the first day of week 1; 309 * days in the new year before this are in week 0. 310 * 311 * @param tm Normalized broken-down time. 312 * @return The week number (0 - 53). 282 313 */ 283 314 static int _mon_week_number(const struct posix_tm *tm) 284 315 { 285 // TODO286 not_implemented();316 int first_day = (1 - _day_of_week(tm->tm_year, 0, 1)) % 7; 317 return (tm->tm_yday - first_day + 7) / 7; 287 318 } 288 319 … … 327 358 } 328 359 329 /** 330 * 331 * @param timep 332 * @return 333 */ 334 struct posix_tm *posix_localtime(const time_t *timep) 360 struct posix_tm *posix_gmtime(const time_t *timer) 335 361 { 336 362 static struct posix_tm result; 337 return posix_ localtime_r(timep, &result);338 } 339 340 struct posix_tm *posix_ localtime_r(const time_t *restrict timer,363 return posix_gmtime_r(timer, &result); 364 } 365 366 struct posix_tm *posix_gmtime_r(const time_t *restrict timer, 341 367 struct posix_tm *restrict result) 342 368 { 343 369 assert(timer != NULL); 344 370 assert(result != NULL); 345 346 // TODO: deal with timezone347 // currently assumes system and all times are in GMT348 371 349 372 /* Set epoch and seconds to _long_tm struct and normalize to get … … 371 394 /** 372 395 * 396 * @param timep 397 * @return 398 */ 399 struct posix_tm *posix_localtime(const time_t *timer) 400 { 401 static struct posix_tm result; 402 return posix_localtime_r(timer, &result); 403 } 404 405 struct posix_tm *posix_localtime_r(const time_t *restrict timer, 406 struct posix_tm *restrict result) 407 { 408 // TODO: deal with timezone 409 // currently assumes system and all times are in GMT 410 return posix_gmtime_r(timer, result); 411 } 412 413 /** 414 * 373 415 * @param tm 374 416 * @return … … 409 451 * @return 410 452 */ 411 char *posix_ctime(const time_t *timep) 412 { 413 return posix_asctime(posix_localtime(timep)); 453 char *posix_ctime(const time_t *timer) 454 { 455 struct posix_tm *loctime = posix_localtime(timer); 456 if (loctime == NULL) { 457 return NULL; 458 } 459 return posix_asctime(loctime); 460 } 461 462 char *posix_ctime_r(const time_t *timer, char *buf) 463 { 464 struct posix_tm loctime; 465 if (posix_localtime_r(timer, &loctime) == NULL) { 466 return NULL; 467 } 468 return posix_asctime_r(&loctime, buf); 414 469 } 415 470 … … 600 655 } 601 656 657 int posix_clock_getres(posix_clockid_t clock_id, struct posix_timespec *res) 658 { 659 assert(res != NULL); 660 661 switch (clock_id) { 662 case CLOCK_REALTIME: 663 res->tv_sec = 0; 664 res->tv_nsec = 1000; /* Microsecond resolution. */ 665 return 0; 666 default: 667 errno = EINVAL; 668 return -1; 669 } 670 } 671 672 int posix_clock_gettime(posix_clockid_t clock_id, struct posix_timespec *tp) 673 { 674 assert(tp != NULL); 675 676 switch (clock_id) { 677 case CLOCK_REALTIME: 678 ; 679 struct timeval tv; 680 gettimeofday(&tv, NULL); 681 tp->tv_sec = tv.tv_sec; 682 tp->tv_nsec = tv.tv_usec * 1000; 683 return 0; 684 default: 685 errno = EINVAL; 686 return -1; 687 } 688 } 689 690 int posix_clock_settime(posix_clockid_t clock_id, 691 const struct posix_timespec *tp) 692 { 693 assert(tp != NULL); 694 695 switch (clock_id) { 696 case CLOCK_REALTIME: 697 // TODO: setting clock 698 // FIXME: HelenOS doesn't actually support hardware 699 // clock yet 700 errno = EPERM; 701 return -1; 702 default: 703 errno = EINVAL; 704 return -1; 705 } 706 } 707 708 int posix_clock_nanosleep(posix_clockid_t clock_id, int flags, 709 const struct posix_timespec *rqtp, struct posix_timespec *rmtp) 710 { 711 assert(rqtp != NULL); 712 assert(rmtp != NULL); 713 714 switch (clock_id) { 715 case CLOCK_REALTIME: 716 // TODO: interruptible sleep 717 if (rqtp->tv_sec != 0) { 718 sleep(rqtp->tv_sec); 719 } 720 if (rqtp->tv_nsec != 0) { 721 usleep(rqtp->tv_nsec / 1000); 722 } 723 return 0; 724 default: 725 errno = EINVAL; 726 return -1; 727 } 728 } 729 730 #if 0 731 732 struct __posix_timer { 733 posix_clockid_t clockid; 734 struct posix_sigevent evp; 735 }; 736 737 int posix_timer_create(posix_clockid_t clockid, 738 struct posix_sigevent *restrict evp, 739 posix_timer_t *restrict timerid) 740 { 741 // TODO 742 not_implemented(); 743 } 744 745 int posix_timer_delete(posix_timer_t timerid) 746 { 747 // TODO 748 not_implemented(); 749 } 750 751 int posix_timer_getoverrun(posix_timer_t timerid) 752 { 753 // TODO 754 not_implemented(); 755 } 756 757 int posix_timer_gettime(posix_timer_t timerid, 758 struct posix_itimerspec *value) 759 { 760 // TODO 761 not_implemented(); 762 } 763 764 int posix_timer_settime(posix_timer_t timerid, int flags, 765 const struct posix_itimerspec *restrict value, 766 struct posix_itimerspec *restrict ovalue) 767 { 768 // TODO 769 not_implemented(); 770 } 771 772 #endif 773 602 774 /** 603 775 * Get CPU time used since the process invocation.
Note:
See TracChangeset
for help on using the changeset viewer.