BUGFIX nc_time2datetime() wrong format of timezone shift
- '--01:00' was printed instead of '-01:00'
- missing check for negative years
- unnecessary check for daylight saving info
diff --git a/src/time.c b/src/time.c
index 9039a80..4d946fa 100644
--- a/src/time.c
+++ b/src/time.c
@@ -127,23 +127,25 @@
}
}
- if (tm.tm_isdst < 0) {
- zoneshift = NULL;
+ /* years cannot be negative */
+ if (tm.tm_year < -1900) {
+ ERRARG("time");
+ return NULL;
+ }
+
+ if (tm.tm_gmtoff == 0) {
+ /* time is Zulu (UTC) */
+ if (asprintf(&zoneshift, "Z") == -1) {
+ ERRMEM;
+ return NULL;
+ }
} else {
- if (tm.tm_gmtoff == 0) {
- /* time is Zulu (UTC) */
- if (asprintf(&zoneshift, "Z") == -1) {
- ERRMEM;
- return NULL;
- }
- } else {
- zonediff = tm.tm_gmtoff;
- zonediff_h = zonediff / 60 / 60;
- zonediff_m = zonediff / 60 % 60;
- if (asprintf(&zoneshift, "%s%02d:%02d", (zonediff < 0) ? "-" : "+", zonediff_h, zonediff_m) == -1) {
- ERRMEM;
- return NULL;
- }
+ zonediff = tm.tm_gmtoff;
+ zonediff_h = zonediff / 60 / 60;
+ zonediff_m = zonediff / 60 % 60;
+ if (asprintf(&zoneshift, "%+02d:%02d", zonediff_h, zonediff_m) == -1) {
+ ERRMEM;
+ return NULL;
}
}
@@ -155,7 +157,7 @@
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
- (zoneshift == NULL) ? "" : zoneshift);
+ zoneshift);
} else if (asprintf(&date, "%04d-%02d-%02dT%02d:%02d:%02d%s",
tm.tm_year + 1900,
tm.tm_mon + 1,
@@ -163,7 +165,7 @@
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
- (zoneshift == NULL) ? "" : zoneshift) == -1) {
+ zoneshift) == -1) {
free(zoneshift);
ERRMEM;
return NULL;