Scrontch
Scrontch

Reputation: 3424

How to get the short date format string from the current locale in C/C++?

The following code (Windows, C++) sets the current locale and retrieves a formatted short date string, formatted according to locale's short date format "%c".

  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"%c",timeinfo);

Say this gives "31/01/2012" for a given date and locale. This corresponds to a date format of "%d/%m/%Y", although "%c" has been specified.

Is there a way i can get the format string itself, i.e. "%d/%m/%Y" for a given locale?

Upvotes: 4

Views: 1109

Answers (1)

infixed
infixed

Reputation: 1155

There is a call in langinfo to do this

as a simple demo

#include <stdio.h>
#include <langinfo.h>

int main()
{
    printf("%s\n", nl_langinfo( D_FMT ) );
}

If your C compiler is not POSIX compliant, and I thought Windows was supposed to be, then perhaps the answers at this question would be more of a guide

What is the correct way to get a LOCALE_SSHORTDATE that is guaranteed to have a full (4-digit) year number?

Upvotes: 2

Related Questions