[Solved] Convert timestamp to localized date string

I feel like this should be fairly easy, but i can’t seem to get it right. How can I convert a timestamp to a localized date string?

E.g. I want to convert ‘1715835661’ to a readable date like 18/05/2024, and formatted as the current locale B_SHORT_DATE_FORMAT?

Ahoy @johan ,

Assuming you use
bash
and
coreutils
installed …

~> pkgman search coreutils
Status  Name                 Description                               
-----------------------------------------------------------------------
S       coreutils            Standard GNU core utilities               
        coreutils_debuginfo  Standard GNU core utilities (debug info)  
        coreutils_source     Standard GNU core utilities (source files)
~> 
~> 
~> date -d @715835661
H szept.  7 05:14:21 CEST 1992
~> 
~> 
~> date -d @$(date -u +%s)
Szo máj. 18 20:38:15 CEST 2024
~> 
~> 
~> date -u
Szo máj. 18 18:38:48 UTC0 2024
~> 
~> 
~> date -u +%s
1716057543
~> 
~> 
~> date -d @715835661 +%d'/'%m'/'%Y
07/09/1992
~> 
~> 
~> date -d @1716057543 +%d'/'%m'/'%Y
18/05/2024
~> 

Basically I do not know exaactly what format used at
B_SHORT_DATE_FORMAT
as I am not a developer, just kinda power user,

…so you must play with date command formatting possibilities
as you can see I did with … formatting as you requested in your example.
I personally better like dash – ‘-’ – as a separator :j

Thanks @KitsunePrefecture , my question wasn’t very clear, but I want to do this programatically (C++), using the available BDateFormat class (or similar).

1 Like

OK, I figured it out after looking at examples in the HaikuArchives Github repo:

BString dateString;
	time_t timestamp = 1715835661;
	BDateFormat dateFormatter;
	if (dateFormatter.Format(dateString, timestamp, B_SHORT_DATE_FORMAT) == B_OK)
		printf("Date: %s\n", dateString.String());

This prints out Date: 16.05.2024 which matches my locale.

6 Likes