fix: account for DST in XCCDF result timestamp offset#2367
Open
eran132 wants to merge 2 commits into
Open
Conversation
The start-time/end-time attributes on XCCDF TestResult elements are produced by _get_timestamp(). On glibc the offset was taken from the global `timezone` variable, which holds the standard-time offset and does not account for daylight saving time. The wall-clock fields come from localtime_r() and are DST-correct, so during DST the printed offset was wrong by one hour, making the timestamp denote the wrong instant (e.g. an America/Los_Angeles scan in summer reported -08:00 instead of -07:00). Use struct tm.tm_gmtoff when available; it carries the actual UTC offset in effect at the represented local time, DST included. A new HAVE_STRUCT_TM_TM_GMTOFF CMake feature check guards it, falling back to the timezone global (plus a DST hour) on platforms without tm_gmtoff such as the MSVC runtime. This also drops the FreeBSD-specific branch that previously re-added an hour to deliberately mirror the glibc bug. Fixes OpenSCAP#1972 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves cross-platform timezone offset handling when generating timestamps by preferring struct tm.tm_gmtoff when available and adding a configure-time check for it.
Changes:
- Use
tm_gmtoff(when present) for UTC offset, including DST, instead of platform-specific conditionals. - Add a CMake compile-time probe and config macro (
HAVE_STRUCT_TM_TM_GMTOFF) to detecttm_gmtoff. - Refine fallback offset logic using the global
timezonevalue plus DST adjustment.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/XCCDF/result.c | Switches to tm_gmtoff when available; adjusts fallback timezone logic and comments. |
| config.h.in | Adds HAVE_STRUCT_TM_TM_GMTOFF config define. |
| CMakeLists.txt | Adds check_struct_has_member probe for struct tm.tm_gmtoff. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1858
to
+1863
| /* Fallback for platforms without tm_gmtoff (e.g. the MSVC runtime). | ||
| * timezone is a global variable set by localtime(3) holding the offset | ||
| * of standard time; add an hour when daylight saving time is in effect. */ | ||
| long std_diff = timezone; | ||
| if (lt->tm_isdst > 0) | ||
| std_diff -= 3600; |
The fallback path subtracts 3600 seconds from `timezone`, but the comment described this as "add an hour", which reads as contradicting the code. Reword to explain that `timezone` is seconds west of UTC and DST shifts local time one hour east (closer to UTC), so the westward offset is reduced by 3600 seconds. No behavior change.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
oscapwritesstart-time/end-timeattributes on XCCDFTestResultelements via
_get_timestamp()insrc/XCCDF/result.c. On glibc the UTCoffset was derived from the global
timezonevariable, which holds thestandard-time offset and ignores daylight saving time. The wall-clock
fields (
tm_hour, ...) come fromlocaltime_r()and are DST-adjusted, soduring DST the offset and the time disagreed and the timestamp pointed to the
wrong instant — exactly the symptom in #1972 (an
America/Los_Angelesscan insummer emitted
-08:00instead of-07:00, an hour off).Fix
Use
struct tm.tm_gmtoffwhen the platform provides it.tm_gmtoffis theoffset east of UTC in seconds, already including DST, so it matches the actual
offset in effect at the represented local time.
HAVE_STRUCT_TM_TM_GMTOFFCMake feature check (check_struct_has_member).tm_gmtoff(e.g. the MSVC runtime) keep thetimezoneglobal path, now corrected to add an hour when
tm_isdst > 0.reproduce the glibc bug for consistency; FreeBSD now takes the correct
tm_gmtoffpath like glibc.Verification
A full project build was not possible in my environment (no
cmake/xmlsec1and no sudo), but I verified the change directly:
against libc's authoritative
strftime("%z")forAmerica/Los_Angeles,Europe/BerlinandUTC, in both summer and winter. Both thetm_gmtoffpath and the fallback path produce the correct offset in every case
(
-07:00/-08:00,+02:00/+01:00,+00:00).gcc -fsyntax-onlyon the editedresult.cagainstthe real project headers passes cleanly with
HAVE_STRUCT_TM_TM_GMTOFFdefined and undefined (both
#ifdefbranches).Fixes #1972