Skip to content

Commit 9adfc8e

Browse files
authored
Merge pull request syslog-ng#4975 from bazsi/strptime-to-accept-single-digit-hour-timezone-offsets
timeutils: accept single digit timezone hours in strptime()
2 parents e038d4b + 7d67c6e commit 9adfc8e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/timeutils/tests/test_wallclocktime.c

+31
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,37 @@ Test(wallclocktime, test_strptime_percent_Z_allows_timezone_to_be_optional)
243243
cr_expect_str_eq(end, "+3");
244244
}
245245

246+
Test(wallclocktime, test_strptime_percent_Z_numeric_formats)
247+
{
248+
WallClockTime wct = WALL_CLOCK_TIME_INIT;
249+
gchar *end;
250+
251+
/* NOTE: %Z accepts the same formats as %z, except that it allows the timezone to be optional */
252+
253+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +02:00");
254+
cr_expect(*end == '\0');
255+
cr_expect(wct.wct_gmtoff == 2*3600, "Unexpected timezone offset: %ld, expected 2*3600", wct.wct_gmtoff);
256+
257+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +0200");
258+
cr_expect(*end == '\0');
259+
cr_expect(wct.wct_gmtoff == 2*3600, "Unexpected timezone offset: %ld, expected 2*3600", wct.wct_gmtoff);
260+
261+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +2:00");
262+
cr_expect(*end == '\0');
263+
cr_expect(wct.wct_gmtoff == 2*3600, "Unexpected timezone offset: %ld, expected 2*3600", wct.wct_gmtoff);
264+
265+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +200");
266+
cr_expect(*end != '\0');
267+
268+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +02");
269+
cr_expect(*end == '\0');
270+
cr_expect(wct.wct_gmtoff == 2*3600, "Unexpected timezone offset: %ld, expected 2*3600", wct.wct_gmtoff);
271+
272+
end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S %Z", "Jan 16 2019 18:23:12 +2");
273+
cr_expect(*end != '\0');
274+
275+
}
276+
246277
Test(wallclocktime, test_strptime_zone_parsing_takes_daylight_saving_into_account_when_using_the_local_timezone)
247278
{
248279
WallClockTime wct = WALL_CLOCK_TIME_INIT;

lib/timeutils/wallclocktime.c

+9
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,13 @@ wall_clock_time_strptime(WallClockTime *wct, const gchar *format, const gchar *i
723723
i++;
724724
continue;
725725
}
726+
if (i == 1 && *bp == ':')
727+
{
728+
/* colon after the first digit, behave as if we had two digits */
729+
bp++;
730+
i++;
731+
continue;
732+
}
726733
if (i == 2 && *bp == ':')
727734
{
728735
bp++;
@@ -733,9 +740,11 @@ wall_clock_time_strptime(WallClockTime *wct, const gchar *format, const gchar *i
733740
switch (i)
734741
{
735742
case 2:
743+
/* just hours, HH */
736744
offs *= 3600;
737745
break;
738746
case 4:
747+
/* full offset HH:MM */
739748
i = offs % 100;
740749
offs /= 100;
741750
if (i >= 60)

0 commit comments

Comments
 (0)