Skip to content

week number of strftime("%U") and %W wrong #16156

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

Closed
tiran opened this issue Jan 30, 2022 · 1 comment
Closed

week number of strftime("%U") and %W wrong #16156

tiran opened this issue Jan 30, 2022 · 1 comment

Comments

@tiran
Copy link
Contributor

tiran commented Jan 30, 2022

Emscripten's strftime() has an off-by-one error for week number with Sunday as first day of a week.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.1 (1934a98e709b57d3592b8272d3f1264a72c089e4)
clang version 14.0.0 (https://github.com/llvm/llvm-project f142c45f1e494f8dbdcc1bcf14122d128ac8f3fe)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin

reproducer

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    char buf[256];
    char *fmt1 = "%Y-%m-%d %H:%M:%S";
    char *fmt2 = "%Y %U %w (%a)";
    time_t t = 1358035200;
    struct tm *tm;

    tm = gmtime(&t);

    strftime(buf, sizeof(buf), fmt1, tm);
    fprintf(stdout, "%s -> %s\n", fmt1, buf);
    strftime(buf, sizeof(buf), fmt2, tm);
    fprintf(stdout, "%s -> %s\n", fmt2, buf);
    return 0;
}

output

$ gcc -o wdbug wdbug.c && ./wdbug 
%Y-%m-%d %H:%M:%S -> 2013-01-13 00:00:00
%Y %U %w (%a) -> 2013 02 0 (Sun)
$ emcc -o wdbug.js wdbug.c && node wdbug.js
%Y-%m-%d %H:%M:%S -> 2013-01-13 00:00:00
%Y %U %w (%a) -> 2013 01 0 (Sun)

python output

# Linux
>>> for day in range(1, 15):
...     print(datetime.datetime(2013, 1, day).strftime("%Y %U %w (%a)"))
... 
2013 00 2 (Tue)
2013 00 3 (Wed)
2013 00 4 (Thu)
2013 00 5 (Fri)
2013 00 6 (Sat)
2013 01 0 (Sun)
2013 01 1 (Mon)
2013 01 2 (Tue)
2013 01 3 (Wed)
2013 01 4 (Thu)
2013 01 5 (Fri)
2013 01 6 (Sat)
2013 02 0 (Sun)
2013 02 1 (Mon)
# wasm32-emscripten
>>> for day in range(1, 15):
...     print(datetime.datetime(2013, 1, day).strftime("%Y %U %w (%a)"))
... 
2013 00 2 (Tue)
2013 00 3 (Wed)
2013 00 4 (Thu)
2013 00 5 (Fri)
2013 00 6 (Sat)
2013 00 0 (Sun)
2013 01 1 (Mon)
2013 01 2 (Tue)
2013 01 3 (Wed)
2013 01 4 (Thu)
2013 01 5 (Fri)
2013 01 6 (Sat)
2013 01 0 (Sun)
2013 02 1 (Mon)

@tiran tiran changed the title strftime("%U") week number off-by-one error week number of strftime("%U") and %W wrong Feb 5, 2022
@tiran
Copy link
Contributor Author

tiran commented Feb 5, 2022

The weeknumber for %W is wrong, too.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    char buf[256];
    char *fmt1 = "%Y-%m-%d - %a %w - U:%U W:%W";
    time_t start = 1357041600;
    struct tm *tm;

    for (int i = 0; i < 31; i++) {
        time_t t = start + (i * 60 * 60 * 24);
        struct tm *tm = gmtime(&t);
        strftime(buf, sizeof(buf), fmt1, tm);
        fprintf(stdout, "%s\n", buf);
        if (tm->tm_wday == 0) {
            fprintf(stdout, "---\n");
        }
    }
    return 0;
}
$ emcc -o wdbug.js wdbug.c && node wdbug.js 
2013-01-01 - Tue 2 - U:00 W:00
2013-01-02 - Wed 3 - U:00 W:00
2013-01-03 - Thu 4 - U:00 W:01
2013-01-04 - Fri 5 - U:00 W:01
2013-01-05 - Sat 6 - U:00 W:01
2013-01-06 - Sun 0 - U:00 W:01
---
2013-01-07 - Mon 1 - U:01 W:01
2013-01-08 - Tue 2 - U:01 W:01
2013-01-09 - Wed 3 - U:01 W:01
2013-01-10 - Thu 4 - U:01 W:02
2013-01-11 - Fri 5 - U:01 W:02
2013-01-12 - Sat 6 - U:01 W:02
2013-01-13 - Sun 0 - U:01 W:02
---
2013-01-14 - Mon 1 - U:02 W:02
2013-01-15 - Tue 2 - U:02 W:02
2013-01-16 - Wed 3 - U:02 W:02
2013-01-17 - Thu 4 - U:02 W:03
2013-01-18 - Fri 5 - U:02 W:03
2013-01-19 - Sat 6 - U:02 W:03
2013-01-20 - Sun 0 - U:02 W:03
---
2013-01-21 - Mon 1 - U:03 W:03
2013-01-22 - Tue 2 - U:03 W:03
2013-01-23 - Wed 3 - U:03 W:03
2013-01-24 - Thu 4 - U:03 W:04
2013-01-25 - Fri 5 - U:03 W:04
2013-01-26 - Sat 6 - U:03 W:04
2013-01-27 - Sun 0 - U:03 W:04
---
2013-01-28 - Mon 1 - U:04 W:04
2013-01-29 - Tue 2 - U:04 W:04
2013-01-30 - Wed 3 - U:04 W:04
2013-01-31 - Thu 4 - U:04 W:05
$ gcc -o wdbug wdbug.c && ./wdbug
2013-01-01 - Tue 2 - U:00 W:00
2013-01-02 - Wed 3 - U:00 W:00
2013-01-03 - Thu 4 - U:00 W:00
2013-01-04 - Fri 5 - U:00 W:00
2013-01-05 - Sat 6 - U:00 W:00
2013-01-06 - Sun 0 - U:01 W:00
---
2013-01-07 - Mon 1 - U:01 W:01
2013-01-08 - Tue 2 - U:01 W:01
2013-01-09 - Wed 3 - U:01 W:01
2013-01-10 - Thu 4 - U:01 W:01
2013-01-11 - Fri 5 - U:01 W:01
2013-01-12 - Sat 6 - U:01 W:01
2013-01-13 - Sun 0 - U:02 W:01
---
2013-01-14 - Mon 1 - U:02 W:02
2013-01-15 - Tue 2 - U:02 W:02
2013-01-16 - Wed 3 - U:02 W:02
2013-01-17 - Thu 4 - U:02 W:02
2013-01-18 - Fri 5 - U:02 W:02
2013-01-19 - Sat 6 - U:02 W:02
2013-01-20 - Sun 0 - U:03 W:02
---
2013-01-21 - Mon 1 - U:03 W:03
2013-01-22 - Tue 2 - U:03 W:03
2013-01-23 - Wed 3 - U:03 W:03
2013-01-24 - Thu 4 - U:03 W:03
2013-01-25 - Fri 5 - U:03 W:03
2013-01-26 - Sat 6 - U:03 W:03
2013-01-27 - Sun 0 - U:04 W:03
---
2013-01-28 - Mon 1 - U:04 W:04
2013-01-29 - Tue 2 - U:04 W:04
2013-01-30 - Wed 3 - U:04 W:04
2013-01-31 - Thu 4 - U:04 W:04

tiran added a commit to tiran/emscripten that referenced this issue Mar 13, 2022
Derive the week number %U (first Sunday as the first day of week 01) and
%W (first Monday as the first day of week 01) from tm_yday and tm_wday
fields. The new implementation uses the same algorithm as musl libc and
glibc.

See: emscripten-core#16156
@sbc100 sbc100 closed this as completed in 2b24024 Mar 13, 2022
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

1 participant