Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 3f466c33 in mainline


Ignore:
Timestamp:
2011-07-07T22:59:42Z (10 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master
Children:
ec18957a
Parents:
324d46b
Message:

More additions to time.h (still WIP)

Location:
uspace/lib/posix
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/signal.h

    r324d46b r3f466c33  
    284284        #define sigset_t posix_sigset_t
    285285        #define sigval posix_sigval
    286         #define sigevent posix_sigevent
     286        #ifndef sigevent
     287                #define sigevent posix_sigevent
     288        #endif
    287289        #define sigaction posix_sigaction
    288290        #define mcontext_t posix_mcontext_t
  • uspace/lib/posix/sys/types.h

    r324d46b r3f466c33  
    5252typedef struct posix_thread_attr posix_thread_attr_t;
    5353
     54/* Clock types */
     55typedef long posix_clock_t;
     56typedef int posix_clockid_t;
     57
    5458#ifndef LIBPOSIX_INTERNAL
    5559        #define ino_t posix_ino_t
     
    6468       
    6569        #define pthread_attr_t posix_thread_attr_t
     70       
     71        #define clock_t posix_clock_t
     72        #define clockid_t posix_clockid_t
    6673#endif
    6774
  • 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.
  • uspace/lib/posix/time.h

    r324d46b r3f466c33  
    5656#endif
    5757
     58#ifndef POSIX_SIGNAL_H_
     59        struct posix_sigevent;
     60        #ifndef LIBPOSIX_INTERNAL
     61                #define sigevent posix_sigevent
     62        #endif
     63#endif
     64
    5865#undef ASCTIME_BUF_LEN
    5966#define ASCTIME_BUF_LEN 26
     
    7481};
    7582
    76 // FIXME: should be in sys/types.h
    77 typedef long posix_clock_t;
    78 
    7983struct posix_timespec {
    8084        time_t tv_sec; /* Seconds. */
     
    8791};
    8892
     93typedef struct __posix_timer *posix_timer_t;
     94
    8995/* Timezones */
    9096
     
    101107/* Broken-down Time */
    102108extern time_t posix_mktime(struct posix_tm *timeptr);
    103 extern struct posix_tm *posix_localtime(const time_t *timep);
     109extern struct posix_tm *posix_gmtime(const time_t *timer);
     110extern struct posix_tm *posix_gmtime_r(const time_t *restrict timer,
     111    struct posix_tm *restrict result);
     112extern struct posix_tm *posix_localtime(const time_t *timer);
    104113extern struct posix_tm *posix_localtime_r(const time_t *restrict timer,
    105114    struct posix_tm *restrict result);
     115
    106116/* Formatting Calendar Time */
    107117extern char *posix_asctime(const struct posix_tm *timeptr);
    108118extern char *posix_asctime_r(const struct posix_tm *restrict timeptr,
    109119    char *restrict buf);
    110 extern char *posix_ctime(const time_t *timep);
     120extern char *posix_ctime(const time_t *timer);
     121extern char *posix_ctime_r(const time_t *timer, char *buf);
     122
    111123extern size_t posix_strftime(char *restrict s, size_t maxsize,
    112124    const char *restrict format, const struct posix_tm *restrict tm);
    113125
     126extern size_t posix_strftime_l(char *restrict s, size_t maxsize,
     127    const char *restrict format, const struct posix_tm *restrict tm,
     128    posix_locale_t loc);
     129
     130/* Clocks. */
     131
     132extern int posix_clock_getres(posix_clockid_t clock_id,
     133    struct posix_timespec *res);
     134extern int posix_clock_gettime(posix_clockid_t clock_id,
     135    struct posix_timespec *tp);
     136extern int posix_clock_settime(posix_clockid_t clock_id,
     137    const struct posix_timespec *tp);
     138extern int posix_clock_nanosleep(posix_clockid_t clock_id, int flags,
     139    const struct posix_timespec *rqtp, struct posix_timespec *rmtp);
     140
     141/* Timers. */
     142
     143#if 0
     144
     145extern int posix_timer_create(posix_clockid_t clockid,
     146    struct posix_sigevent *restrict evp,
     147    posix_timer_t *restrict timerid);
     148extern int posix_timer_delete(posix_timer_t timerid);
     149extern int posix_timer_getoverrun(posix_timer_t timerid);
     150extern int posix_timer_gettime(posix_timer_t timerid,
     151    struct posix_itimerspec *value);
     152extern int posix_timer_settime(posix_timer_t timerid, int flags,
     153    const struct posix_itimerspec *restrict value,
     154    struct posix_itimerspec *restrict ovalue);
     155
     156#endif
     157
    114158/* CPU Time */
    115159extern posix_clock_t posix_clock(void);
     
    119163        #define tm posix_tm
    120164
    121         #define clock_t posix_clock_t
    122165        #define timespec posix_timespec
    123166        #define itimerspec posix_itimerspec
     167        #define timer_t posix_timer_t
    124168
    125169        #define difftime posix_difftime
    126170        #define mktime posix_mktime
     171        #define gmtime posix_gmtime
     172        #define gmtime_r posix_gmtime_r
    127173        #define localtime posix_localtime
    128174        #define localtime_r posix_localtime_r
     
    136182        #define asctime_r posix_asctime_r
    137183        #define ctime posix_ctime
     184        #define ctime_r posix_ctime_r
    138185        #define strftime posix_strftime
    139186
     187        #define clock_getres posix_clock_getres
     188        #define clock_gettime posix_clock_gettime
     189        #define clock_settime posix_clock_settime
     190        #define clock_nanosleep posix_clock_nanosleep
     191       
     192        #define timer_create posix_timer_create
     193        #define timer_delete posix_timer_delete
     194        #define timer_getoverrun posix_timer_getoverrun
     195        #define timer_gettime posix_timer_gettime
     196        #define timer_settime posix_timer_settime
     197       
    140198        #define clock posix_clock
    141199#endif
Note: See TracChangeset for help on using the changeset viewer.