Bug 1344014 - mktime behavior changed
Summary: mktime behavior changed
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: glibc
Version: 23
Hardware: Unspecified
OS: Unspecified
unspecified
high
Target Milestone: ---
Assignee: Carlos O'Donell
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-06-08 13:59 UTC by Steve Grubb
Modified: 2016-06-08 16:27 UTC (History)
9 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-06-08 15:31:38 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

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?


#define _XOPEN_SOURCE
#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):
glibc-2.22-17.fc23.x86_64

How reproducible:
always

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;

        clear_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.


Note You need to log in before you can comment on or make changes to this bug.