Skip to content

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

Closed
@tiran

Description

@tiran

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions