std::strftime

Header: <ctime>

Converts the date and time information from a given calendar time tp to a null-terminated multibyte character string str according to format string format. Up to count bytes are written.

# Declarations

std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* tp );

# Parameters

# Return value

The number of bytes written into the character array pointed to by str not including the terminating ‘\0’ on success. If count was reached before the entire string could be stored, 0 is returned and the contents are indeterminate.

# Example

#include <ctime>
#include <iostream>
#include <iterator>
#include <locale>
 
void utcExample()
{
    // Example of the very popular RFC 3339 format UTC time
    std::time_t time = std::time({});
    char timeString[std::size("yyyy-mm-ddThh:mm:ssZ")];
    std::strftime(std::data(timeString), std::size(timeString),
                  "%FT%TZ", std::gmtime(&time));
    std::cout << timeString << '\n';
}
 
int main()
{
    std::time_t t = std::time(nullptr);
    char mbstr[100];
 
    if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&t)))
        std::cout << mbstr << '\n';
 
    std::locale::global(std::locale("ja_JP.utf8"));
    if (std::strftime(mbstr, sizeof(mbstr), "%A %c", std::localtime(&t)))
        std::cout << mbstr << '\n';
 
    utcExample();
}

# See also