Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mktime invalid normalization #28

Open
anglov opened this issue Dec 3, 2019 · 5 comments
Open

mktime invalid normalization #28

anglov opened this issue Dec 3, 2019 · 5 comments

Comments

@anglov
Copy link
Member

anglov commented Dec 3, 2019

mktime incorrectly normalizes struct tm. When year is leap year and you normalize month interval, one day is lost.

eg.:

for (int i = 0; i < 60; ++i) {
    tm.tm_mon += 1;
    time = mktime(&tm);
    printf("%s", ctime(&time));
}

results with:
Tue Feb 1 06:00:00 2000
Wed Mar 1 06:00:00 2000
Sat Apr 1 06:00:00 2000
Mon May 1 06:00:00 2000
Thu Jun 1 06:00:00 2000
Sat Jul 1 06:00:00 2000
Tue Aug 1 06:00:00 2000
Fri Sep 1 06:00:00 2000
Sun Oct 1 06:00:00 2000
Wed Nov 1 06:00:00 2000
Fri Dec 1 06:00:00 2000
Sun Dec 31 06:00:00 2000
Tue Jan 30 06:00:00 2001

Fri Mar 2 06:00:00 2001
Mon Apr 2 06:00:00 2001
Wed May 2 06:00:00 2001
Sat Jun 2 06:00:00 2001
Mon Jul 2 06:00:00 2001
Thu Aug 2 06:00:00 2001
Sun Sep 2 06:00:00 2001
Tue Oct 2 06:00:00 2001
Fri Nov 2 06:00:00 2001
Sun Dec 2 06:00:00 2001
Wed Jan 2 06:00:00 2002
Sat Feb 2 06:00:00 2002
Sat Mar 2 06:00:00 2002
Tue Apr 2 06:00:00 2002
Thu May 2 06:00:00 2002
Sun Jun 2 06:00:00 2002
Tue Jul 2 06:00:00 2002
Fri Aug 2 06:00:00 2002
Mon Sep 2 06:00:00 2002
Wed Oct 2 06:00:00 2002
Sat Nov 2 06:00:00 2002
Mon Dec 2 06:00:00 2002
Thu Jan 2 06:00:00 2003
Sun Feb 2 06:00:00 2003
Sun Mar 2 06:00:00 2003
Wed Apr 2 06:00:00 2003
Fri May 2 06:00:00 2003
Mon Jun 2 06:00:00 2003
Wed Jul 2 06:00:00 2003
Sat Aug 2 06:00:00 2003
Tue Sep 2 06:00:00 2003
Thu Oct 2 06:00:00 2003
Sun Nov 2 06:00:00 2003
Tue Dec 2 06:00:00 2003
Fri Jan 2 06:00:00 2004
Mon Feb 2 06:00:00 2004
Tue Mar 2 06:00:00 2004
Fri Apr 2 06:00:00 2004
Sun May 2 06:00:00 2004
Wed Jun 2 06:00:00 2004
Fri Jul 2 06:00:00 2004
Mon Aug 2 06:00:00 2004
Thu Sep 2 06:00:00 2004
Sat Oct 2 06:00:00 2004
Tue Nov 2 06:00:00 2004
Thu Dec 2 06:00:00 2004
Sat Jan 1 06:00:00 2005

@agkaminski
Copy link
Member

Fixed in 0cde62e. Still possible problem if days or hours cause a year change, so I am leaving this one open.

DawidSzpejna pushed a commit that referenced this issue Jul 26, 2022
This supersedes GH-28 as the problem exists on all platforms.
gerard5 pushed a commit that referenced this issue Feb 21, 2023
psh: fixed restore oryginal terminal settings
@badochov
Copy link
Contributor

is it still a problem after recent time related changes?

@anglov
Copy link
Member Author

anglov commented Oct 10, 2023

If so, it would be great to have automatic test for validation.

@jmaksymowicz
Copy link
Contributor

Yes, it seems that the problem is fixed now. Tested libphoenix at 023e717
Code:

struct tm tm = {0};
tm.tm_year = 100;
tm.tm_mday = 1;
tm.tm_hour = 6;
for (int i = 0; i < 60; ++i) {
    tm.tm_mon += 1;
    time_t time = mktime(&tm);
    printf("%s", ctime(&time));
}

Result:

Tue Feb 1 06:00:00 2000
Wed Mar 1 06:00:00 2000
Sat Apr 1 06:00:00 2000
Mon May 1 06:00:00 2000
Thu Jun 1 06:00:00 2000
Sat Jul 1 06:00:00 2000
Tue Aug 1 06:00:00 2000
Fri Sep 1 06:00:00 2000
Sun Oct 1 06:00:00 2000
Wed Nov 1 06:00:00 2000
Fri Dec 1 06:00:00 2000
Mon Jan 1 06:00:00 2001
Thu Feb 1 06:00:00 2001
Thu Mar 1 06:00:00 2001
Sun Apr 1 06:00:00 2001
Tue May 1 06:00:00 2001
Fri Jun 1 06:00:00 2001
Sun Jul 1 06:00:00 2001
Wed Aug 1 06:00:00 2001
Sat Sep 1 06:00:00 2001
Mon Oct 1 06:00:00 2001
Thu Nov 1 06:00:00 2001
Sat Dec 1 06:00:00 2001
Tue Jan 1 06:00:00 2002
Fri Feb 1 06:00:00 2002
Fri Mar 1 06:00:00 2002
Mon Apr 1 06:00:00 2002
Wed May 1 06:00:00 2002
Sat Jun 1 06:00:00 2002
Mon Jul 1 06:00:00 2002
Thu Aug 1 06:00:00 2002
Sun Sep 1 06:00:00 2002
Tue Oct 1 06:00:00 2002
Fri Nov 1 06:00:00 2002
Sun Dec 1 06:00:00 2002
Wed Jan 1 06:00:00 2003
Sat Feb 1 06:00:00 2003
Sat Mar 1 06:00:00 2003
Tue Apr 1 06:00:00 2003
Thu May 1 06:00:00 2003
Sun Jun 1 06:00:00 2003
Tue Jul 1 06:00:00 2003
Fri Aug 1 06:00:00 2003
Mon Sep 1 06:00:00 2003
Wed Oct 1 06:00:00 2003
Sat Nov 1 06:00:00 2003
Mon Dec 1 06:00:00 2003
Thu Jan 1 06:00:00 2004
Sun Feb 1 06:00:00 2004
Mon Mar 1 06:00:00 2004
Thu Apr 1 06:00:00 2004
Sat May 1 06:00:00 2004
Tue Jun 1 06:00:00 2004
Thu Jul 1 06:00:00 2004
Sun Aug 1 06:00:00 2004
Wed Sep 1 06:00:00 2004
Fri Oct 1 06:00:00 2004
Mon Nov 1 06:00:00 2004
Wed Dec 1 06:00:00 2004
Sat Jan 1 06:00:00 2005

I'm working on automatic tests for libphoenix time functions: phoenix-rtos/phoenix-rtos-tests#255 so I'll try to add this as a test case.

@anglov
Copy link
Member Author

anglov commented Oct 12, 2023

I believe this task reamains open because of other possibilities to year changes than month. Original normalisation issue was fixed here:
#29

I'm really grateful that you add this as test (as this kind of things are crucial and problems can be really painful) but I think proper test should also validate other cases. Please check #28 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants