OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [time/] [strptime.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*
/*
 * Copyright (c) 1999 Kungliga Tekniska Högskolan
 * Copyright (c) 1999 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 *
 *
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 *
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 *
 *
 * 3. Neither the name of KTH nor the names of its contributors may be
 * 3. Neither the name of KTH nor the names of its contributors may be
 *    used to endorse or promote products derived from this software without
 *    used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *    specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 
 
#include <stddef.h>
#include <stddef.h>
#include <stdio.h>
#include <stdio.h>
#include <time.h>
#include <time.h>
#include <string.h>
#include <string.h>
#include <ctype.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
static const char *abb_weekdays[] = {
static const char *abb_weekdays[] = {
    "Sun",
    "Sun",
    "Mon",
    "Mon",
    "Tue",
    "Tue",
    "Wed",
    "Wed",
    "Thu",
    "Thu",
    "Fri",
    "Fri",
    "Sat",
    "Sat",
    NULL
    NULL
};
};
 
 
static const char *full_weekdays[] = {
static const char *full_weekdays[] = {
    "Sunday",
    "Sunday",
    "Monday",
    "Monday",
    "Tuesday",
    "Tuesday",
    "Wednesday",
    "Wednesday",
    "Thursday",
    "Thursday",
    "Friday",
    "Friday",
    "Saturday",
    "Saturday",
    NULL
    NULL
};
};
 
 
static const char *abb_month[] = {
static const char *abb_month[] = {
    "Jan",
    "Jan",
    "Feb",
    "Feb",
    "Mar",
    "Mar",
    "Apr",
    "Apr",
    "May",
    "May",
    "Jun",
    "Jun",
    "Jul",
    "Jul",
    "Aug",
    "Aug",
    "Sep",
    "Sep",
    "Oct",
    "Oct",
    "Nov",
    "Nov",
    "Dec",
    "Dec",
    NULL
    NULL
};
};
 
 
static const char *full_month[] = {
static const char *full_month[] = {
    "January",
    "January",
    "February",
    "February",
    "March",
    "March",
    "April",
    "April",
    "May",
    "May",
    "June",
    "June",
    "July",
    "July",
    "August",
    "August",
    "September",
    "September",
    "October",
    "October",
    "November",
    "November",
    "December",
    "December",
    NULL,
    NULL,
};
};
 
 
static const char *ampm[] = {
static const char *ampm[] = {
    "am",
    "am",
    "pm",
    "pm",
    NULL
    NULL
};
};
 
 
/*
/*
 * tm_year is relative this year
 * tm_year is relative this year
 */
 */
const int tm_year_base = 1900;
const int tm_year_base = 1900;
 
 
/*
/*
 * Return TRUE iff `year' was a leap year.
 * Return TRUE iff `year' was a leap year.
 * Needed for strptime.
 * Needed for strptime.
 */
 */
static int
static int
is_leap_year (int year)
is_leap_year (int year)
{
{
    return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
    return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
}
}
 
 
/* Needed for strptime. */
/* Needed for strptime. */
static int
static int
match_string (const char **buf, const char **strs)
match_string (const char **buf, const char **strs)
{
{
    int i = 0;
    int i = 0;
 
 
    for (i = 0; strs[i] != NULL; ++i) {
    for (i = 0; strs[i] != NULL; ++i) {
        int len = strlen (strs[i]);
        int len = strlen (strs[i]);
 
 
        if (strncasecmp (*buf, strs[i], len) == 0) {
        if (strncasecmp (*buf, strs[i], len) == 0) {
            *buf += len;
            *buf += len;
            return i;
            return i;
        }
        }
    }
    }
    return -1;
    return -1;
}
}
 
 
/* Needed for strptime. */
/* Needed for strptime. */
static int
static int
first_day (int year)
first_day (int year)
{
{
    int ret = 4;
    int ret = 4;
 
 
    for (; year > 1970; --year)
    for (; year > 1970; --year)
        ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
        ret = (ret + 365 + is_leap_year (year) ? 1 : 0) % 7;
    return ret;
    return ret;
}
}
 
 
/*
/*
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Needed for strptime
 * Needed for strptime
 */
 */
 
 
static void
static void
set_week_number_sun (struct tm *timeptr, int wnum)
set_week_number_sun (struct tm *timeptr, int wnum)
{
{
    int fday = first_day (timeptr->tm_year + tm_year_base);
    int fday = first_day (timeptr->tm_year + tm_year_base);
 
 
    timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
    timeptr->tm_yday = wnum * 7 + timeptr->tm_wday - fday;
    if (timeptr->tm_yday < 0) {
    if (timeptr->tm_yday < 0) {
        timeptr->tm_wday = fday;
        timeptr->tm_wday = fday;
        timeptr->tm_yday = 0;
        timeptr->tm_yday = 0;
    }
    }
}
}
 
 
/*
/*
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Needed for strptime
 * Needed for strptime
 */
 */
 
 
static void
static void
set_week_number_mon (struct tm *timeptr, int wnum)
set_week_number_mon (struct tm *timeptr, int wnum)
{
{
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
 
 
    timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
    timeptr->tm_yday = wnum * 7 + (timeptr->tm_wday + 6) % 7 - fday;
    if (timeptr->tm_yday < 0) {
    if (timeptr->tm_yday < 0) {
        timeptr->tm_wday = (fday + 1) % 7;
        timeptr->tm_wday = (fday + 1) % 7;
        timeptr->tm_yday = 0;
        timeptr->tm_yday = 0;
    }
    }
}
}
 
 
/*
/*
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Set `timeptr' given `wnum' (week number [0, 53])
 * Needed for strptime
 * Needed for strptime
 */
 */
static void
static void
set_week_number_mon4 (struct tm *timeptr, int wnum)
set_week_number_mon4 (struct tm *timeptr, int wnum)
{
{
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
    int fday = (first_day (timeptr->tm_year + tm_year_base) + 6) % 7;
    int offset = 0;
    int offset = 0;
 
 
    if (fday < 4)
    if (fday < 4)
        offset += 7;
        offset += 7;
 
 
    timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
    timeptr->tm_yday = offset + (wnum - 1) * 7 + timeptr->tm_wday - fday;
    if (timeptr->tm_yday < 0) {
    if (timeptr->tm_yday < 0) {
        timeptr->tm_wday = fday;
        timeptr->tm_wday = fday;
        timeptr->tm_yday = 0;
        timeptr->tm_yday = 0;
    }
    }
}
}
 
 
/* strptime: roken */
/* strptime: roken */
//extern "C"
//extern "C"
char *
char *
//strptime (const char *buf, const char *format, struct tm *timeptr)
//strptime (const char *buf, const char *format, struct tm *timeptr)
_DEFUN (strptime, (buf, format, timeptr),
_DEFUN (strptime, (buf, format, timeptr),
        _CONST char *buf _AND
        _CONST char *buf _AND
        _CONST char *format _AND
        _CONST char *format _AND
        struct tm *timeptr)
        struct tm *timeptr)
{
{
    char c;
    char c;
 
 
    for (; (c = *format) != '\0'; ++format) {
    for (; (c = *format) != '\0'; ++format) {
        char *s;
        char *s;
        int ret;
        int ret;
 
 
        if (isspace (c)) {
        if (isspace (c)) {
            while (isspace (*buf))
            while (isspace (*buf))
                ++buf;
                ++buf;
        } else if (c == '%' && format[1] != '\0') {
        } else if (c == '%' && format[1] != '\0') {
            c = *++format;
            c = *++format;
            if (c == 'E' || c == 'O')
            if (c == 'E' || c == 'O')
                c = *++format;
                c = *++format;
            switch (c) {
            switch (c) {
            case 'A' :
            case 'A' :
                ret = match_string (&buf, full_weekdays);
                ret = match_string (&buf, full_weekdays);
                if (ret < 0)
                if (ret < 0)
                    return NULL;
                    return NULL;
                timeptr->tm_wday = ret;
                timeptr->tm_wday = ret;
                break;
                break;
            case 'a' :
            case 'a' :
                ret = match_string (&buf, abb_weekdays);
                ret = match_string (&buf, abb_weekdays);
                if (ret < 0)
                if (ret < 0)
                    return NULL;
                    return NULL;
                timeptr->tm_wday = ret;
                timeptr->tm_wday = ret;
                break;
                break;
            case 'B' :
            case 'B' :
                ret = match_string (&buf, full_month);
                ret = match_string (&buf, full_month);
                if (ret < 0)
                if (ret < 0)
                    return NULL;
                    return NULL;
                timeptr->tm_mon = ret;
                timeptr->tm_mon = ret;
                break;
                break;
            case 'b' :
            case 'b' :
            case 'h' :
            case 'h' :
                ret = match_string (&buf, abb_month);
                ret = match_string (&buf, abb_month);
                if (ret < 0)
                if (ret < 0)
                    return NULL;
                    return NULL;
                timeptr->tm_mon = ret;
                timeptr->tm_mon = ret;
                break;
                break;
            case 'C' :
            case 'C' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_year = (ret * 100) - tm_year_base;
                timeptr->tm_year = (ret * 100) - tm_year_base;
                buf = s;
                buf = s;
                break;
                break;
            case 'c' :          /* %a %b %e %H:%M:%S %Y */
            case 'c' :          /* %a %b %e %H:%M:%S %Y */
                s = strptime (buf, "%a %b %e %H:%M:%S %Y", timeptr);
                s = strptime (buf, "%a %b %e %H:%M:%S %Y", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'D' :          /* %m/%d/%y */
            case 'D' :          /* %m/%d/%y */
                s = strptime (buf, "%m/%d/%y", timeptr);
                s = strptime (buf, "%m/%d/%y", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'd' :
            case 'd' :
            case 'e' :
            case 'e' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_mday = ret;
                timeptr->tm_mday = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'H' :
            case 'H' :
            case 'k' :
            case 'k' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_hour = ret;
                timeptr->tm_hour = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'I' :
            case 'I' :
            case 'l' :
            case 'l' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                if (ret == 12)
                if (ret == 12)
                    timeptr->tm_hour = 0;
                    timeptr->tm_hour = 0;
                else
                else
                    timeptr->tm_hour = ret;
                    timeptr->tm_hour = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'j' :
            case 'j' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_yday = ret - 1;
                timeptr->tm_yday = ret - 1;
                buf = s;
                buf = s;
                break;
                break;
            case 'm' :
            case 'm' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_mon = ret - 1;
                timeptr->tm_mon = ret - 1;
                buf = s;
                buf = s;
                break;
                break;
            case 'M' :
            case 'M' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_min = ret;
                timeptr->tm_min = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'n' :
            case 'n' :
                if (*buf == '\n')
                if (*buf == '\n')
                    ++buf;
                    ++buf;
                else
                else
                    return NULL;
                    return NULL;
                break;
                break;
            case 'p' :
            case 'p' :
                ret = match_string (&buf, ampm);
                ret = match_string (&buf, ampm);
                if (ret < 0)
                if (ret < 0)
                    return NULL;
                    return NULL;
                if (timeptr->tm_hour == 0) {
                if (timeptr->tm_hour == 0) {
                    if (ret == 1)
                    if (ret == 1)
                        timeptr->tm_hour = 12;
                        timeptr->tm_hour = 12;
                } else
                } else
                    timeptr->tm_hour += 12;
                    timeptr->tm_hour += 12;
                break;
                break;
            case 'r' :          /* %I:%M:%S %p */
            case 'r' :          /* %I:%M:%S %p */
                s = strptime (buf, "%I:%M:%S %p", timeptr);
                s = strptime (buf, "%I:%M:%S %p", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'R' :          /* %H:%M */
            case 'R' :          /* %H:%M */
                s = strptime (buf, "%H:%M", timeptr);
                s = strptime (buf, "%H:%M", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'S' :
            case 'S' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_sec = ret;
                timeptr->tm_sec = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 't' :
            case 't' :
                if (*buf == '\t')
                if (*buf == '\t')
                    ++buf;
                    ++buf;
                else
                else
                    return NULL;
                    return NULL;
                break;
                break;
            case 'T' :          /* %H:%M:%S */
            case 'T' :          /* %H:%M:%S */
            case 'X' :
            case 'X' :
                s = strptime (buf, "%H:%M:%S", timeptr);
                s = strptime (buf, "%H:%M:%S", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'u' :
            case 'u' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_wday = ret - 1;
                timeptr->tm_wday = ret - 1;
                buf = s;
                buf = s;
                break;
                break;
            case 'w' :
            case 'w' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_wday = ret;
                timeptr->tm_wday = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'U' :
            case 'U' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                set_week_number_sun (timeptr, ret);
                set_week_number_sun (timeptr, ret);
                buf = s;
                buf = s;
                break;
                break;
            case 'V' :
            case 'V' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                set_week_number_mon4 (timeptr, ret);
                set_week_number_mon4 (timeptr, ret);
                buf = s;
                buf = s;
                break;
                break;
            case 'W' :
            case 'W' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                set_week_number_mon (timeptr, ret);
                set_week_number_mon (timeptr, ret);
                buf = s;
                buf = s;
                break;
                break;
            case 'x' :
            case 'x' :
                s = strptime (buf, "%Y:%m:%d", timeptr);
                s = strptime (buf, "%Y:%m:%d", timeptr);
                if (s == NULL)
                if (s == NULL)
                    return NULL;
                    return NULL;
                buf = s;
                buf = s;
                break;
                break;
            case 'y' :
            case 'y' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                if (ret < 70)
                if (ret < 70)
                    timeptr->tm_year = 100 + ret;
                    timeptr->tm_year = 100 + ret;
                else
                else
                    timeptr->tm_year = ret;
                    timeptr->tm_year = ret;
                buf = s;
                buf = s;
                break;
                break;
            case 'Y' :
            case 'Y' :
                ret = strtol (buf, &s, 10);
                ret = strtol (buf, &s, 10);
                if (s == buf)
                if (s == buf)
                    return NULL;
                    return NULL;
                timeptr->tm_year = ret - tm_year_base;
                timeptr->tm_year = ret - tm_year_base;
                buf = s;
                buf = s;
                break;
                break;
            case 'Z' :
            case 'Z' :
                /* Unsupported. Just ignore.  */
                /* Unsupported. Just ignore.  */
                break;
                break;
            case '\0' :
            case '\0' :
                --format;
                --format;
                /* FALLTHROUGH */
                /* FALLTHROUGH */
            case '%' :
            case '%' :
                if (*buf == '%')
                if (*buf == '%')
                    ++buf;
                    ++buf;
                else
                else
                    return NULL;
                    return NULL;
                break;
                break;
            default :
            default :
                if (*buf == '%' || *++buf == c)
                if (*buf == '%' || *++buf == c)
                    ++buf;
                    ++buf;
                else
                else
                    return NULL;
                    return NULL;
                break;
                break;
            }
            }
        } else {
        } else {
            if (*buf == c)
            if (*buf == c)
                ++buf;
                ++buf;
            else
            else
                return NULL;
                return NULL;
        }
        }
    }
    }
    return (char *)buf;
    return (char *)buf;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.