Changeset 3f466c33 in mainline for uspace/lib/posix/time.c


Ignore:
Timestamp:
2011-07-07T22:59:42Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ec18957a
Parents:
324d46b
Message:

More additions to time.h (still WIP)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/time.c

    r324d46b r3f466c33  
    4444#include "ctype.h"
    4545#include "errno.h"
     46#include "signal.h"
    4647
    4748#include "libc/malloc.h"
    4849#include "libc/task.h"
    4950#include "libc/stats.h"
     51#include "libc/sys/time.h"
    5052
    5153// TODO: documentation
     
    212214        /* Now the difficult part - days of month. */
    213215        /* Slow, but simple. */
    214         // TODO: do this faster
     216        // FIXME: do this faster
    215217
    216218        while (tm->tm_mday < 1) {
     
    253255static int _wbyear(const struct posix_tm *tm)
    254256{
    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. */
    256260                return tm->tm_year - 1;
    257261        }
    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. */
    260264                return tm->tm_year + 1;
    261265        }
     266        /* All the other days are in the calendar year. */
    262267        return tm->tm_year;
    263268}
    264269
    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).
    266276 */
    267277static int _sun_week_number(const struct posix_tm *tm)
    268278{
    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).
    274291 */
    275292static int _iso_week_number(const struct posix_tm *tm)
    276293{
    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).
    282313 */
    283314static int _mon_week_number(const struct posix_tm *tm)
    284315{
    285         // TODO
    286         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;
    287318}
    288319
     
    327358}
    328359
    329 /**
    330  *
    331  * @param timep
    332  * @return
    333  */
    334 struct posix_tm *posix_localtime(const time_t *timep)
     360struct posix_tm *posix_gmtime(const time_t *timer)
    335361{
    336362        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
     366struct posix_tm *posix_gmtime_r(const time_t *restrict timer,
    341367    struct posix_tm *restrict result)
    342368{
    343369        assert(timer != NULL);
    344370        assert(result != NULL);
    345 
    346         // TODO: deal with timezone
    347         // currently assumes system and all times are in GMT
    348371
    349372        /* Set epoch and seconds to _long_tm struct and normalize to get
     
    371394/**
    372395 *
     396 * @param timep
     397 * @return
     398 */
     399struct posix_tm *posix_localtime(const time_t *timer)
     400{
     401        static struct posix_tm result;
     402        return posix_localtime_r(timer, &result);
     403}
     404
     405struct 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 *
    373415 * @param tm
    374416 * @return
     
    409451 * @return
    410452 */
    411 char *posix_ctime(const time_t *timep)
    412 {
    413         return posix_asctime(posix_localtime(timep));
     453char *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
     462char *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);
    414469}
    415470
     
    600655}
    601656
     657int 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
     672int 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
     690int 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
     708int 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
     732struct __posix_timer {
     733        posix_clockid_t clockid;
     734        struct posix_sigevent evp;
     735};
     736
     737int 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
     745int posix_timer_delete(posix_timer_t timerid)
     746{
     747        // TODO
     748        not_implemented();
     749}
     750
     751int posix_timer_getoverrun(posix_timer_t timerid)
     752{
     753        // TODO
     754        not_implemented();
     755}
     756
     757int posix_timer_gettime(posix_timer_t timerid,
     758    struct posix_itimerspec *value)
     759{
     760        // TODO
     761        not_implemented();
     762}
     763
     764int 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
    602774/**
    603775 * Get CPU time used since the process invocation.
Note: See TracChangeset for help on using the changeset viewer.