Skip to content

strftime() mishandles quoted percent, e.g. %%z #16155

Closed
@tiran

Description

@tiran

Emscripten's strftime does not parse quoted percent correctly. It treats %%z as % + %z instead of %% + z. For %%z the function returns %+0000 instead of %z. I detected the problem in CPython port to Emscripten.

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 *fmt = "%H:%M:%S %%Z=%Z %%z=%z";
    time_t t;
    struct tm *tm;

    t = time(NULL);
    tm = localtime(&t);

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

glibc output

# gcc -o fmttime fmttime.c && ./fmttime  
%H:%M:%S %%Z=%Z %%z=%z -> 15:33:56 %Z=UTC %z=+0000

emcc + node output

# emcc -o fmttime.js fmttime.c && node fmttime.js 
%H:%M:%S %%Z=%Z %%z=%z -> 15:34:28 %Coordinated Universal Time=Coordinated Universal Time %+0000=+0000

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