Bug 1344014

Summary: mktime behavior changed
Product: [Fedora] Fedora Reporter: Steve Grubb <sgrubb>
Component: glibcAssignee: Carlos O'Donell <codonell>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: high Docs Contact:
Priority: unspecified    
Version: 23CC: arjun.is, codonell, dj, fweimer, jakub, law, mfabian, pfrankli, siddhesh
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-06-08 15:31:38 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

Description Steve Grubb 2016-06-08 13:59:05 UTC
Description of problem:
The behavior of mktime has changed between Fedora 23 and RHEL. I need the behavior to be consistent for the audit utilities. On Fedora 23 QE is reporting that the search tools cannot find events and on RHEL7 it works fine. In trying to locate the cause, I did run across something odd. The program below demonstrates the issue. Which one is the correct behavior, Fedora 23 or RHEL7?

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <locale.h>

time_t start_time = 0;

void init_time(const char *da, const char *ti)
        struct tm d;
        strptime(da, "%x", &d);
        strptime(ti, "%X", &d);
        start_time = mktime(&d);

int main(void)
        setlocale (LC_ALL, "en_US.utf8");

        init_time("06/07/2016", "17:00:00");
        printf("Time is: %s\n", ctime(&start_time));

        return 0;

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. run test program

Actual results:
Time is: Tue Jun  7 17:00:00 2016

Expected results:
Time is: Tue Jun  7 18:00:00 2016

Comment 1 Florian Weimer 2016-06-08 14:53:39 UTC
(In reply to Steve Grubb from comment #0)
> void init_time(const char *da, const char *ti)
> {
>         struct tm d;
>         strptime(da, "%x", &d);
>         strptime(ti, "%X", &d);
>         start_time = mktime(&d);
> }

Please test with an initializer:

        struct tm d = {};

I suspect you see essentially random differences due to uninitialized data.

Comment 2 Steve Grubb 2016-06-08 15:29:16 UTC
Thanks. That seems to make them both consistent.

But it leads to the actual problem of now the time is just wrong. If a user types 17:00:00 in as the time, why does mktime change it to 18:00:00? How to you get reliable time conversion in both December and June?

(I understand this is not a bz topic and will close this as not a bug.)

Comment 3 Steve Grubb 2016-06-08 16:27:11 UTC
This seems to be the recipe to get reliable time conversion:

        struct tm d;

        strptime(da, "%x", &d);
        start_time = mktime(&d);
        strptime(ti, "%X", &d);
        start_time = mktime(&d);

strptime is not setting the dst flag on the date conversion so you have to let mktime fix it, then you can do the time conversion and have correct time converted into time_t.