diff options
author | Björn-Egil Dahlberg <[email protected]> | 2011-08-09 18:27:20 +0200 |
---|---|---|
committer | Björn-Egil Dahlberg <[email protected]> | 2011-12-02 15:41:13 +0100 |
commit | 1eef7653baae2046c26cfe8fb5fc91d6f9fa709b (patch) | |
tree | 13a5370545695603c824f68ab315182e50b862ad | |
parent | 82c79cc3b00175ac3ff6f892736c95fe3b00ea06 (diff) | |
download | otp-1eef7653baae2046c26cfe8fb5fc91d6f9fa709b.tar.gz otp-1eef7653baae2046c26cfe8fb5fc91d6f9fa709b.tar.bz2 otp-1eef7653baae2046c26cfe8fb5fc91d6f9fa709b.zip |
Workaround for the -1 problem of mktime
-rw-r--r-- | erts/emulator/beam/erl_time_sup.c | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/erts/emulator/beam/erl_time_sup.c b/erts/emulator/beam/erl_time_sup.c index bc09ba8001..829ec32288 100644 --- a/erts/emulator/beam/erl_time_sup.c +++ b/erts/emulator/beam/erl_time_sup.c @@ -585,6 +585,41 @@ static const int mdays[14] = {0, 31, 28, 31, 30, 31, 30, #define BASEYEAR INT_MIN +/* A more "clever" mktime + * return 1, if successful + * return -1, if not successful + */ + +static int erl_mktime(time_t *c, struct tm *tm) { + time_t clock; + + clock = mktime(tm); + + if (clock != -1) { + *c = clock; + return 1; + } + + /* in rare occasions mktime returns -1 + * when a correct value has been entered + * + * decrease seconds with one second + * if the result is -2, epochs should be -1 + */ + + tm->tm_sec = tm->tm_sec - 1; + clock = mktime(tm); + tm->tm_sec = tm->tm_sec + 1; + + *c = -1; + + if (clock == -2) { + return 1; + } + + return -1; +} + /* * gregday * @@ -644,15 +679,18 @@ local_to_univ(Sint *year, Sint *month, Sint *day, t.tm_min = *minute; t.tm_sec = *second; t.tm_isdst = isdst; - the_clock = mktime(&t); - if (the_clock == -1) { + + /* the nature of mktime makes this a bit interesting, + * up to four mktime calls could happen here + */ + + if (erl_mktime(&the_clock, &t) < 0) { if (isdst) { /* If this is a timezone without DST and the OS (correctly) refuses to give us a DST time, we simulate the Linux/Solaris behaviour of giving the same data as if is_dst was not set. */ t.tm_isdst = 0; - the_clock = mktime(&t); - if (the_clock == -1) { + if (erl_mktime(&the_clock, &t)) { /* Failed anyway, something else is bad - will be a badarg */ return 0; } |