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

[Minios-devel] [UNIKRAFT/LIBNEWLIBC PATCH 1/1] Import the timegm function from musl


  • To: "minios-devel@xxxxxxxxxxxxx" <minios-devel@xxxxxxxxxxxxx>
  • From: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
  • Date: Thu, 5 Sep 2019 00:20:07 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=stud.acs.upb.ro; dmarc=pass action=none header.from=stud.acs.upb.ro; dkim=pass header.d=stud.acs.upb.ro; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Fyrl+sXqpnLyaHI8/MKTWfb4SqR7Jc/AOFDFVyHX+9I=; b=QfApeeRtKV1vr9CKBuuVhaF9eY4CxjhbhnYbYrX0F9csJ35XVLC1im/HI2hEUj9jIwmzur6961mObfvdDh+6KMD+YIa8A60fkVfixpL6iwsR00+qn2nTnfEMKjVwdRYsnVygHlFmZWEvlIgObw5gsHeo6KUBY5fyr1ad9qSDBP+Zfa+4LaLABLRlFLZgrPuLgj5E7xUem0TmGRpuxmoW691xPuSEmuWsl1gedzCvGggCYGaFVpYzYVBCVDummOHUEnhrXJFP2i5JGv5GZfvYAKFwx+NoflwKTZLjX+ogo2dsOycCwQDmjqefMk1OAnkHmfNuHceCBjjXy4QoqWrWAA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ql+7JRqoCMh25OeBPu+bzHQUIgC+nKGRl3x6U5FmVKECsKpVW+Hw3x27PYgctw5yF841+aRwl6VwpZTw6Erp3eAS5kBj0qMhr9HJPPk3rRV3tijWorMypyeuuZabC3FtyeMGnw3CH0OYAriLiwah5z2OJ3sZGmh1A70K9Xf3atx7ujyQZa4yCRU31VcTkyTAcD5mr32XnHh9sIaejWAgJGLeN/WxKY3Q4cBTKuNvoF+ClrBMvIhNLPkRUHidW2RRYUm8wB8dZIHo1UxZMg/IyhJf2OgIvlaraJC0vxZVEZeXKQnMCzIqkBIRCKdBQX9bn8e9W+qgywLd8Sx1+xQ2Ng==
  • Authentication-results: spf=none (sender IP is ) smtp.mailfrom=vlad_andrei.badoiu@xxxxxxxxxxxxxxx;
  • Cc: Vlad-Andrei BĂDOIU (78692) <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>, "costin.lupu@xxxxxxxxx" <costin.lupu@xxxxxxxxx>
  • Delivery-date: Thu, 05 Sep 2019 00:20:15 +0000
  • List-id: Mini-os development list <minios-devel.lists.xenproject.org>
  • Thread-index: AQHVY3+reRAqTVTZFU+MmnrLKqM+qQ==
  • Thread-topic: [UNIKRAFT/LIBNEWLIBC PATCH 1/1] Import the timegm function from musl

Copied as is from the official musl git mirror
git://git.musl-libc.org/musl, commit 6fcb440d.
The only change made to the files was to remove
the dependency upon the time_impl.h header.

Signed-off-by: Vlad-Andrei Badoiu <vlad_andrei.badoiu@xxxxxxxxxxxxxxx>
---
 Makefile.uk                              |  5 ++
 musl-imported/src/time/__month_to_secs.c | 10 +++
 musl-imported/src/time/__secs_to_tm.c    | 81 ++++++++++++++++++++++++
 musl-imported/src/time/__tm_to_secs.c    | 26 ++++++++
 musl-imported/src/time/__year_to_secs.c  | 47 ++++++++++++++
 musl-imported/src/time/timegm.c          | 17 +++++
 6 files changed, 186 insertions(+)
 create mode 100644 musl-imported/src/time/__month_to_secs.c
 create mode 100644 musl-imported/src/time/__secs_to_tm.c
 create mode 100644 musl-imported/src/time/__tm_to_secs.c
 create mode 100644 musl-imported/src/time/__year_to_secs.c
 create mode 100644 musl-imported/src/time/timegm.c

diff --git a/Makefile.uk b/Makefile.uk
index 01da724..1c9e6bb 100644
--- a/Makefile.uk
+++ b/Makefile.uk
@@ -127,6 +127,11 @@ LIBNEWLIBGLUE_SRCS-y += $(LIBNEWLIBC_BASE)/mntent.c
 LIBNEWLIBGLUE_SRCS-y += $(LIBNEWLIBC_BASE)/musl-imported/src/misc/syslog.c
 LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/termios/tcsetattr.c
 LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/termios/tcgetattr.c
+LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/time/__month_to_secs.c
+LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/time/__secs_to_tm.c
+LIBNEWLIBGLUE_SRCS-y += $(LIBNEWLIBC_BASE)/musl-imported/src/time/timegm.c
+LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/time/__tm_to_secs.c
+LIBNEWLIBGLUE_SRCS-y += 
$(LIBNEWLIBC_BASE)/musl-imported/src/time/__year_to_secs.c
 
 
################################################################################
 # Newlib/libc code -- argz
diff --git a/musl-imported/src/time/__month_to_secs.c 
b/musl-imported/src/time/__month_to_secs.c
new file mode 100644
index 0000000..43248fb
--- /dev/null
+++ b/musl-imported/src/time/__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/musl-imported/src/time/__secs_to_tm.c 
b/musl-imported/src/time/__secs_to_tm.c
new file mode 100644
index 0000000..1b7bf70
--- /dev/null
+++ b/musl-imported/src/time/__secs_to_tm.c
@@ -0,0 +1,81 @@
+#include <limits.h>
+#include <time.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;
+       int remdays, remsecs, remyears;
+       int qc_cycles, c_cycles, q_cycles;
+       int years, 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 + 400*qc_cycles;
+
+       for (months=0; days_in_month[months] <= remdays; months++)
+               remdays -= days_in_month[months];
+
+       if (years+100 > INT_MAX || years+100 < INT_MIN)
+               return -1;
+
+       tm->tm_year = years + 100;
+       tm->tm_mon = months + 2;
+       if (tm->tm_mon >= 12) {
+               tm->tm_mon -=12;
+               tm->tm_year++;
+       }
+       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/musl-imported/src/time/__tm_to_secs.c 
b/musl-imported/src/time/__tm_to_secs.c
new file mode 100644
index 0000000..3448d67
--- /dev/null
+++ b/musl-imported/src/time/__tm_to_secs.c
@@ -0,0 +1,26 @@
+#include <time.h>
+
+#define LEAPOCH (946684800LL + 86400*(31+29))
+
+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/musl-imported/src/time/__year_to_secs.c 
b/musl-imported/src/time/__year_to_secs.c
new file mode 100644
index 0000000..2824ec6
--- /dev/null
+++ b/musl-imported/src/time/__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/musl-imported/src/time/timegm.c b/musl-imported/src/time/timegm.c
new file mode 100644
index 0000000..a09dcc1
--- /dev/null
+++ b/musl-imported/src/time/timegm.c
@@ -0,0 +1,17 @@
+#include <errno.h>
+#include <time.h>
+
+extern const char __gmt[];
+
+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;
+       return t;
+}
-- 
2.20.1


_______________________________________________
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®.