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

Fix date_format crash on Windows #279

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions libs/std/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ static value date_new( value s ) {
return alloc_int32(o);
}

#ifdef NEKO_WINDOWS
static char VALID_FORMAT_CODES[] = "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%";
#endif

/**
date_format : #int32 -> fmt:string? -> string
<doc>Format a date using [strftime]. If [fmt] is [null] then default format is used</doc>
Expand All @@ -134,6 +138,37 @@ static value date_format( value o, value fmt ) {
d = val_any_int(o);
if( localtime_r(&d,&t) == NULL )
neko_error();
#ifdef NEKO_WINDOWS
int len = val_strlen(fmt);
const char* str = val_string(fmt);
int i = 0;
while (i < len) {
if (str[i] != '%') {
i++;
continue;
}
i++;
if (str[i] == '#') {
i++;
}
if (str[i] == 'E' || str[i] == 'O') {
i++;
}
bool is_valid = false;
const char* format_code = VALID_FORMAT_CODES;
while (*format_code) {
if (*format_code == str[i]) {
is_valid = true;
break;
}
format_code++;
}
if (!is_valid) {
neko_error();
}
i++;
}
#endif
if( strftime(buf,127,val_string(fmt),&t) == 0 )
neko_error();
return alloc_string(buf);
Expand Down