]> git.sur5r.net Git - cc65/commitdiff
Added lots of functions from time.h
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 12 Nov 2002 22:06:02 +0000 (22:06 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 12 Nov 2002 22:06:02 +0000 (22:06 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@1507 b7a2c559-68d2-44c3-8de9-860c34a00d81

libsrc/common/.cvsignore
libsrc/common/Makefile
libsrc/common/asctime.c [new file with mode: 0644]
libsrc/common/ctime.s [new file with mode: 0644]
libsrc/common/gmtime.c [new file with mode: 0644]
libsrc/common/localtime.c [new file with mode: 0644]
libsrc/common/mktime.c [new file with mode: 0644]
libsrc/common/timezone.c [new file with mode: 0644]

index 68876ae1620d1f88acedc461703ead0de6170047..0939e9b105c619cb64e831eeda4abf25dce9f792 100644 (file)
@@ -4,6 +4,7 @@ _fopen.s
 _hextab.s
 _scanf.s
 abort.s
+asctime.s
 bsearch.s
 errormsg.s
 fclose.s
@@ -22,7 +23,10 @@ ftell.s
 fwrite.s
 getchar.s
 gets.s
+gmtime.s
 locale.s
+localtime.s
+mktime.s
 perror.s
 putchar.s
 puts.s
@@ -30,7 +34,9 @@ qsort.s
 realloc.s
 rewind.s
 sscanf.s
+strftime.s
 strtok.s
 strxfrm.s
+timezone.s
 vprintf.s
 vsscanf.s
index 6df96901de10e18c4813d1a007ddaf12d9183ab5..11c209dd15082a915945c351b1cd228395895c3c 100644 (file)
@@ -16,6 +16,7 @@ C_OBJS =      _afailed.o      \
                _hextab.o       \
                _scanf.o        \
                abort.o         \
+                asctime.o       \
                bsearch.o       \
                errormsg.o      \
                fclose.o        \
@@ -34,7 +35,10 @@ C_OBJS =     _afailed.o      \
                fwrite.o        \
                getchar.o       \
                gets.o          \
+                gmtime.o        \
                locale.o        \
+                localtime.o     \
+                mktime.o        \
                perror.o        \
                putchar.o       \
                puts.o          \
@@ -44,6 +48,7 @@ C_OBJS =      _afailed.o      \
                sscanf.o        \
                strxfrm.o       \
                strtok.o        \
+                timezone.o      \
                vprintf.o       \
                 vsscanf.o
 
@@ -62,6 +67,7 @@ S_OBJS =      _fdesc.o        \
                calloc.o        \
                copydata.o      \
                cprintf.o       \
+                ctime.o         \
                 divt.o          \
                errno.o         \
                fmisc.o         \
diff --git a/libsrc/common/asctime.c b/libsrc/common/asctime.c
new file mode 100644 (file)
index 0000000..76c7d92
--- /dev/null
@@ -0,0 +1,73 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                   asctime.c                               */
+/*                                                                           */
+/*                 Convert a broken down time into a string                  */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2002      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <stdio.h>
+#include <time.h>
+
+
+
+char* __fastcall__ asctime (const struct tm* timep)
+{
+    static const char days[7][4]  = {
+        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
+    };
+    static const char months[12][4] = {
+        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+    };
+    static char buf[26];
+
+    /* Create a copy of the given data and make sure it is valid */
+    struct tm t;
+    t = *timep;
+    mktime (&t);
+
+    /* Format into given buffer */
+    sprintf(buf,
+            "%s %s%3d %02d:%02d:%02d %d\n",
+           days[t.tm_wday],
+            months[t.tm_mon],
+            t.tm_mday,
+           t.tm_hour,
+            t.tm_min,
+            t.tm_sec,
+            t.tm_year + 1900);
+
+    /* Return the result */
+    return buf;
+}
+
+
+
diff --git a/libsrc/common/ctime.s b/libsrc/common/ctime.s
new file mode 100644 (file)
index 0000000..d15df3c
--- /dev/null
@@ -0,0 +1,19 @@
+;
+; Ullrich von Bassewitz, 12.11.2002
+;
+; char* __fastcall__ ctime (time_t* timep);
+;
+
+       .export         _ctime
+        .import         _localtime, _asctime
+
+
+.proc   _ctime
+
+; return asctime (localtime (timep));
+
+        jsr     _localtime
+        jmp     _asctime
+
+.endproc
+
diff --git a/libsrc/common/gmtime.c b/libsrc/common/gmtime.c
new file mode 100644 (file)
index 0000000..fde5f8c
--- /dev/null
@@ -0,0 +1,57 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                 gmtime.c                                  */
+/*                                                                           */
+/*            Convert calendar time into broken down time in UTC             */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2002      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <time.h>
+
+
+
+struct tm* __fastcall__ gmtime (const time_t* timep)
+{
+    time_t t;
+
+    /* Check for a valid time spec */
+    if (timep == 0) {
+        return 0;
+    }
+
+    /* Get the time and correct for the time zone offset */
+    t = *timep + _tz.timezone;
+
+    /* Use localtime for conversion */
+    return localtime (&t);
+}
+
+
+
diff --git a/libsrc/common/localtime.c b/libsrc/common/localtime.c
new file mode 100644 (file)
index 0000000..92fabcd
--- /dev/null
@@ -0,0 +1,69 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                localtime.c                                */
+/*                                                                           */
+/*            Convert calendar time into broken down local time              */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2002      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <time.h>
+
+
+
+struct tm* __fastcall__ localtime (const time_t* timep)
+{
+    static struct tm timebuf;
+    time_t t;
+
+    /* Check the argument */
+    if (timep == 0 || (long) (t = *timep) < 0) {
+        /* Invalid arg */
+        return 0;
+    }
+
+    /* Since our ints are just 16 bits, split the given time into seconds,
+     * hours and days. Each of the values will fit in a 16 bit variable.
+     * The mktime routine will then do the rest.
+     */
+    timebuf.tm_sec  = t % 3600;
+    timebuf.tm_min  = 0;
+    timebuf.tm_hour = (t / 3600) % 24;
+    timebuf.tm_mday = (t / (3600UL * 24UL)) + 1;
+    timebuf.tm_mon  = 0;
+    timebuf.tm_year = 70;       /* Base value is 1/1/1970 */
+
+    /* Call mktime to do the final conversion */
+    mktime (&timebuf);
+
+    /* Return the result */
+    return &timebuf;
+}
+
+
diff --git a/libsrc/common/mktime.c b/libsrc/common/mktime.c
new file mode 100644 (file)
index 0000000..b185142
--- /dev/null
@@ -0,0 +1,191 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                 mktime.c                                  */
+/*                                                                           */
+/*           Make calendar time from broken down time and cleanup            */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2002      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <limits.h>
+#include <stdlib.h>
+#include <time.h>
+
+
+
+/*****************************************************************************/
+/*                                   Data                                    */
+/*****************************************************************************/
+
+
+
+#define JANUARY         0
+#define FEBRUARY        1
+#define DECEMBER       11
+#define JAN_1_1970      4               /* 1/1/1970 is a thursday */
+
+
+
+static const unsigned char MonthLength [] = {
+    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+};
+static const unsigned MonthDays [] = {
+    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+};
+
+
+
+/*****************************************************************************/
+/*                                   Code                                    */
+/*****************************************************************************/
+
+
+
+static unsigned char IsLeapYear (int Year)
+/* Returns 1 if the given year is a leap year */
+{
+    return (((Year % 4) == 0) && ((Year % 100) != 0 || (Year % 400) == 0));
+}
+
+
+
+time_t __fastcall__ mktime (struct tm* TM)
+/* Make a time in seconds since 1/1/1970 from the broken down time in TM.
+ * A call to mktime does also correct the time in TM to contain correct
+ * values.
+ */
+{
+    div_t D;
+    int Max;
+    unsigned DayCount;
+
+    /* Check if TM is valid */
+    if (TM == 0) {
+        /* Invalid data */
+        goto Error;
+    }
+
+    /* Adjust seconds. */
+    D = div (TM->tm_sec, 60);
+    TM->tm_sec = D.rem;
+
+    /* Adjust minutes */
+    if (TM->tm_min + D.quot < 0) {
+        goto Error;
+    }
+    TM->tm_min += D.quot;
+    D = div (TM->tm_min, 60);
+    TM->tm_min = D.rem;
+
+    /* Adjust hours */
+    if (TM->tm_hour + D.quot < 0) {
+        goto Error;
+    }
+    TM->tm_hour += D.quot;
+    D = div (TM->tm_hour, 24);
+    TM->tm_hour = D.rem;
+
+    /* Adjust days */
+    if (TM->tm_mday + D.quot < 0) {
+        goto Error;
+    }
+    TM->tm_mday += D.quot;
+
+    /* Adjust month and year. This is an iterative process, since changing
+     * the month will change the allowed days for this month.
+     */
+    while (1) {
+
+        /* Make sure, month is in the range 0..11 */
+        D = div (TM->tm_mon, 12);
+        TM->tm_mon = D.rem;
+        if (TM->tm_year + D.quot < 0) {
+            goto Error;
+        }
+        TM->tm_year += D.quot;
+
+        /* Now check if mday is in the correct range, if not, correct month
+         * and eventually year and repeat the process.
+         */
+        if (TM->tm_mon == FEBRUARY && IsLeapYear (TM->tm_year + 1900)) {
+            Max = 29;
+        } else {
+            Max = MonthLength[TM->tm_mon];
+        }
+        if (TM->tm_mday > Max) {
+            /* Must correct month and eventually, year */
+            if (TM->tm_mon == DECEMBER) {
+                TM->tm_mon = JANUARY;
+                ++TM->tm_year;
+            } else {
+                ++TM->tm_mon;
+            }
+            TM->tm_mday -= Max;
+        } else {
+            /* Done */
+            break;
+        }
+    }
+
+    /* Ok, all time/date fields are now correct. Calculate the days in this
+     * year.
+     */
+    TM->tm_yday = MonthDays[TM->tm_mon] + TM->tm_mday - 1;
+    if (TM->tm_mon > FEBRUARY && IsLeapYear (TM->tm_year + 1900)) {
+        ++TM->tm_yday;
+    }
+
+    /* Calculate days since 1/1/1970. In the complete epoch (1/1/1970 to
+     * somewhere in 2038) all years dividable by 4 are leap years, so
+     * dividing by 4 gives the days that must be added cause of leap years.
+     * (and the last leap year before 1970 was 1968)
+     */
+    DayCount = ((unsigned) (TM->tm_year-70)) * 365U +
+               (((unsigned) (TM->tm_year-68-1)) / 4) +
+               TM->tm_yday;
+
+    /* Calculate the weekday */
+    TM->tm_wday = (JAN_1_1970 + DayCount) % 7;
+
+    /* No (US) daylight saving (for now) */
+    TM->tm_isdst = 0;
+
+    /* Return seconds since 1970 */
+    return DayCount * 86400UL +
+           ((unsigned) TM->tm_hour) * 3600UL +
+           ((unsigned) TM->tm_min) * 60U +
+           ((unsigned) TM->tm_sec);
+
+Error:
+    /* Error exit */
+    return (time_t) -1L;
+}
+
+
+
diff --git a/libsrc/common/timezone.c b/libsrc/common/timezone.c
new file mode 100644 (file)
index 0000000..b8484dc
--- /dev/null
@@ -0,0 +1,48 @@
+/*****************************************************************************/
+/*                                                                           */
+/*                                  timezone.c                               */
+/*                                                                           */
+/*                            Timezone specification                         */
+/*                                                                           */
+/*                                                                           */
+/*                                                                           */
+/* (C) 2002      Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
+/*                                                                           */
+/*                                                                           */
+/* This software is provided 'as-is', without any expressed or implied       */
+/* warranty.  In no event will the authors be held liable for any damages    */
+/* arising from the use of this software.                                    */
+/*                                                                           */
+/* Permission is granted to anyone to use this software for any purpose,     */
+/* including commercial applications, and to alter it and redistribute it    */
+/* freely, subject to the following restrictions:                            */
+/*                                                                           */
+/* 1. The origin of this software must not be misrepresented; you must not   */
+/*    claim that you wrote the original software. If you use this software   */
+/*    in a product, an acknowledgment in the product documentation would be  */
+/*    appreciated but is not required.                                       */
+/* 2. Altered source versions must be plainly marked as such, and must not   */
+/*    be misrepresented as being the original software.                      */
+/* 3. This notice may not be removed or altered from any source              */
+/*    distribution.                                                          */
+/*                                                                           */
+/*****************************************************************************/
+
+
+
+#include <time.h>
+
+
+
+struct _timezone _tz = {
+    0,                  /* True if daylight savings time active */
+    0,                  /* Number of seconds behind UTC */
+    "UTC",              /* Name of timezone, e.g. CET */
+    "UTC"               /* Name when daylight true, e.g. CEST */
+};
+
+
+