pg: Add date type & httpdate tests

...I was hoping not to have to implement the date type, because date
conversions suck, but it turns out manned.org actually needs it.
(Only to then convert it into a Unix timestamp again, hmm, maybe this
string conversion isn't useful at all?)
This commit is contained in:
Yorhel 2025-02-24 11:51:43 +01:00
parent 8595c4ba64
commit fbbaa23842
4 changed files with 56 additions and 2 deletions

View file

@ -511,6 +511,34 @@ SENDFN(timestamp) {
fustr_writebeI(64, out, ts);
}
RECVFN(date) {
RLEN(4);
time_t ts = ((time_t)fu_frombeI(32, buf)) * 86400 + UNIX_PG_EPOCH;
struct tm tm;
gmtime_r(&ts, &tm);
return newSVpvf("%04d-%02d-%02d", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
}
SENDFN(date) {
int year, month, day;
if (sscanf(SvPV_nolen(val), "%4d-%2d-%2d", &year, &month, &day) != 3) SERR("invalid date format");
/* Can't use mktime() hackery here because libc has no UTC variant. Code
* below is adapted from PostgreSQL date2j() instead. */
if (month > 2) {
month += 1;
year += 4800;
} else {
month += 13;
year += 4799;
}
int century = year / 100;
int v = year * 365 - 32167;
v += year / 4 - century + century / 4;
v += 7834 * month / 256 + day;
v -= 2451545; /* Julian -> Postgres */
fustr_writebeI(32, out, v);
}
#undef UNIX_PG_EPOCH
#undef SIV
@ -615,7 +643,7 @@ SENDFN(timestamp) {
A( 1041, "_inet", 869 )\
B( 1042, "bpchar", text )\
B( 1043, "varchar", text )\
/* 1082 date */\
B( 1082, "date", date )\
/* 1083 time */\
B( 1114, "timestamp", timestamp)\
A( 1115, "_timestamp", 1114 )\