Blame src/functional/strftime.c

Dennis Wölfing 390c00
#include <limits.h>
Dennis Wölfing 390c00
#include <stdlib.h>
Dennis Wölfing 390c00
#include <string.h>
Dennis Wölfing 390c00
#include <time.h>
Dennis Wölfing 390c00
#include "test.h"
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static char buffer[100];
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static void checkStrftime(const char* format, const struct tm* tm,
Dennis Wölfing 390c00
		const char* expected) {
Dennis Wölfing 390c00
	size_t resultLength = strftime(buffer, sizeof(buffer), format, tm);
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	if (resultLength != 0 && strcmp(buffer, expected) != 0) {
Dennis Wölfing 390c00
		t_error("\"%s\": expected \"%s\", got \"%s\"\n", format, expected, buffer);
Dennis Wölfing 390c00
	} else if (resultLength == 0 && strlen(expected) != 0) {
Dennis Wölfing 390c00
		t_error("\"%s\": expected \"%s\", got nothing\n", format, expected);
Dennis Wölfing 390c00
	}
Dennis Wölfing 390c00
}
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static struct tm tm1 = {
Dennis Wölfing 390c00
	.tm_sec = 45,
Dennis Wölfing 390c00
	.tm_min = 23,
Dennis Wölfing 390c00
	.tm_hour = 13,
Dennis Wölfing 390c00
	.tm_mday = 3,
Dennis Wölfing 390c00
	.tm_mon = 0,
Dennis Wölfing 390c00
	.tm_year = 2016 - 1900,
Dennis Wölfing 390c00
	.tm_wday = 0,
Dennis Wölfing 390c00
	.tm_yday = 2,
Dennis Wölfing 390c00
	.tm_isdst = 0
Dennis Wölfing 390c00
};
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static struct tm tm2 = {
Dennis Wölfing 390c00
	.tm_sec = 53,
Dennis Wölfing 390c00
	.tm_min = 17,
Dennis Wölfing 390c00
	.tm_hour = 5,
Dennis Wölfing 390c00
	.tm_mday = 5,
Dennis Wölfing 390c00
	.tm_mon = 0,
Dennis Wölfing 390c00
	.tm_year = 10009 - 1900,
Dennis Wölfing 390c00
	.tm_wday = 1,
Dennis Wölfing 390c00
	.tm_yday = 4,
Dennis Wölfing 390c00
	.tm_isdst = 0
Dennis Wölfing 390c00
};
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static struct tm tm3 = {
Dennis Wölfing 390c00
	.tm_sec = 0,
Dennis Wölfing 390c00
	.tm_min = 0,
Dennis Wölfing 390c00
	.tm_hour = 12,
Dennis Wölfing 390c00
	.tm_mday = 23,
Dennis Wölfing 390c00
	.tm_mon = 1,
Dennis Wölfing 390c00
	.tm_year = 0 - 1900,
Dennis Wölfing 390c00
	.tm_wday = 3,
Dennis Wölfing 390c00
	.tm_yday = 53,
Dennis Wölfing 390c00
	.tm_isdst = 0
Dennis Wölfing 390c00
};
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static struct tm tm4 = {
Dennis Wölfing 390c00
	.tm_sec = 0,
Dennis Wölfing 390c00
	.tm_min = 0,
Dennis Wölfing 390c00
	.tm_hour = 0,
Dennis Wölfing 390c00
	.tm_mday = 1,
Dennis Wölfing 390c00
	.tm_mon = 0,
Dennis Wölfing 390c00
	.tm_year = -123 - 1900,
Dennis Wölfing 390c00
	.tm_wday = 1,
Dennis Wölfing 390c00
	.tm_yday = 0,
Dennis Wölfing 390c00
	.tm_isdst = 0
Dennis Wölfing 390c00
};
Dennis Wölfing 390c00
Dennis Wölfing 390c00
static struct tm tm5 = {
Dennis Wölfing 390c00
	.tm_sec = 0,
Dennis Wölfing 390c00
	.tm_min = 0,
Dennis Wölfing 390c00
	.tm_hour = 0,
Dennis Wölfing 390c00
	.tm_mday = 1,
Dennis Wölfing 390c00
	.tm_mon = 0,
Dennis Wölfing 390c00
	.tm_year = INT_MAX,
Dennis Wölfing 390c00
	.tm_wday = 3,
Dennis Wölfing 390c00
	.tm_yday = 0,
Dennis Wölfing 390c00
	.tm_isdst = 0
Dennis Wölfing 390c00
};
Dennis Wölfing 390c00
Dennis Wölfing 390c00
int main() {
Dennis Wölfing 390c00
	setenv("TZ", "UTC0", 1);
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%c", &tm1, "Sun Jan  3 13:23:45 2016");
Dennis Wölfing 390c00
	checkStrftime("%c", &tm2, "Mon Jan  5 05:17:53 +10009");
Dennis Wölfing 390c00
	checkStrftime("%c", &tm3, "Wed Feb 23 12:00:00 0000");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	// The POSIX.1-2008 standard does not specify the padding character for
Dennis Wölfing 390c00
	// "%C". The C standard requires that the number is padded by '0'.
Dennis Wölfing 390c00
	// See also http://austingroupbugs.net/view.php?id=1184
Dennis Wölfing 390c00
	checkStrftime("%C", &tm1, "20");
Dennis Wölfing 390c00
	checkStrftime("%03C", &tm1, "020");
Dennis Wölfing 390c00
	checkStrftime("%+3C", &tm1, "+20");
Dennis Wölfing 390c00
	checkStrftime("%C", &tm2, "100");
Dennis Wölfing 390c00
	checkStrftime("%C", &tm3, "00");
Dennis Wölfing 390c00
	checkStrftime("%01C", &tm3, "0");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%F", &tm1, "2016-01-03");
Dennis Wölfing 390c00
	checkStrftime("%012F", &tm1, "002016-01-03");
Dennis Wölfing 390c00
	checkStrftime("%+10F", &tm1, "2016-01-03");
Dennis Wölfing 390c00
	checkStrftime("%+11F", &tm1, "+2016-01-03");
Dennis Wölfing 390c00
	checkStrftime("%F", &tm2, "+10009-01-05");
Dennis Wölfing 390c00
	checkStrftime("%011F", &tm2, "10009-01-05");
Dennis Wölfing 390c00
	checkStrftime("%F", &tm3, "0000-02-23");
Dennis Wölfing 390c00
	checkStrftime("%01F", &tm3, "0-02-23");
Dennis Wölfing 390c00
	checkStrftime("%06F", &tm3, "0-02-23");
Dennis Wölfing 390c00
	checkStrftime("%010F", &tm3, "0000-02-23");
Dennis Wölfing 390c00
	checkStrftime("%F", &tm4, "-123-01-01");
Dennis Wölfing 390c00
	checkStrftime("%011F", &tm4, "-0123-01-01");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%g", &tm1, "15");
Dennis Wölfing 390c00
	checkStrftime("%g", &tm2, "09");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%G", &tm1, "2015");
Dennis Wölfing 390c00
	checkStrftime("%+5G", &tm1, "+2015");
Dennis Wölfing 390c00
	checkStrftime("%04G", &tm2, "10009");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%r", &tm1, "01:23:45 PM");
Dennis Wölfing 390c00
	checkStrftime("%r", &tm2, "05:17:53 AM");
Dennis Wölfing 390c00
	checkStrftime("%r", &tm3, "12:00:00 PM");
Dennis Wölfing 390c00
	checkStrftime("%r", &tm4, "12:00:00 AM");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	// The "%s" specifier was accepted by the Austin Group for the next POSIX.1
Dennis Wölfing 390c00
	// revision. See http://austingroupbugs.net/view.php?id=169
Dennis Wölfing 390c00
	checkStrftime("%s", &tm1, "1451827425");
Dennis Wölfing 390c00
	if (sizeof(time_t) * CHAR_BIT >= 64) {
Dennis Wölfing 390c00
		checkStrftime("%s", &tm2, "253686748673");
Dennis Wölfing 390c00
	}
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%T", &tm1, "13:23:45");
Dennis Wölfing 390c00
	checkStrftime("%T", &tm2, "05:17:53");
Dennis Wölfing 390c00
	checkStrftime("%T", &tm3, "12:00:00");
Dennis Wölfing 390c00
	checkStrftime("%T", &tm4, "00:00:00");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%U", &tm1, "01");
Dennis Wölfing 390c00
	checkStrftime("%U", &tm2, "01");
Dennis Wölfing 390c00
	checkStrftime("%U", &tm3, "08");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%V", &tm1, "53");
Dennis Wölfing 390c00
	checkStrftime("%V", &tm2, "02");
Dennis Wölfing 390c00
	checkStrftime("%V", &tm3, "08");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%W", &tm1, "00");
Dennis Wölfing 390c00
	checkStrftime("%W", &tm2, "01");
Dennis Wölfing 390c00
	checkStrftime("%W", &tm3, "08");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	checkStrftime("%x", &tm1, "01/03/16");
Dennis Wölfing 390c00
	checkStrftime("%X", &tm1, "13:23:45");
Dennis Wölfing 390c00
	checkStrftime("%y", &tm1, "16");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	// There is no standard that explicitly specifies the exact format of "%Y".
Dennis Wölfing 390c00
	// The C standard says that "%F" is equivalent to "%Y-%m-%d". The
Dennis Wölfing 390c00
	// POSIX.1-2008 standard says that "%F" is equivalent to "%+4Y-%m-%d".
Dennis Wölfing 390c00
	// This implies that to conform to both standards "%Y" needs to be
Dennis Wölfing 390c00
	// equivalent to "%+4Y".
Dennis Wölfing 390c00
	// See also http://austingroupbugs.net/view.php?id=739
Dennis Wölfing 390c00
	checkStrftime("%Y", &tm1, "2016");
Dennis Wölfing 390c00
	checkStrftime("%05Y", &tm1, "02016");
Dennis Wölfing 390c00
	checkStrftime("%+4Y", &tm1, "2016");
Dennis Wölfing 390c00
	checkStrftime("%+5Y", &tm1, "+2016");
Dennis Wölfing 390c00
	checkStrftime("%Y", &tm2, "+10009");
Dennis Wölfing 390c00
	checkStrftime("%05Y", &tm2, "10009");
Dennis Wölfing 390c00
	checkStrftime("%Y", &tm3, "0000");
Dennis Wölfing 390c00
	checkStrftime("%02Y", &tm3, "00");
Dennis Wölfing 390c00
	checkStrftime("%+5Y", &tm3, "+0000");
Dennis Wölfing 390c00
	checkStrftime("%Y", &tm4, "-123");
Dennis Wölfing 390c00
	checkStrftime("%+4Y", &tm4, "-123");
Dennis Wölfing 390c00
	checkStrftime("%+5Y", &tm4, "-0123");
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	if (INT_MAX == 0x7FFFFFFF) {
Dennis Wölfing 390c00
		// The standard does not specify any range for tm_year, so INT_MAX
Dennis Wölfing 390c00
		// should be valid.
Dennis Wölfing 390c00
		checkStrftime("%y", &tm5, "47");
Dennis Wölfing 390c00
		checkStrftime("%Y", &tm5, "+2147485547");
Dennis Wölfing 390c00
		checkStrftime("%011Y", &tm5, "02147485547");
Dennis Wölfing 390c00
		if (sizeof(time_t) * CHAR_BIT >= 64) {
Dennis Wölfing 390c00
			checkStrftime("%s", &tm5, "67768036160140800");
Dennis Wölfing 390c00
		}
Dennis Wölfing 390c00
	}
Dennis Wölfing 390c00
Dennis Wölfing 390c00
	return t_status;
Dennis Wölfing 390c00
}