I can’t get libc.strftime to respect the locale, any ideas what’s going on?
My computer locale is set to fr_FR.UTF-8.
This C program works correctly:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
int
main(int argc, char *argv[])
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
setlocale(LC_ALL, "");
if (strftime(outstr, sizeof(outstr), argv[1], tmp) == 0) {
fprintf(stderr, "strftime returned 0");
exit(EXIT_FAILURE);
}
printf("Result string is \"%s\"\n", outstr);
exit(EXIT_SUCCESS);
}
When I compile and run it, get the expected results:
$ gcc -o strftime strftime.c -lc
$ ./strftime %B
Result string is "août"
$ ./strftime %a
Result string is "jeu."
Here is the Odin version that I came up with:
package my_program
import "core:fmt"
import "core:time"
import "core:os"
import "core:strings"
import "core:c/libc"
main :: proc () {
libc.setlocale(libc.Locale_Category.ALL, nil)
unix_time := libc.time_t(time.to_unix_seconds(time.now()))
buf: [200]u8
len := libc.strftime(
raw_data(&buf),
size_of(buf),
strings.unsafe_string_to_cstring(os.args[1]), // Format string from command line argument
libc.localtime(&unix_time),
)
date := string(buf[:len])
fmt.println("Result string is:", date)
}
It completely disregards the locale, always sending answers in English:
$ odin run odin_strftime.odin -file -- %B
Result string is: August
$ odin run odin_strftime.odin -file -- %a
Result string is: Thu
I’m running this on Ubuntu 24.04, here is the output of locale:
$ locale
LANG=fr_FR.UTF-8
LANGUAGE=fr_FR.UTF-8
LC_CTYPE="fr_FR.UTF-8"
LC_NUMERIC="fr_FR.UTF-8"
LC_TIME="fr_FR.UTF-8"
LC_COLLATE="fr_FR.UTF-8"
LC_MONETARY="fr_FR.UTF-8"
LC_MESSAGES="fr_FR.UTF-8"
LC_PAPER="fr_FR.UTF-8"
LC_NAME="fr_FR.UTF-8"
LC_ADDRESS="fr_FR.UTF-8"
LC_TELEPHONE="fr_FR.UTF-8"
LC_MEASUREMENT="fr_FR.UTF-8"
LC_IDENTIFICATION="fr_FR.UTF-8"
LC_ALL=fr_FR.UTF-8
Any ideas what might be happening here? What am I missing?