Skip to content

Wrong year for "month day" input #925

Open

Description

@pgtan
~_ print ${.sh.version}      
Version JM 93u+m/1.1.0-alpha+0f6866b6/MOD 2025-06-22
~_ printf '%(%Y)T\n' "Dec 20"
2026
~_ printf '%(%Y)T\n' "Jan 20"
2026
~_ printf '%(%Y)T\n' "Jan"   
2025
~_ date -d "Dec 20" +%Y
2026
~_ date -d "Dec" +%Y   
2025
~_ whence -a date
date is a shell builtin
date is /usr/bin/date

"Month day" input gives next year, not current. Same error also on AIX:

~> print -f '%(%Y)T\n' "Jan 20"
2026
~> print ${.sh.version}
Version JM 93u+m/1.1.0-alpha 2025-06-22
~> uname
AIX

Bug appears also in Version JM 93u+m/1.0.10 2024-08-01

Workaround is to use "first":

~_ print -f '%(%Y)T\n' "first Dec 20"
2025
~_ date -d "first Dec 20" +%Y        
2025

Activity

  1. jelmd commented on Jan 14, 2026

    @jelmd
    # ksh -c 'printf "%T\n" 2026-01-01'
    Thu ELF  1 21:41:50 MET 2026
    # ksh -c 'printf "%T\n"'
    Wed ELF 14 21:42:36 MET 2026
    # ksh -c 'printf "%T\n" 2026-02-01'
    Sun Feb  1 21:43:25 MET 2026
    
  2. pgtan commented on Jan 14, 2026

    @pgtan
    Author
    # ksh -c 'printf "%T\n" 2026-01-01'
    Thu ELF  1 21:41:50 MET 2026
    # ksh -c 'printf "%T\n"'
    Wed ELF 14 21:42:36 MET 2026
    # ksh -c 'printf "%T\n" 2026-02-01'
    Sun Feb  1 21:43:25 MET 2026
    

    What locale is this? Anyway, I cannot confirm:

    ~_ ksh -c 'printf "%T\n" 2026-01-01'
    Thu Jan  1 00:10:39 CET 2026
    ~_ ksh -c 'LC_TIME=C printf "%T\n" 2026-01-01'
    Thu Jan  1 00:10:50 CET 2026
    ~_ ksh -c 'printf "%T\n"'
    Thu Jan 15 00:11:12 CET 2026
    ~_ ksh -c 'LC_TIME=C printf "%T\n" 2026-02-01'
    Sun Feb  1 00:11:24 CET 2026
    ~_ ksh -c 'LC_TIME=de_DE printf "%T\n" 2026-02-01'
    So 1. Feb 00:12:14 CET 2026
    ~_ ksh -c 'LC_TIME=de_DE printf "%T\n" 2026-01-01'
    Do 1. Jan 00:12:18 CET 2026
    
  3. jelmd commented on Jan 16, 2026

    @jelmd
    # env|grep ^LC
    LC_CTYPE=de_DE.UTF-8
    LC_COLLATE=C
    LC_TIME=C
    LC_MESSAGES=C
    
    # ksh -c 'LC_TIME=de_DE printf "%T\n" 2026-01-01'
     ELF  1 13:16:07 UTC 2026
    
  4. jelmd commented on Jan 16, 2026

    @jelmd
    $ print ${.sh.version}                                
    Version AJM 93u+m/1.0.8 2024-01-01
    
  5. JohnoKing commented on Feb 11, 2026

    @JohnoKing

    This seems to have been introduced in 1a9d8ad by setting the default factor to one (ping @phidebian):

    f = 1; /* Default factor */
    if (state & LAST)
    f = -f; /* Invert on 'last'/'ago', etc...*/

  6. phidebian commented on Feb 11, 2026

    @phidebian

    I can't look at the moment, surgery recovery, you may error/try other factor, I don't remember why I used 1, if this is really me introduced it. I remember this code was hard to follow and may be I overlooked some corner cases...

  7. jelmd commented on Feb 12, 2026

    @jelmd

    Not sure. Might be even older: Checked an old Solaris machine with a Sun/Oracle patched Version AJM 93u+ 2012-08-01 ksh on it - same behavior, i.e. YYYY-01-DD => %{b,h,Ec} == *ELF*, but %B seems to be ok. Unfortunately have no time to make a deep dive, too. :(

  8. McDutchie commented on Apr 2, 2026

    @McDutchie

    This seems to have been introduced in 1a9d8ad by setting the default factor to one (ping @phidebian):

    f = 1; /* Default factor */
    if (state & LAST)
    f = -f; /* Invert on 'last'/'ago', etc...*/

    I've confirmed that this is the commit that introduces the bug. It's unfortunate, because that commit fixes a load of other bugs, so we can't just revert it.

    I've tried changing the default value of f to 0 and that seems to fix the issue, but causes dozens of regression test failures in printf.sh, so it's clearly not correct.

  9. phidebian commented on Apr 2, 2026

    @phidebian
  10. JohnoKing commented on Apr 2, 2026

    @JohnoKing

    Here's my attempt at fixing it (uncertain of correctness):

    diff --git a/src/lib/libast/tm/tmxdate.c b/src/lib/libast/tm/tmxdate.c
    index 49ef24ef4..153e35cb1 100644
    --- a/src/lib/libast/tm/tmxdate.c
    +++ b/src/lib/libast/tm/tmxdate.c
    @@ -1553,7 +1553,7 @@ tmxdate(const char* s, char** e, Time_t now)
     								goto done;
     							state |= DAY|MDAY;
     							tm->tm_mday = (int)n;
    -							if (f > 0)
    +							if (f > 1)
     								tm->tm_year += f;
     						}
     						if (state & (LAST|NEXT|THIS))
  11. McDutchie commented on Apr 2, 2026

    @McDutchie

    Ha, I had just arrived at the exact same point in the code by tagging everything that handles tm_year with debug sfprintfs. :)

    Yes, lines 1556-1557 is where this bug happens.

    My patch was to simply remove that:

    diff --git a/src/lib/libast/tm/tmxdate.c b/src/lib/libast/tm/tmxdate.c
    index 49ef24ef4..b37499c5c 100644
    --- a/src/lib/libast/tm/tmxdate.c
    +++ b/src/lib/libast/tm/tmxdate.c
    @@ -1553,8 +1553,6 @@ tmxdate(const char* s, char** e, Time_t now)
     								goto done;
     							state |= DAY|MDAY;
     							tm->tm_mday = (int)n;
    -							if (f > 0)
    -								tm->tm_year += f;
     						}
     						if (state & (LAST|NEXT|THIS))
     						{

    No regression test failures happen after that (neither in printf.sh nor elsewhere). But I'm also not sure of the correctness.

  12. McDutchie commented on Apr 2, 2026

    @McDutchie

    I'll note that those two lines weren't added by the commit in question; they're original. However, they may have been somehow rendered incorrect by that commit. I'm going to have to spend some time trying to actually understand all this code, but it's very difficult.

  13. McDutchie commented on Jul 25, 2026

    @McDutchie

    Ping @pghvlaans: Would you happen to have time to take a look at this tm(3) issue as well? Reproducer at the top (just add a year). There are a couple of patch ideas above but I'm not confident about them.

  14. pghvlaans commented on Jul 25, 2026

    @pghvlaans

    I'll see what I can do.

  15. pghvlaans commented on Jul 25, 2026

    @pghvlaans

    I can confirm that neither patch is correct, unfortunately. To reproduce, try printf "%(%Y)T" "third Dec 20". The output should be 2028.

  16. McDutchie commented on Jul 25, 2026

    @McDutchie

    The fact that using "first" is a workaround may provide a clue to a fix -- if you can find where/how that is processed.

  17. McDutchie commented on Jul 25, 2026

    @McDutchie

    However... it seems like second, third, etc., are also broken.

    $ printf "%(%Y)T\n" "first Dec 20"
    2026
    $ printf "%(%Y)T\n" "second Dec 20"
    2028
    $ printf "%(%Y)T\n" "third Dec 20" 
    2029
    
  18. pghvlaans commented on Jul 25, 2026

    @pghvlaans

    Well, this does appear to work:

    index 56b1e413..d9cc7a72 100644
    --- a/src/lib/libast/tm/tmxdate.c
    +++ b/src/lib/libast/tm/tmxdate.c
    @@ -1563,7 +1563,7 @@ tmxdate(const char* s, char** e, Time_t now)
                                                            state |= DAY|MDAY;
                                                            tm->tm_mday = (int)n;
                                                            if (f > 0)
    -                                                               tm->tm_year += f;
    +                                                               tm->tm_year += (f - 1);
                                                    }
                                                    if (state & (LAST|NEXT|THIS))
                                                    {

    I'm a bit suspicious, though, since f is apparently 1 without an ordinal and 0 with first. It could very well be something far sillier (and messier).

    EDIT: It does not work. next Jul 23 and next Jun 23 shouldn't have different output, and with this (and the current code), they do.

  19. pghvlaans commented on Jul 25, 2026

    @pghvlaans

    Something along these lines would be better:

    diff --git a/src/lib/libast/tm/tmxdate.c b/src/lib/libast/tm/tmxdate.c
    index 56b1e413..cdb535ca 100644
    --- a/src/lib/libast/tm/tmxdate.c
    +++ b/src/lib/libast/tm/tmxdate.c
    @@ -1562,7 +1562,7 @@ tmxdate(const char* s, char** e, Time_t now)
                                                                    goto done;
                                                            state |= DAY|MDAY;
                                                            tm->tm_mday = (int)n;
    -                                                       if (f > 0)
    +                                                       if (f > 0 && state & (LAST|NEXT|THIS))
                                                                    tm->tm_year += f;
                                                    }
                                                    if (state & (LAST|NEXT|THIS))
    @@ -1670,7 +1670,7 @@ tmxdate(const char* s, char** e, Time_t now)
                                            if (state & LAST)
                                                    tm->tm_year -= (tm->tm_mon < n) ? 0 : 1;
                                            else
    -                                               tm->tm_year += ((state & NEXT) ? 1 : 0) + ((tm->tm_mon < n) ? 1 : 0);
    +                                               tm->tm_year += (tm->tm_mon < n) ? 1 : 0;
                                            if (state & MDAY)
                                                    goto clear_hour;
                                            set &= ~(LAST|NEXT|THIS); /*AHA*/

    Basically, that (f > 0) condition expects a baseline value of f = 0. This would make plain "Month Day" requests have f of 0. The baseline can't be changed without lots of other breakage, though. There might be something else bumping the year when it shouldn't, but I haven't managed to track it down just yet.

    The "Jun 23" and "Jul 23" thing I mentioned earlier is actually just down to a lack of resolution in lines 1671 and 1673, so it isn't entirely related.

    I'll take another look tomorrow.

  20. pghvlaans commented on Jul 26, 2026

    @pghvlaans

    Also found this, which was causing second to not be recognized as an ordinal with the abbreviation jan:

    diff --git a/src/lib/libast/tm/tmxdate.c b/src/lib/libast/tm/tmxdate.c
    index cdb535ca..77c214cc 100644
    --- a/src/lib/libast/tm/tmxdate.c
    +++ b/src/lib/libast/tm/tmxdate.c
    @@ -1325,7 +1325,7 @@ tmxdate(const char* s, char** e, Time_t now)
                                                            k = tmlex(u, &u,
                                                                    tm_info.format, TM_NFORM,
                                                                    tm_info.format + TM_SUFFIXES, TM_PARTS - TM_SUFFIXES);
    -                                                       if (k > 0)
    +                                                       if (k >= 0)
                                                            {
                                                                    n = 2;
                                                                    goto ordinal;
  21. pghvlaans commented on Jul 26, 2026

    @pghvlaans

    I've made a fair amount of progress on my tmxdate branch. The following problems have been solved:

    • The wrong "Month Day" year
    • e.g. third dec failing to work
    • e.g. third dec 10 being a year ahead
    • next does not correspond to GNU date for days of the week; it's also too aggressive for months, though date doesn't handle that.
    • second jan (day) failing to recognize second as ordinal
    • e.g. 2020-2-3 next fri is off by weeks

    There are other pending issues I intend to get to before making a PR (though more may come to light):

    • e.g. 2020-2-3 next fri is off by weeks
    • There is insufficient resolution to recognize whether a day has passed in the current month. second jul 10 and second jul 30 should ideally have different years when called on July 26th, and they don't.
  22. McDutchie commented on Sep 1, 2026

    @McDutchie

    Any luck on the pending issues, @pghvlaans? Let me know if I can be of any help.

  23. pghvlaans commented on Sep 1, 2026

    @pghvlaans

    Hey, sorry, I've been busy, but I'll have a chance to have another good look this week. I'll let you know if anything comes up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is not working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions