* improved ParseDateTest to check several different timezones
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38605 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include <parsedate.h>
|
#include <parsedate.h>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
#define ABSOLUTE 0
|
#define ABSOLUTE 0
|
||||||
@@ -18,7 +19,7 @@
|
|||||||
#define INVALID PARSEDATE_INVALID_DATE
|
#define INVALID PARSEDATE_INVALID_DATE
|
||||||
|
|
||||||
|
|
||||||
char*
|
const char*
|
||||||
parsedate_flags_to_string(time_t result, int flags)
|
parsedate_flags_to_string(time_t result, int flags)
|
||||||
{
|
{
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
@@ -42,49 +43,50 @@ int
|
|||||||
main(int argc, char** argv)
|
main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
time_t now = 1249471430;
|
time_t now = 1249471430;
|
||||||
// August 5th, 2009, 11:23:50
|
// August 5th, 2009, 11:23:50 GMT
|
||||||
|
|
||||||
struct test {
|
struct test {
|
||||||
time_t result;
|
time_t result;
|
||||||
const char* date;
|
const char* date;
|
||||||
int flags;
|
int flags;
|
||||||
|
bool uses_dst;
|
||||||
|
bool requires_zone_shift;
|
||||||
};
|
};
|
||||||
static const test kDates[] = {
|
static const test kDates[] = {
|
||||||
{1248739200, "last tuesday", DAY_RELATIVE},
|
{1248739200, "last tuesday", DAY_RELATIVE, true, true},
|
||||||
{1249430400, "today", DAY_RELATIVE},
|
{1249430400, "today", DAY_RELATIVE, true, true},
|
||||||
{1249948800, "next tuesday", DAY_RELATIVE},
|
{1249948800, "next tuesday", DAY_RELATIVE, true, true},
|
||||||
{1249344000, "tuesday", DAY_RELATIVE},
|
{1249344000, "tuesday", DAY_RELATIVE, true, true},
|
||||||
{1249467830, "last hour", MINUTE_RELATIVE},
|
{1249467830, "last hour", MINUTE_RELATIVE, true, false},
|
||||||
{1249475030, "1 hour", MINUTE_RELATIVE},
|
{1249475030, "1 hour", MINUTE_RELATIVE, true, false},
|
||||||
{now, "now", MINUTE_RELATIVE},
|
{now, "now", MINUTE_RELATIVE, true, false},
|
||||||
{219456000, "1976-12-15", ABSOLUTE},
|
{219456000, "1976-12-15", ABSOLUTE, false, true},
|
||||||
{219456000, "12/15/1976", ABSOLUTE},
|
{219456000, "12/15/1976", ABSOLUTE, false, true},
|
||||||
{219456000, "1976/12/15", ABSOLUTE},
|
{219456000, "1976/12/15", ABSOLUTE, false, true},
|
||||||
{219456000, "15.12.1976", ABSOLUTE},
|
{219456000, "15.12.1976", ABSOLUTE, false, true},
|
||||||
{208051200, "5.8.1976", ABSOLUTE},
|
{1061596800, "Sat, 08/23/2003", ABSOLUTE, true, true},
|
||||||
{1061596800, "Sat, 08/23/2003", ABSOLUTE},
|
{1061596800, "Sun, 08/23/2003", ABSOLUTE, true, true},
|
||||||
{1061596800, "Sun, 08/23/2003", ABSOLUTE},
|
{-1, "", INVALID, false, false},
|
||||||
{-1, "", INVALID},
|
{1249873200, "next monday 3:00", DAY_RELATIVE, true, true},
|
||||||
{1249873200, "next monday 3:00", DAY_RELATIVE},
|
{1249533720, "thursday 4:42", DAY_RELATIVE, true, true},
|
||||||
{1249533720, "thursday 4:42", DAY_RELATIVE},
|
{1249533740, "thursday +4:42:20", DAY_RELATIVE, true, true},
|
||||||
{1249533740, "thursday +4:42:20", DAY_RELATIVE},
|
{1249533720, "this thursday 4:42", DAY_RELATIVE, true, true},
|
||||||
{1249533720, "this thursday 4:42", DAY_RELATIVE},
|
{1249473950, "42 minutes", MINUTE_RELATIVE, true, false},
|
||||||
{1249473950, "42 minutes", MINUTE_RELATIVE},
|
{1250640000, "2 weeks", DAY_RELATIVE, true, true},
|
||||||
{1250640000, "2 weeks", DAY_RELATIVE},
|
{1249471730, "next 5 minutes", MINUTE_RELATIVE, true, false},
|
||||||
{1249471730, "next 5 minutes", MINUTE_RELATIVE},
|
{1249470530, "last 15 minutes", MINUTE_RELATIVE, true, false},
|
||||||
{1249470530, "last 15 minutes", MINUTE_RELATIVE},
|
{1249470530, "-15 minutes", MINUTE_RELATIVE, true, false},
|
||||||
{1249470530, "-15 minutes", MINUTE_RELATIVE},
|
{1249486380, "3:33pm GMT", DAY_RELATIVE, true, false},
|
||||||
{1249486380, "3:33pm GMT", DAY_RELATIVE},
|
{739706403, "Mon, June 10th, 1993 10:00:03 am GMT", ABSOLUTE, true, false},
|
||||||
{739706403, "Mon, June 10th, 1993 10:00:03 am GMT", ABSOLUTE},
|
{-1, "Sat, 16 Aug Ìîñêîâñêîå âðåìÿ (çèìà)", INVALID, false, false},
|
||||||
{-1, "Sat, 16 Aug Ìîñêîâñêîå âðåìÿ (çèìà)", INVALID},
|
{739706403, "Mon, June 10th, 1993 10:00:03 am UTC", ABSOLUTE, true, false},
|
||||||
{739706403, "Mon, June 10th, 1993 10:00:03 am UTC", ABSOLUTE},
|
{739699203, "Mon, June 10th, 1993 10:00:03 am CEST", ABSOLUTE, true, false},
|
||||||
{739699203, "Mon, June 10th, 1993 10:00:03 am CEST", ABSOLUTE},
|
{739706403, "Mon, June 10th, 1993 10:00:03 am +0000", ABSOLUTE, true, false},
|
||||||
{739706403, "Mon, June 10th, 1993 10:00:03 am +0000", ABSOLUTE},
|
{-1, "Mon, June 10th, 1993 10:00:03 am 0000", UNKNOWN, true, false},
|
||||||
{-1, "Mon, June 10th, 1993 10:00:03 am 0000", UNKNOWN},
|
{739702803, "Mon, June 10th, 1993 10:00:03 am +0100", ABSOLUTE, true, false},
|
||||||
{739702803, "Mon, June 10th, 1993 10:00:03 am +0100", ABSOLUTE},
|
{739731603, "Mon, June 10th, 1993 10:00:03 am -0700", ABSOLUTE, true, false},
|
||||||
{739731603, "Mon, June 10th, 1993 10:00:03 am -0700", ABSOLUTE},
|
{739654203, "Mon, June 10th, 1993 06:00:03 am ACDT", ABSOLUTE, true, false},
|
||||||
{739654203, "Mon, June 10th, 1993 06:00:03 am ACDT", ABSOLUTE},
|
{-1, NULL, 0, false}
|
||||||
{-1, NULL, 0}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool verbose = argc > 1;
|
bool verbose = argc > 1;
|
||||||
@@ -92,17 +94,43 @@ main(int argc, char** argv)
|
|||||||
if (verbose)
|
if (verbose)
|
||||||
printf("All times relative to: %s (%ld)\n", ctime(&now), now);
|
printf("All times relative to: %s (%ld)\n", ctime(&now), now);
|
||||||
|
|
||||||
|
struct TzInfo {
|
||||||
|
const char* timezone;
|
||||||
|
time_t std_offset;
|
||||||
|
time_t dst_offset;
|
||||||
|
};
|
||||||
|
TzInfo tzInfo[] = {
|
||||||
|
{ "GMT", 0 },
|
||||||
|
{ "Europe/Berlin", -1 * 3600, -2 * 3600 },
|
||||||
|
{ "Asia/Tokyo", -9 * 3600, -9 * 3600 },
|
||||||
|
{ "America/Los_Angeles", 8 * 3600, 7 * 3600 },
|
||||||
|
{ NULL, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int tz = 0; tzInfo[tz].timezone != NULL; tz++) {
|
||||||
|
printf("timezone: %s ...\n", tzInfo[tz].timezone);
|
||||||
|
setenv("TZ", tzInfo[tz].timezone, 1);
|
||||||
|
tzset();
|
||||||
|
|
||||||
for (int32 i = 0; kDates[i].date; i++) {
|
for (int32 i = 0; kDates[i].date; i++) {
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
time_t result = parsedate_etc(kDates[i].date, now, &flags);
|
time_t result = parsedate_etc(kDates[i].date, now, &flags);
|
||||||
|
|
||||||
|
time_t expectedResult = kDates[i].result;
|
||||||
|
if (kDates[i].requires_zone_shift) {
|
||||||
|
expectedResult +=
|
||||||
|
kDates[i].uses_dst
|
||||||
|
? tzInfo[tz].dst_offset
|
||||||
|
: tzInfo[tz].std_offset;
|
||||||
|
}
|
||||||
bool failure = false;
|
bool failure = false;
|
||||||
if (flags != kDates[i].flags || result != kDates[i].result)
|
if (flags != kDates[i].flags || result != expectedResult)
|
||||||
failure = true;
|
failure = true;
|
||||||
if (failure) {
|
if (failure) {
|
||||||
printf("FAILURE: parsing time at index %ld (should be %ld, %s)\n",
|
printf(
|
||||||
i, kDates[i].result,
|
"FAILURE: parsing time at index %ld (should be %ld, %s)\n",
|
||||||
parsedate_flags_to_string(kDates[i].result, flags));
|
i, expectedResult,
|
||||||
|
parsedate_flags_to_string(expectedResult, flags));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (verbose || failure) {
|
if (verbose || failure) {
|
||||||
@@ -111,6 +139,7 @@ main(int argc, char** argv)
|
|||||||
? "-\n" : ctime(&result));
|
? "-\n" : ctime(&result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user