time 9.32 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* time.c: ANSI draft (X3J11 Oct 86) section 4.12 code */
/* Copyright (C) Codemist Ltd, 1988 */
/* version 0.02a */

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>

#include "kernel.h"
#include "territory.h"
#include "swis.h"

/* Array indices corresponding to the LC macros above */
#define N_LC_COLLATE  0
#define N_LC_CTYPE    1
#define N_LC_MONETARY 2
#define N_LC_NUMERIC  3
#define N_LC_TIME     4
#define N_LC_ALL      5

extern int __locales[5];

/* NB strftime() is in locale.c since it seems VERY locale-dependent      */

/* In NorCroft C time() yields the unix result of an unsigned int holding */
/* seconds since 1 Jan 1970.                                              */
/* clock() returns an unsigned int with ticks of cpu time.                */

/* N.B. clock() and time() are defined in opsys.c                         */

static int monlen[13] = { 31,29,31,30,31,30,31,31,30,31,30,31,0x40000000 };

double difftime(time_t time1, time_t time0)
{   return (double)time1 - (double)time0;
}


static int tm_carry(int *a, int b, int q)
{   /* *a = (*a + b) % q, return (*a + b)/q.  Care with overflow.          */
    int aa = *a;
    int hi = (aa >> 16) + (b >> 16);    /* NB signed shift arithmetic here */
    int lo = (aa & 0xffff) + (b & 0xffff);
    lo += (hi % q) << 16;
    hi = hi / q;
    aa = lo % q;
    lo = lo / q;
    while (aa < 0)
    {   aa += q;
        lo -= 1;
    }
    *a = aa;        /* remainder is positive here */
    return (hi << 16) + lo;
}

time_t mktime(struct tm *timeptr)
{   /* the Oct 1986 ANSI draft spec allows ANY values for the contents     */
    /* of timeptr.  This leave the question - what is month -9 or +123?    */
    /* the code below resolves it in one way:                              */
    /* Also note that struct tm is allowed to have signed values in it for */
    /* the purposes of this function even though normalized times all have */
    /* just positive entries.                                              */
    time_t t;
    int w, v, yday;
    int sec = timeptr->tm_sec;
    int min = timeptr->tm_min;
    int hour = timeptr->tm_hour;
    int mday = timeptr->tm_mday;
    int mon = timeptr->tm_mon;
    int year = timeptr->tm_year;
    int quadyear = 0;
/* The next line applies a simple test that detects some gross overflows */
    if (year > 0x40000000 || year < -0x40000000) return (time_t)-1;

    /* we really do have to propagate carries up it seems                  */
    /* careful about overflow for divide, but not carry add.               */

    w = tm_carry(&sec,0,60);    /* leaves 0 <= sec < 60  */
    w = tm_carry(&min,w,60);    /* leaves 0 <= min < 60  */
    w = tm_carry(&hour,w,24);   /* leaves 0 <= hour < 24 */
    quadyear = tm_carry(&mday,w - 1,(4*365+1));  /* 0 <= mday < 4 years    */
/* The next line can not possibly result in year overflowing since the     */
/* initial values was checked earlier and the month can only cause a       */
/* carry of size up to MAXINT/12 with quadyear limited to MAXINT/365.      */
    year += quadyear*4 + tm_carry(&mon,0,12);
    /* at last the mday is in 0..4*365 and the mon in 0..11                */

#define notleapyear(year) (((year) & 3)!=0)
/* Note that 1900 is not in the range of valid dates and so I will fudge   */
/* the issue about it not being a leap year.                               */

    while (mday >= monlen[mon])
    {   mday -= monlen[mon++];
        if (mon==2 && notleapyear(year)) mday++;
        else if (mon == 12) mon = 0, year++;
    }
    if (mon==1 && mday==28 && notleapyear(year)) mon++, mday=0;

#define YEARS (0x7fffffff/60/60/24/365 + 1)
    if (year < 70 || year > 70+2*YEARS) return (time_t)-1;
#undef YEARS

    yday = mday;
    {   int i;
        for (i = 0; i<mon; i++) yday += monlen[i];
    }
    if (mon > 1 && notleapyear(year)) yday--;

    v = (365*4+1)*(year/4) + 365*(year & 3) + yday;
    if (!notleapyear(year)) v--;
/* v is now the number of days since 1 Jan 1900, and I have subtracted a   */
/* sly 1 to adjust for 1900 not being a leap year.                         */

#undef notleapyear

/* Adjust for a base at 1 Jan 1970                                       */

#define DAYS (17*(365*4+1)+2*365)
    t = min + 60*(hour + 24*(v - DAYS));
#undef DAYS
    {   int thi = ((int)t >> 16)*60;
        int tlo = ((int)t & 0xffff)*60 + sec;
        thi += (tlo >> 16) & 0xffff;
        t = (time_t)((thi << 16) | (tlo & 0xffff));
        if ((thi & 0xffff0000) != 0) return (time_t)-1;
    }

    timeptr->tm_sec = sec;
    timeptr->tm_min = min;
    timeptr->tm_hour = hour;
    timeptr->tm_mday = mday + 1;
    timeptr->tm_mon = mon;
    timeptr->tm_year = year;
    timeptr->tm_wday = (v + 1) % 7;
    timeptr->tm_yday = yday;
    timeptr->tm_isdst = -1;                  /* unavailable */

    return t;
    /* Now I know why Unix didn't have this                              */
}

char *asctime(const struct tm *timeptr)
{   static char _timebuf[26+(8+3*9+7)];  /* slop in case illegal args */
    sprintf(_timebuf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
       "SunMonTueWedThuFriSat" + (timeptr -> tm_wday)*3,
       "JanFebMarAprMayJunJulAugSepOctNovDecBad" + (timeptr -> tm_mon)*3,
       timeptr -> tm_mday,
       timeptr -> tm_hour, timeptr -> tm_min, timeptr -> tm_sec,
       timeptr -> tm_year + 1900);
    return _timebuf;
}

char *ctime(const time_t *timer)
{   return asctime(localtime(timer));
}

static struct tm *time_to_tm(struct tm *_tms, time_t t, int dst)
{
    int i = 0, yr;

    /* unix time already in seconds (since 1-Jan-1970) ... */
    _tms->tm_sec = t % 60; t /= 60;
    _tms->tm_min = t % 60; t /= 60;
    _tms->tm_hour = t % 24; t /= 24;
/* The next line converts *timer arg into days since 1-Jan-1900 from t which
   now holds days since 1-Jan-1970.  Now there are really only 17 leap years
   in this range 04,08,...,68 but we use 18 so that we do not have to do
   special case code for 1900 which was not a leap year.  Of course this
   cannot give problems as pre-1970 times are not representable in *timer. */
    t += 70*365 + 18;
    _tms->tm_wday = t % 7;               /* it just happens to be so */
    yr = 4 * (t / (365*4+1)); t %= (365*4+1);
    if (t >= 366) yr += (t-1) / 365, t = (t-1) % 365;
    _tms->tm_year = yr; /* Add in magic timebase */
    _tms->tm_yday = t;
    if ((yr & 3) != 0 && t >= 31+28) t++;
    while (t >= monlen[i]) t -= monlen[i++];
    _tms->tm_mday = t+1;
    _tms->tm_mon = i;
    _tms->tm_isdst = dst;                  /* unavailable */
    return _tms;
}

struct tm *gmtime(const time_t *timer)
{
    static struct tm _tms;
    time_t t;
    int territory, dst;
    int v;
    _kernel_swi_regs r;

    t = *timer;
    territory = __locales[N_LC_TIME];
    if (!territory)
        territory = -1;
    /* Read CMOS Ram for DST flag in bit 7 of loc 0xdc */
    v = _kernel_osbyte(161, 0xdc, 0);
    dst = v & (1 << 15);
    r.r[0] = territory;
    _kernel_swi(Territory_ReadTimeZones, &r, &r);
    t -= (dst ? r.r[3] : r.r[2]) / 100;
    if (t == (time_t)-1) {
        memset(&_tms, 0, sizeof(_tms));
        _tms.tm_mday = 1;
        return &_tms;
    }
    return time_to_tm(&_tms, t, 0); /* No DST component */
}

struct tm *localtime(const time_t *timer)
{
    time_t t;
    static struct tm _tms;
    int dst;
    int ordblock[9];
    unsigned bt[2];
    int territory;
    int i, v;
    _kernel_swi_regs r;

/* treat unset dates as 1-Jan-1900 - any better ideas? */
    t = *timer;
    if (t == (time_t)-1) {
        memset(&_tms, 0, sizeof(_tms));
        _tms.tm_mday = 1;
        return &_tms;
    }
    territory = __locales[N_LC_TIME];
    if (!territory)        /* If C locale use current configured territory */
        territory = -1;
    dst = -1;
    /* Read CMOS Ram for DST flag in bit 7 of loc 0xdc */
    v = _kernel_osbyte(161, 0xdc, 0);
    if (v >= 0)
        dst = ((v >> 8) >> 7) & 1;
    r.r[0] = territory;
    if (!_kernel_swi(Territory_ReadTimeZones, &r, &r)) {
        v = r.r[2];
        if (dst) v = r.r[3];
        t -= v / 100;
    }
    bt[0] = 0;
    bt[1] = 0;
#define secs0070 (((unsigned)86400)*(365*70+17))  /* less than 2^32 */
    for (i = 0; i < 100; i++) {
        bt[0] += t;
        if (bt[0] < (unsigned)t)
            bt[1]++;
        bt[0] += secs0070;
        if (bt[0] < (unsigned)secs0070)
            bt[1]++;
    }
    r.r[0] = territory;
    r.r[1] = (int)bt;
    r.r[2] = (int)ordblock;
    if (!_kernel_swi(Territory_ConvertTimeToOrdinals, &r, &r)) {
        _tms.tm_sec = ordblock[1];
        _tms.tm_min = ordblock[2];
        _tms.tm_hour = ordblock[3];
        _tms.tm_mday = ordblock[4];
        _tms.tm_mon = ordblock[5] - 1;
        _tms.tm_year = ordblock[6] - 1900;
        _tms.tm_wday = ordblock[7] - 1;
        _tms.tm_yday = ordblock[8] - 1;
        _tms.tm_isdst = dst;
        return &_tms;
    }
    return time_to_tm(&_tms, t, dst);
}

/* end of time.c */