[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH v3 1/3] lib/uktime: Import timegm from musl


  • To: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>, "minios-devel@xxxxxxxxxxxxx" <minios-devel@xxxxxxxxxxxxx>
  • From: Costin Lupu <costin.lupu@xxxxxxxxx>
  • Date: Thu, 12 Sep 2019 14:51:45 +0300
  • Delivery-date: Thu, 12 Sep 2019 11:51:55 +0000
  • Ironport-phdr: 9a23:OFKfDRGTojR1lIpyBNvaXJ1GYnF86YWxBRYc798ds5kLTJ76p8mybnLW6fgltlLVR4KTs6sC17ON9fi9EjBbqdbZ6TZeKcYKD0dEwewt3CUYSPafDkP6KPO4JwcbJ+9lEGFfwnegLEJOE9z/bVCB6le77DoVBwmtfVEtfre9FYHdldm42P6v8JPPfQpImCC9YbRvJxmqsAndrMYbjZZsJ6or1hfEonREdutYyGh1IV6fgwvw6t2/8ZJ+7Shcoe4t+9JFXa7nY6k2ULtUASg8PWso/sPrrx7DTQWO5nsYTGoblwdDDhbG4h/nQJr/qzP2ueVh1iaUO832Vq00Vi+576h3Uh/oiTwIOCA//WrKl8F/lqNboBampxxi347ZZZyeOfRicq/Be94RWHFMVdhNWSNfHoy8bpMPD+sfMuZes4n9vEYFoR+nCQWxGO/j1jpEi3nr1qM4zushCxnL0hE+EdIAsHrar9v7O6kdXu+30KbGwi7Ob+9P2Trn9ITEbh4srPOKULltccTR004vFwbdg1iOrozlJC+V1uULs2OG6eRgSPqvi3M6oAx2vjevwdwsipXIhoIT0FzE8Th5wJ4oJdCjVEF7esSoEINNty6BLYd2WMQiTnhutS0nxLMGvpu7czILyJQh3xPfbuGHc5OM4h76UOaRICx3i25/dLK4nRay/k6twfD/WMmsyFtGsyVInsTWun0O1xHf8NaLRuV/80u71zuDzx3f5+JHLEwui6bWK4QtzqA/m5cTq0jOESv7lUPrh6GMbEok4PKn6+H/b7XjoZ+TKpF7hxnlMqQrhsy/GeM4MhUSX2SD+eSzyrnj/UrhTbVUi/05iKjZsJTAKcsHva61GRdV0ps45BqlFTem18wUnXgBLF1bZBKKl5XlNl7TLPziDvqyg06gnCl1y/3EJLHsDI3BLn3Zn7fgebZ95VRcyA02zd1H+51UDaoBIO7yWk/3rtDYCgI5MxSpw+v8FNp9zZkSWXqSAqCCKq/Sq1iI6vo1I+aQfI8VpCr9K/896v7rjH85n14dfa6y3ZcOdX+3AOpmLFudYXf3htcBEHwKvhYlTODwh1yNTzhTa2y1X6Im6TExEJimApvbRoCxnLyB2z+2HoNSZm9cF1+DCW3nd4OZVPcWdi2SI9VtnSceWre8TI8h1AyuuRThy7p7M+rb5zcUuoj/29hu/eHTiAky+iduA8SH1GHeB11zy10MTjNz9aZzu0F8/X0o/Y9ZvssYSowNtqkIGlMkMZfQ+OV8B8r/W0TLd9uOSUuOWcm7DXc4Rcl33scEJVx6TZHqxBXC2SutGPoZmqKGALQw87nAxD7hKsA7zGzJnuF1ilggQ8xUcGGrmKN73wzSHJLS1VWUkeCtb6tKjwDX82LW5m2VoEBeGCptSbiNCXsYfVfXq5L9+1vfZ7S1T6w6OE1bzpjReeNxdtT1gAAeF7/YM9PEbjf0wj/oCA==
  • Ironport-sdr: B5AtynlTI0NdBJPxUQHMLbWfitPXqSBYBoCSz1534KBYdbrnpjCm9NHBOhthZzxX06HaW1e5d5 yhbU6OdDiJkw==
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>

Thanks for the patch, Vlad!

Reviewed-by: Costin Lupu <costin.lupu@xxxxxxxxx>

On 9/12/19 2:31 PM, Vlad-Andrei BĂDOIU (78692) wrote:
> Copied as is from the official musl git mirror
> git://git.musl-libc.org/musl, commit 6fcb440d.
> 
> Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
> ---
>  lib/uktime/musl-imported/include/time.h       | 138 ++++++++++++++++++
>  .../musl-imported/src/__month_to_secs.c       |  10 ++
>  lib/uktime/musl-imported/src/__secs_to_tm.c   |  82 +++++++++++
>  lib/uktime/musl-imported/src/__tm_to_secs.c   |  24 +++
>  lib/uktime/musl-imported/src/__year_to_secs.c |  47 ++++++
>  lib/uktime/musl-imported/src/time_impl.h      |  11 ++
>  lib/uktime/musl-imported/src/timegm.c         |  18 +++
>  7 files changed, 330 insertions(+)
>  create mode 100644 lib/uktime/musl-imported/include/time.h
>  create mode 100644 lib/uktime/musl-imported/src/__month_to_secs.c
>  create mode 100644 lib/uktime/musl-imported/src/__secs_to_tm.c
>  create mode 100644 lib/uktime/musl-imported/src/__tm_to_secs.c
>  create mode 100644 lib/uktime/musl-imported/src/__year_to_secs.c
>  create mode 100644 lib/uktime/musl-imported/src/time_impl.h
>  create mode 100644 lib/uktime/musl-imported/src/timegm.c
> 
> diff --git a/lib/uktime/musl-imported/include/time.h 
> b/lib/uktime/musl-imported/include/time.h
> new file mode 100644
> index 00000000..672b3fc3
> --- /dev/null
> +++ b/lib/uktime/musl-imported/include/time.h
> @@ -0,0 +1,138 @@
> +#ifndef      _TIME_H
> +#define _TIME_H
> +
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +#include <features.h>
> +
> +#ifdef __cplusplus
> +#define NULL 0L
> +#else
> +#define NULL ((void*)0)
> +#endif
> +
> +
> +#define __NEED_size_t
> +#define __NEED_time_t
> +#define __NEED_clock_t
> +#define __NEED_struct_timespec
> +
> +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
> + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> + || defined(_BSD_SOURCE)
> +#define __NEED_clockid_t
> +#define __NEED_timer_t
> +#define __NEED_pid_t
> +#define __NEED_locale_t
> +#endif
> +
> +#include <bits/alltypes.h>
> +
> +#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
> +#define __tm_gmtoff tm_gmtoff
> +#define __tm_zone tm_zone
> +#endif
> +
> +struct tm {
> +     int tm_sec;
> +     int tm_min;
> +     int tm_hour;
> +     int tm_mday;
> +     int tm_mon;
> +     int tm_year;
> +     int tm_wday;
> +     int tm_yday;
> +     int tm_isdst;
> +     long __tm_gmtoff;
> +     const char *__tm_zone;
> +};
> +
> +clock_t clock (void);
> +time_t time (time_t *);
> +double difftime (time_t, time_t);
> +time_t mktime (struct tm *);
> +size_t strftime (char *__restrict, size_t, const char *__restrict, const 
> struct tm *__restrict);
> +struct tm *gmtime (const time_t *);
> +struct tm *localtime (const time_t *);
> +char *asctime (const struct tm *);
> +char *ctime (const time_t *);
> +int timespec_get(struct timespec *, int);
> +
> +#define CLOCKS_PER_SEC 1000000L
> +
> +#define TIME_UTC 1
> +
> +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
> + || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
> + || defined(_BSD_SOURCE)
> +
> +size_t strftime_l (char *  __restrict, size_t, const char *  __restrict, 
> const struct tm *  __restrict, locale_t);
> +
> +struct tm *gmtime_r (const time_t *__restrict, struct tm *__restrict);
> +struct tm *localtime_r (const time_t *__restrict, struct tm *__restrict);
> +char *asctime_r (const struct tm *__restrict, char *__restrict);
> +char *ctime_r (const time_t *, char *);
> +
> +void tzset (void);
> +
> +struct itimerspec {
> +     struct timespec it_interval;
> +     struct timespec it_value;
> +};
> +
> +#define CLOCK_REALTIME           0
> +#define CLOCK_MONOTONIC          1
> +#define CLOCK_PROCESS_CPUTIME_ID 2
> +#define CLOCK_THREAD_CPUTIME_ID  3
> +#define CLOCK_MONOTONIC_RAW      4
> +#define CLOCK_REALTIME_COARSE    5
> +#define CLOCK_MONOTONIC_COARSE   6
> +#define CLOCK_BOOTTIME           7
> +#define CLOCK_REALTIME_ALARM     8
> +#define CLOCK_BOOTTIME_ALARM     9
> +#define CLOCK_SGI_CYCLE         10
> +#define CLOCK_TAI               11
> +
> +#define TIMER_ABSTIME 1
> +
> +int nanosleep (const struct timespec *, struct timespec *);
> +int clock_getres (clockid_t, struct timespec *);
> +int clock_gettime (clockid_t, struct timespec *);
> +int clock_settime (clockid_t, const struct timespec *);
> +int clock_nanosleep (clockid_t, int, const struct timespec *, struct 
> timespec *);
> +int clock_getcpuclockid (pid_t, clockid_t *);
> +
> +struct sigevent;
> +int timer_create (clockid_t, struct sigevent *__restrict, timer_t 
> *__restrict);
> +int timer_delete (timer_t);
> +int timer_settime (timer_t, int, const struct itimerspec *__restrict, struct 
> itimerspec *__restrict);
> +int timer_gettime (timer_t, struct itimerspec *);
> +int timer_getoverrun (timer_t);
> +
> +extern char *tzname[2];
> +
> +#endif
> +
> +
> +#if defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
> +char *strptime (const char *__restrict, const char *__restrict, struct tm 
> *__restrict);
> +extern int daylight;
> +extern long timezone;
> +extern int getdate_err;
> +struct tm *getdate (const char *);
> +#endif
> +
> +
> +#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
> +int stime(const time_t *);
> +time_t timegm(struct tm *);
> +#endif
> +
> +#ifdef __cplusplus
> +}
> +#endif
> +
> +
> +#endif
> diff --git a/lib/uktime/musl-imported/src/__month_to_secs.c 
> b/lib/uktime/musl-imported/src/__month_to_secs.c
> new file mode 100644
> index 00000000..43248fb3
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/__month_to_secs.c
> @@ -0,0 +1,10 @@
> +int __month_to_secs(int month, int is_leap)
> +{
> +     static const int secs_through_month[] = {
> +             0, 31*86400, 59*86400, 90*86400,
> +             120*86400, 151*86400, 181*86400, 212*86400,
> +             243*86400, 273*86400, 304*86400, 334*86400 };
> +     int t = secs_through_month[month];
> +     if (is_leap && month >= 2) t+=86400;
> +     return t;
> +}
> diff --git a/lib/uktime/musl-imported/src/__secs_to_tm.c 
> b/lib/uktime/musl-imported/src/__secs_to_tm.c
> new file mode 100644
> index 00000000..093d9021
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/__secs_to_tm.c
> @@ -0,0 +1,82 @@
> +#include "time_impl.h"
> +#include <limits.h>
> +
> +/* 2000-03-01 (mod 400 year, immediately after feb29 */
> +#define LEAPOCH (946684800LL + 86400*(31+29))
> +
> +#define DAYS_PER_400Y (365*400 + 97)
> +#define DAYS_PER_100Y (365*100 + 24)
> +#define DAYS_PER_4Y   (365*4   + 1)
> +
> +int __secs_to_tm(long long t, struct tm *tm)
> +{
> +     long long days, secs, years;
> +     int remdays, remsecs, remyears;
> +     int qc_cycles, c_cycles, q_cycles;
> +     int months;
> +     int wday, yday, leap;
> +     static const char days_in_month[] = 
> {31,30,31,30,31,31,30,31,30,31,31,29};
> +
> +     /* Reject time_t values whose year would overflow int */
> +     if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
> +             return -1;
> +
> +     secs = t - LEAPOCH;
> +     days = secs / 86400;
> +     remsecs = secs % 86400;
> +     if (remsecs < 0) {
> +             remsecs += 86400;
> +             days--;
> +     }
> +
> +     wday = (3+days)%7;
> +     if (wday < 0) wday += 7;
> +
> +     qc_cycles = days / DAYS_PER_400Y;
> +     remdays = days % DAYS_PER_400Y;
> +     if (remdays < 0) {
> +             remdays += DAYS_PER_400Y;
> +             qc_cycles--;
> +     }
> +
> +     c_cycles = remdays / DAYS_PER_100Y;
> +     if (c_cycles == 4) c_cycles--;
> +     remdays -= c_cycles * DAYS_PER_100Y;
> +
> +     q_cycles = remdays / DAYS_PER_4Y;
> +     if (q_cycles == 25) q_cycles--;
> +     remdays -= q_cycles * DAYS_PER_4Y;
> +
> +     remyears = remdays / 365;
> +     if (remyears == 4) remyears--;
> +     remdays -= remyears * 365;
> +
> +     leap = !remyears && (q_cycles || !c_cycles);
> +     yday = remdays + 31 + 28 + leap;
> +     if (yday >= 365+leap) yday -= 365+leap;
> +
> +     years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
> +
> +     for (months=0; days_in_month[months] <= remdays; months++)
> +             remdays -= days_in_month[months];
> +
> +     if (months >= 10) {
> +             months -= 12;
> +             years++;
> +     }
> +
> +     if (years+100 > INT_MAX || years+100 < INT_MIN)
> +             return -1;
> +
> +     tm->tm_year = years + 100;
> +     tm->tm_mon = months + 2;
> +     tm->tm_mday = remdays + 1;
> +     tm->tm_wday = wday;
> +     tm->tm_yday = yday;
> +
> +     tm->tm_hour = remsecs / 3600;
> +     tm->tm_min = remsecs / 60 % 60;
> +     tm->tm_sec = remsecs % 60;
> +
> +     return 0;
> +}
> diff --git a/lib/uktime/musl-imported/src/__tm_to_secs.c 
> b/lib/uktime/musl-imported/src/__tm_to_secs.c
> new file mode 100644
> index 00000000..c29fa985
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/__tm_to_secs.c
> @@ -0,0 +1,24 @@
> +#include "time_impl.h"
> +
> +long long __tm_to_secs(const struct tm *tm)
> +{
> +     int is_leap;
> +     long long year = tm->tm_year;
> +     int month = tm->tm_mon;
> +     if (month >= 12 || month < 0) {
> +             int adj = month / 12;
> +             month %= 12;
> +             if (month < 0) {
> +                     adj--;
> +                     month += 12;
> +             }
> +             year += adj;
> +     }
> +     long long t = __year_to_secs(year, &is_leap);
> +     t += __month_to_secs(month, is_leap);
> +     t += 86400LL * (tm->tm_mday-1);
> +     t += 3600LL * tm->tm_hour;
> +     t += 60LL * tm->tm_min;
> +     t += tm->tm_sec;
> +     return t;
> +}
> diff --git a/lib/uktime/musl-imported/src/__year_to_secs.c 
> b/lib/uktime/musl-imported/src/__year_to_secs.c
> new file mode 100644
> index 00000000..2824ec6d
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/__year_to_secs.c
> @@ -0,0 +1,47 @@
> +long long __year_to_secs(long long year, int *is_leap)
> +{
> +     if (year-2ULL <= 136) {
> +             int y = year;
> +             int leaps = (y-68)>>2;
> +             if (!((y-68)&3)) {
> +                     leaps--;
> +                     if (is_leap) *is_leap = 1;
> +             } else if (is_leap) *is_leap = 0;
> +             return 31536000*(y-70) + 86400*leaps;
> +     }
> +
> +     int cycles, centuries, leaps, rem;
> +
> +     if (!is_leap) is_leap = &(int){0};
> +     cycles = (year-100) / 400;
> +     rem = (year-100) % 400;
> +     if (rem < 0) {
> +             cycles--;
> +             rem += 400;
> +     }
> +     if (!rem) {
> +             *is_leap = 1;
> +             centuries = 0;
> +             leaps = 0;
> +     } else {
> +             if (rem >= 200) {
> +                     if (rem >= 300) centuries = 3, rem -= 300;
> +                     else centuries = 2, rem -= 200;
> +             } else {
> +                     if (rem >= 100) centuries = 1, rem -= 100;
> +                     else centuries = 0;
> +             }
> +             if (!rem) {
> +                     *is_leap = 0;
> +                     leaps = 0;
> +             } else {
> +                     leaps = rem / 4U;
> +                     rem %= 4U;
> +                     *is_leap = !rem;
> +             }
> +     }
> +
> +     leaps += 97*cycles + 24*centuries - *is_leap;
> +
> +     return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
> +}
> diff --git a/lib/uktime/musl-imported/src/time_impl.h 
> b/lib/uktime/musl-imported/src/time_impl.h
> new file mode 100644
> index 00000000..f26d8005
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/time_impl.h
> @@ -0,0 +1,11 @@
> +#include <time.h>
> +
> +hidden int __days_in_month(int, int);
> +hidden int __month_to_secs(int, int);
> +hidden long long __year_to_secs(long long, int *);
> +hidden long long __tm_to_secs(const struct tm *);
> +hidden const char *__tm_to_tzname(const struct tm *);
> +hidden int __secs_to_tm(long long, struct tm *);
> +hidden void __secs_to_zone(long long, int, int *, long *, long *, const char 
> **);
> +hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const 
> struct tm *, locale_t, int);
> +extern hidden const char __utc[];
> diff --git a/lib/uktime/musl-imported/src/timegm.c 
> b/lib/uktime/musl-imported/src/timegm.c
> new file mode 100644
> index 00000000..4e5907d7
> --- /dev/null
> +++ b/lib/uktime/musl-imported/src/timegm.c
> @@ -0,0 +1,18 @@
> +#define _GNU_SOURCE
> +#include "time_impl.h"
> +#include <errno.h>
> +
> +time_t timegm(struct tm *tm)
> +{
> +     struct tm new;
> +     long long t = __tm_to_secs(tm);
> +     if (__secs_to_tm(t, &new) < 0) {
> +             errno = EOVERFLOW;
> +             return -1;
> +     }
> +     *tm = new;
> +     tm->tm_isdst = 0;
> +     tm->__tm_gmtoff = 0;
> +     tm->__tm_zone = __utc;
> +     return t;
> +}
> 

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.