|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Minios-devel] [UNIKRAFT PATCH v2 7/8] lib/uktimeconv: Add time conversion library
libuktimeconv adds helpers for detecting leap years,
calculating the days per month or year, and converting
BMK clocks to __nsec timestamps.
The code is ported from Solo5.
Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
Signed-off-by: Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
---
lib/Config.uk | 1 +
lib/Makefile.uk | 1 +
lib/uktimeconv/Config.uk | 4 +
lib/uktimeconv/Makefile.uk | 6 +
lib/uktimeconv/include/uk/timeconv.h | 109 +++++++++++++++++
lib/uktimeconv/timeconv.c | 228 +++++++++++++++++++++++++++++++++++
6 files changed, 349 insertions(+)
create mode 100644 lib/uktimeconv/Config.uk
create mode 100644 lib/uktimeconv/Makefile.uk
create mode 100644 lib/uktimeconv/include/uk/timeconv.h
create mode 100644 lib/uktimeconv/timeconv.c
diff --git a/lib/Config.uk b/lib/Config.uk
index 637c5d4..5bf5ebc 100644
--- a/lib/Config.uk
+++ b/lib/Config.uk
@@ -26,6 +26,7 @@ config HAVE_SCHED
source "lib/ukboot/Config.uk"
source "lib/ukdebug/Config.uk"
source "lib/ukargparse/Config.uk"
+source "lib/uktimeconv/Config.uk"
source "lib/nolibc/Config.uk"
source "lib/ukalloc/Config.uk"
source "lib/ukallocbbuddy/Config.uk"
diff --git a/lib/Makefile.uk b/lib/Makefile.uk
index a594137..d08a50f 100644
--- a/lib/Makefile.uk
+++ b/lib/Makefile.uk
@@ -7,6 +7,7 @@
$(eval $(call _import_lib,$(UK_BASE)/lib/ukboot))
$(eval $(call _import_lib,$(UK_BASE)/lib/ukdebug))
$(eval $(call _import_lib,$(UK_BASE)/lib/ukargparse))
+$(eval $(call _import_lib,$(UK_BASE)/lib/uktimeconv))
$(eval $(call _import_lib,$(UK_BASE)/lib/nolibc))
$(eval $(call _import_lib,$(UK_BASE)/lib/ukalloc))
$(eval $(call _import_lib,$(UK_BASE)/lib/ukallocbbuddy))
diff --git a/lib/uktimeconv/Config.uk b/lib/uktimeconv/Config.uk
new file mode 100644
index 0000000..f0494ca
--- /dev/null
+++ b/lib/uktimeconv/Config.uk
@@ -0,0 +1,4 @@
+config LIBUKTIMECONV
+ bool "uktimeconv: Time conversion functions"
+ default n
+ select LIBUKDEBUG
diff --git a/lib/uktimeconv/Makefile.uk b/lib/uktimeconv/Makefile.uk
new file mode 100644
index 0000000..0f44984
--- /dev/null
+++ b/lib/uktimeconv/Makefile.uk
@@ -0,0 +1,6 @@
+$(eval $(call addlib_s,libuktimeconv,$(LIBUKTIMECONV)))
+
+CINCLUDES-$(LIBUKTIMECONV) += -I$(LIBUKTIMECONV_BASE)/include
+CXXINCLUDES-$(LIBUKTIMECONV) += -I$(LIBUKTIMECONV_BASE)/include
+
+LIBUKTIMECONV_SRCS-y += $(LIBUKTIMECONV_BASE)/timeconv.c
diff --git a/lib/uktimeconv/include/uk/timeconv.h
b/lib/uktimeconv/include/uk/timeconv.h
new file mode 100644
index 0000000..0a67bb5
--- /dev/null
+++ b/lib/uktimeconv/include/uk/timeconv.h
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: ISC AND BSD-2-Clause-NetBSD */
+/*
+ * Authors: Martin Lucina
+ * Ricardo Koller
+ * Costin Lupu <costin.lupu@xxxxxxxxx>
+ * Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
+ *
+ * Copyright (c) 2015-2017 IBM
+ * Copyright (c) 2016-2017 Docker, Inc.
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* Taken from solo5 clock_subr.h */
+
+/*-
+ * Copyright (c) 1996 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Gordon W. Ross
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __UKTIMECONV_H__
+#define __UKTIMECONV_H__
+
+#include <uk/arch/time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Some handy constants. */
+#define __SECS_PER_MINUTE 60
+#define __SECS_PER_HOUR 3600
+#define __SECS_PER_DAY 86400
+#define __DAYS_PER_COMMON_YEAR 365
+#define __DAYS_PER_LEAP_YEAR 366
+
+struct uktimeconv_bmkclock {
+ __s64 dt_year;
+ __u8 dt_mon;
+ __u8 dt_day;
+ __u8 dt_hour;
+ __u8 dt_min;
+ __u8 dt_sec;
+};
+
+int uktimeconv_is_leap_year(__s64 year);
+__u8 uktimeconv_days_in_month(__u8 month, int is_leap_year);
+
+static inline __u16 uktimeconv_days_per_year(__s64 year)
+{
+ return uktimeconv_is_leap_year(year)
+ ? __DAYS_PER_LEAP_YEAR
+ : __DAYS_PER_COMMON_YEAR;
+}
+
+/*
+ * "POSIX time" from "YY/MM/DD/hh/mm/ss"
+ */
+__nsec uktimeconv_bmkclock_to_nsec(struct uktimeconv_bmkclock *dt);
+
+/*
+ * BCD to binary.
+ */
+static inline unsigned int uktimeconv_bcdtobin(unsigned int bcd)
+{
+ return ((bcd >> 4) & 0x0f) * 10 + (bcd & 0x0f);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __UKTIMECONV_H__ */
diff --git a/lib/uktimeconv/timeconv.c b/lib/uktimeconv/timeconv.c
new file mode 100644
index 0000000..0d07f50
--- /dev/null
+++ b/lib/uktimeconv/timeconv.c
@@ -0,0 +1,228 @@
+/* SPDX-License-Identifier: ISC AND BSD-2-Clause-NetBSD AND BSD-3-Clause */
+/*
+ * Authors: Martin Lucina
+ * Ricardo Koller
+ * Costin Lupu <costin.lupu@xxxxxxxxx>
+ * Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
+ *
+ * Copyright (c) 2015-2017 IBM
+ * Copyright (c) 2016-2017 Docker, Inc.
+ * Copyright (c) 2018, NEC Europe Ltd., NEC Corporation
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+/* Taken from solo5 clock_subr.c */
+
+/* $NetBSD: clock_subr.c,v 1.26 2014/12/22 18:09:20 christos Exp $ */
+
+/*-
+ * Copyright (c) 1996 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Gordon W. Ross
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1988 University of Utah.
+ * Copyright (c) 1982, 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * the Systems Programming Group of the University of Utah Computer
+ * Science Department.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * from: Utah $Hdr: clock.c 1.18 91/01/21$
+ *
+ * @(#)clock.c 8.2 (Berkeley) 1/12/94
+ */
+
+#include <uk/timeconv.h>
+#include <uk/assert.h>
+
+#define SECS_PER_COMMON_YEAR (__SECS_PER_DAY * DAYS_PER_COMMON_YEAR)
+#define SECS_PER_LEAP_YEAR (__SECS_PER_DAY * DAYS_PER_LEAP_YEAR)
+
+#define FEBRUARY 2
+
+/* Traditional POSIX base year */
+#define POSIX_BASE_YEAR 1970
+
+/* Some handy functions */
+static __u8 _days_in_month(__u8 m)
+{
+ switch (m) {
+ case FEBRUARY:
+ return 28;
+ case 4: case 6: case 9: case 11:
+ return 30;
+ case 1: case 3: case 5: case 7: case 8: case 10: case 12:
+ return 31;
+ default:
+ /* m out of range */
+ return 0;
+ }
+}
+
+__u8 uktimeconv_days_in_month(__u8 month, int is_leap_year)
+{
+ __u8 days;
+
+ days = _days_in_month(month);
+ if (is_leap_year && month == FEBRUARY)
+ ++days;
+
+ return days;
+}
+
+/*
+ * This function avoids some unnecessary modulo
+ * operations as compared with the usual macro:
+ * ( ((year % 4) == 0 &&
+ * (year % 100) != 0) ||
+ * ((year % 400) == 0) )
+ * It is otherwise equivalent.
+ */
+int uktimeconv_is_leap_year(__s64 year)
+{
+ if ((year & 3) != 0)
+ return 0;
+
+ if ((year % 100) != 0)
+ return 1;
+
+ return (year % 400) == 0;
+}
+
+
+/*
+ * Generic routines to convert between a POSIX date
+ * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
+ * Derived from arch/hp300/hp300/clock.c
+ */
+
+/* for easier alignment:
+ * time from the epoch to 2000 (there were 7 leap years):
+ */
+#define DAYSTO2000 (365 * 30 + 7)
+
+/* 4 year intervals include 1 leap year */
+#define DAYS4YEARS (365 * 4 + 1)
+
+/* 100 year intervals include 24 leap years */
+#define DAYS100YEARS (365 * 100 + 24)
+
+/* 400 year intervals include 97 leap years */
+#define DAYS400YEARS (365 * 400 + 97)
+
+__nsec uktimeconv_bmkclock_to_nsec(struct uktimeconv_bmkclock *dt)
+{
+ __u64 secs, year, days, i;
+
+ UK_ASSERT(dt->dt_year >= POSIX_BASE_YEAR);
+
+ /*
+ * Compute days since start of time
+ * First from years, then from months.
+ */
+ year = (__u64) dt->dt_year;
+ days = 0;
+ if (uktimeconv_is_leap_year(year) && dt->dt_mon > FEBRUARY)
+ days++;
+
+ if (year < 2000) {
+ /* simple way for early years */
+ for (i = POSIX_BASE_YEAR; i < year; i++)
+ days += uktimeconv_days_per_year(i);
+
+ } else {
+ /* years are properly aligned */
+ days += DAYSTO2000;
+ year -= 2000;
+
+ i = year / 400;
+ days += i * DAYS400YEARS;
+ year -= i * 400;
+
+ i = year / 100;
+ days += i * DAYS100YEARS;
+ year -= i * 100;
+
+ i = year / 4;
+ days += i * DAYS4YEARS;
+ year -= i * 4;
+
+ for (i = dt->dt_year - year; i < (__u64) dt->dt_year; i++)
+ days += uktimeconv_days_per_year(i);
+ }
+
+ /* Months */
+ for (i = 1; i < dt->dt_mon; i++)
+ days += _days_in_month(i);
+ days += (dt->dt_day - 1);
+
+ /* Add hours, minutes, seconds. */
+ secs = ((days
+ * 24 + dt->dt_hour)
+ * 60 + dt->dt_min)
+ * 60 + dt->dt_sec;
+
+ return ukarch_time_sec_to_nsec((__nsec) secs);
+}
--
2.7.4
_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |