Skip to content

Commit 247a1b4

Browse files
committed
Fix httpGetDateTime for dates in the far future (Issue #124)
1 parent c08c54e commit 247a1b4

3 files changed

Lines changed: 54 additions & 23 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ libcups v3.0.0 (YYYY-MM-DD)
1717
- Updated the `httpSetCookie` API to support multiple "Set-Cookie:" header
1818
values.
1919
- Updated `ippfind` to use the `cupsGetClock` API.
20+
- Fixed `httpGetDateTime` for dates in the far future (Issue #124)
2021
- Fixed input checks for `cupsCreateCredentials` and
2122
`cupsCreateCredentialsRequest` APIs (Issue #125)
2223
- Fixed return values of `ippDateToTime` when the timezone isn't GMT.

cups/http-support.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ httpGetDateTime(const char *s) // I - Date/time string
655655
char mon[16]; // Abbreviated month name
656656
int day, year; // Day of month and year
657657
int hour, min, sec; // Time
658-
int days; // Number of days since 1970
658+
long days; // Number of days since 1970
659659
static const int normal_days[] = // Days to a month, normal years
660660
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
661661
static const int leap_days[] = // Days to a month, leap years
@@ -694,14 +694,14 @@ httpGetDateTime(const char *s) // I - Date/time string
694694
else
695695
days = normal_days[i] + day - 1;
696696

697-
DEBUG_printf("4httpGetDateTime: days=%d", days);
697+
DEBUG_printf("4httpGetDateTime: days=%ld", days);
698698

699699
days += (year - 1970) * 365 + // 365 days per year (normally)
700700
((year - 1) / 4 - 492) - // + leap days
701701
((year - 1) / 100 - 19) + // - 100 year days
702702
((year - 1) / 400 - 4); // + 400 year days
703703

704-
DEBUG_printf("4httpGetDateTime: days=%d\n", days);
704+
DEBUG_printf("4httpGetDateTime: days=%ld\n", days);
705705

706706
return (days * 86400 + hour * 3600 + min * 60 + sec);
707707
}

cups/testhttp.c

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ static const char * const base64_tests[][2] =
206206
};
207207

208208

209+
//
210+
// Local functions...
211+
//
212+
213+
static bool test_date(time_t t);
214+
215+
209216
//
210217
// 'main()' - Main entry.
211218
//
@@ -260,27 +267,14 @@ main(int argc, // I - Number of command-line arguments
260267
failures = 0;
261268

262269
// httpGetDateString()/httpGetDateTime()
263-
testBegin("httpGetDateString()/httpGetDateTime()");
264-
265-
start = time(NULL);
266-
httpGetDateString(start, buffer, sizeof(buffer));
267-
current = httpGetDateTime(buffer);
268-
269-
i = (int)(current - start);
270-
if (i < 0)
271-
i = -i;
272-
273-
if (!i)
274-
testEnd(true);
275-
else
276-
{
270+
if (!test_date(0))
271+
failures ++;
272+
if (!test_date(time(NULL)))
273+
failures ++;
274+
if (!test_date(INT_MAX))
275+
failures ++;
276+
if (!test_date(UINT_MAX))
277277
failures ++;
278-
testEnd(false);
279-
testError("Difference is %d seconds, %02d:%02d:%02d.", i, i / 3600, (i / 60) % 60, i % 60);
280-
testError("httpGetDateString(%d) returned \"%s\"", (int)start, buffer);
281-
testError("httpGetDateTime(\"%s\") returned %d", buffer, (int)current);
282-
testError("httpGetDateString(%d) returned \"%s\"", (int)current, httpGetDateString(current, buffer, sizeof(buffer)));
283-
}
284278

285279
// httpDecode64()/httpEncode64()
286280
testBegin("httpDecode64()/httpEncode64()");
@@ -872,3 +866,39 @@ main(int argc, // I - Number of command-line arguments
872866

873867
return (0);
874868
}
869+
870+
871+
//
872+
// 'test_date()' - Test the date/time functions for a specific time.
873+
//
874+
875+
static bool // O - `true` on success, `false` on failure
876+
test_date(time_t t) // I - Time in seconds since Jan 1, 1970
877+
{
878+
char dateval[256]; // Date string
879+
time_t timeval; // Time value
880+
881+
882+
testBegin("httpGetDateString(%ld)", (long)t);
883+
if (httpGetDateString(t, dateval, sizeof(dateval)))
884+
{
885+
testEndMessage(true, dateval);
886+
}
887+
else
888+
{
889+
testEnd(false);
890+
return (false);
891+
}
892+
893+
testBegin("httpGetDateTime(\"%s\")", dateval);
894+
if ((timeval = httpGetDateTime(dateval)) == t)
895+
{
896+
testEnd(true);
897+
return (true);
898+
}
899+
else
900+
{
901+
testEndMessage(false, "got %ld, expected %ld", timeval, t);
902+
return (false);
903+
}
904+
}

0 commit comments

Comments
 (0)