tm.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was contributed to The NetBSD Foundation by Klaus Klein.
*
* 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
*
* 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.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
/*
* Portable conversion routines for struct tm, replacing
* timegm() and strptime(), which are not available on all
* platforms and don't always behave the same way when they
* are.
*/
/*
* We do not implement alternate representations. However, we always
* check whether a given modifier is allowed for a certain conversion.
*/
#define ALT_E 0x01
#define ALT_O 0x02
#define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
#ifndef TM_YEAR_BASE
#define TM_YEAR_BASE 1900
#endif
static const char *day[7] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
static const char *abday[7] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *mon[12] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
static const char *abmon[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const char *am_pm[2] = {
"AM", "PM"
};
static int
int result = 0;
/* The limit also determines the number of valid digits. */
return (0);
do {
result *= 10;
rulim /= 10;
return (0);
return (1);
}
(86400 * (yday +
return (ret);
}
char *
char c, *ret;
const char *bp;
int alt_format, i, split_year = 0;
while ((c = *fmt) != '\0') {
/* Clear `alternate' modifier prior to new conversion. */
alt_format = 0;
/* Eat up white-space. */
if (isspace((unsigned char) c)) {
bp++;
fmt++;
continue;
}
if ((c = *fmt++) != '%')
goto literal;
case '%': /* "%%" is converted to "%". */
if (c != *bp++)
return (0);
break;
/*
* "Alternative" modifiers. Just set the appropriate flag
* and start over again.
*/
case 'E': /* "%E?" alternative conversion modifier. */
LEGAL_ALT(0);
alt_format |= ALT_E;
goto again;
case 'O': /* "%O?" alternative conversion modifier. */
LEGAL_ALT(0);
alt_format |= ALT_O;
goto again;
/*
* "Complex" conversion rules, implemented through recursion.
*/
case 'c': /* Date and time, using the locale's format. */
return (0);
break;
case 'D': /* The date as "%m/%d/%y". */
LEGAL_ALT(0);
return (0);
break;
case 'R': /* The time as "%H:%M". */
LEGAL_ALT(0);
return (0);
break;
case 'r': /* The time in 12-hour clock representation. */
LEGAL_ALT(0);
return (0);
break;
case 'T': /* The time as "%H:%M:%S". */
LEGAL_ALT(0);
return (0);
break;
case 'X': /* The time, using the locale's format. */
return (0);
break;
case 'x': /* The date, using the locale's format. */
return (0);
break;
/*
* "Elementary" conversion rules.
*/
case 'A': /* The day of week, using the locale's form. */
case 'a':
LEGAL_ALT(0);
for (i = 0; i < 7; i++) {
/* Full name. */
break;
/* Abbreviated name. */
break;
}
/* Nothing matched. */
if (i == 7)
return (0);
break;
case 'B': /* The month, using the locale's form. */
case 'b':
case 'h':
LEGAL_ALT(0);
for (i = 0; i < 12; i++) {
/* Full name. */
break;
/* Abbreviated name. */
break;
}
/* Nothing matched. */
if (i == 12)
return (0);
break;
case 'C': /* The century number. */
return (0);
if (split_year) {
} else {
split_year = 1;
}
break;
case 'd': /* The day of month. */
case 'e':
return (0);
break;
case 'k': /* The hour (24-hour clock representation). */
LEGAL_ALT(0);
/* FALLTHROUGH */
case 'H':
return (0);
break;
case 'l': /* The hour (12-hour clock representation). */
LEGAL_ALT(0);
/* FALLTHROUGH */
case 'I':
return (0);
break;
case 'j': /* The day of year. */
LEGAL_ALT(0);
return (0);
break;
case 'M': /* The minute. */
return (0);
break;
case 'm': /* The month. */
return (0);
break;
LEGAL_ALT(0);
/* AM? */
return (0);
break;
}
/* PM? */
return (0);
break;
}
/* Nothing matched. */
return (0);
case 'S': /* The seconds. */
return (0);
break;
case 'U': /* The week of year, beginning on sunday. */
case 'W': /* The week of year, beginning on monday. */
/*
* XXX This is bogus, as we can not assume any valid
* information present in the tm structure at this
* point to calculate a real value, so just check the
* range for now.
*/
return (0);
break;
case 'w': /* The day of week, beginning on sunday. */
return (0);
break;
case 'Y': /* The year. */
return (0);
break;
case 'y': /* The year within 100 years of the epoch. */
return (0);
if (split_year) {
break;
}
split_year = 1;
if (i <= 68)
else
break;
/*
* Miscellaneous conversions.
*/
case 'n': /* Any kind of white-space. */
case 't':
LEGAL_ALT(0);
bp++;
break;
default: /* Unknown/unsupported conversion. */
return (0);
}
}
/* LINTED functional specification */
return (ret);
}