4

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?

Scrontch
  • 3,275
  • 5
  • 30
  • 45

1 Answers1

2

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?

infixed
  • 1,155
  • 7
  • 15
  • 3
    That is an external library. No standard C or C++ solution? Or a WinApi call? – Scrontch Feb 04 '19 at 15:30
  • POSIX is pretty standard. I thought Windows was supposed to be POSIX complient. See link added to answer for native windows question along your your area of interest – infixed Feb 04 '19 at 15:56