vendredi 11 décembre 2020

Convert timestamp string to format "%d-%m-%Y"

I have a unix timestamp as std::string and I want to convert it to a beatiful, date string. The already asked questions show only converstions with getting the current time (and use "auto", at all places so I am not sure what class is appropriate) but this is an already existing std::string.

std::string beauty_date; //"%d-%m-%Y"
std::string stamp = "1567555200";
time_t stamp_as_time = (time_t) std::stoi(stamp);

I imagine this needs to be converted first to a unsigned long int (aka time_t). My question is how to put the "stamp_as_time" into beauty_date.

Thanks in advance!

EDIT: From the comments here is what I tried with put_time

std::string beauty_date; //"%d-%m-%Y"
std::string stamp = "1567555200";
time_t stamp_as_time = (time_t) std::stoi(stamp);
beauty_date = std::put_time(std::localtime(&stamp_as_time), "%d-%m-%Y");

This doesn't work as well.

Aucun commentaire:

Enregistrer un commentaire