The underlying function should return -1 and set errno when given invalid
strings, but we were not looking and have decided on 0 for error.
It would be a pain to change this function to return -1. Apart from the
API fuss, it is sometimes used unchecked to set an unsigned number and
an unchecked 0 is better than UINT*_MAX in those contexts.
It is probably not easy to get an -1 from a timegm() -- most
implementations will happily convert overflows for you, so e.g. the
15th month would be March of the next year. But EOVERFLOW is mentioned
in the manpages.
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
time_t ldb_string_to_time(const char *s)
{
struct tm tm;
+ time_t t;
if (s == NULL) return 0;
tm.tm_year -= 1900;
tm.tm_mon -= 1;
- return timegm(&tm);
+ t = timegm(&tm);
+
+ if (t == (time_t)-1 && errno != 0) {
+ /*
+ * timegm() returns -1 on error, but also for '19691231235959.0Z'.
+ */
+ return 0;
+ }
+ return t;
}
/*
def test_string_to_time(self):
self.assertEqual(0, ldb.string_to_time("19700101000000.0Z"))
+ self.assertEqual(-1, ldb.string_to_time("19691231235959.0Z"))
self.assertEqual(1195499412, ldb.string_to_time("20071119191012.0Z"))
self.assertEqual(-62167219200, ldb.string_to_time("00000101000000.0Z"))