Lógica mktime rara con segundos negativos
He estado usando mktime / localtime para la administración del tiempo, incluida una gran aritmética en fechas / horas.
Noté algo muy extraño al proporcionarle a mktime una estructura que contiene valores negativos.
Toma el código de abajo. Hubo un cambio de horario de verano en LA el 3 de noviembre de 2013. Si especifico el tiempo en TM como la medianoche del 2013-11-04 y resto 24 horas, obtengo el mismo valor que la medianoche del 2013-11-03. Es una diferencia de 25 horas en UTC, lo cual está bien, ya que con isdst = -1 se podría decir que estamos mirando a 'wallclock-time'. Lo mismo si resto 1440 minutos (24 * 60). Pero, si resto 86400 (24 * 60 * 60) segundos, obtengo el 2013-11-03 a la 1am. Esa es la diferencia de 24 horas en UTC. Aquí está la salida del código de abajo:
2013-11-03 00:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000
2013-12--27 00:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000
2013-11-04 -24:00:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000
2013-11-04 00:-1440:00 (gmtoff=0, isdst=-1) -> 2013-11-03 00:00:00 (gmtoff=-25200, isdst=1) -> 1383462000
2013-11-04 00:00:-86400 (gmtoff=0, isdst=-1) -> 2013-11-03 01:00:00 (gmtoff=-25200, isdst=1) -> 1383465600
Para mí no tiene sentido, ¿por qué los segundos se tratan de manera diferente a minutos, horas y días? Miré al hombre y al estándar C pero no pude encontrar nada.
Este comportamiento rompe algunas de mis suposiciones y complica las cosas. Alguien sabe una buena alternativa a mktime / localtime (probé boost, ICU y tzcode, demasiado lento para lo que necesito).
Gracias de antemano por cualquier pensamiento :)
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* printtm(struct tm tm)
{
static char buf[100];
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d (gmtoff=%ld, isdst=%d)",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec,
tm.tm_gmtoff, tm.tm_isdst);
return buf;
}
void test(int y, int m, int d, int hh, int mm, int ss, int isdst)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_year = y - 1900;
tm.tm_mon = m - 1;
tm.tm_mday = d;
tm.tm_hour = hh;
tm.tm_min = mm;
tm.tm_sec = ss;
tm.tm_isdst = isdst;
printf("%s -> ", printtm(tm));
time_t t = mktime(&tm);
printf("%s -> %ld\n", printtm(tm), t);
}
int main()
{
setenv("TZ", ":America/Los_Angeles", 1);
tzset();
test(2013,11,03, 0,0,0, -1);
test(2013,12,-27, 0,0,0, -1);
test(2013,11,04, -24,0,0, -1);
test(2013,11,04, 0,-1440,0, -1);
test(2013,11,04, 0,0,-86400, -1);
return 0;
}