Я использую этот метод GetDateFormat из MF C C ++ , чтобы получить текущий формат даты из системы.
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
SYSTEMTIME st, lt;
TCHAR szTime[256];
GetSystemTime(&st);
GetLocalTime(<);
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, szTime, 250); // prints current date format from system
cout << szTime << endl;
return 0;
}
Сценарий: если я изменю дату вручную в моем system от YYYY-DD-MM
до DD-M-YY
, тогда при повторном запуске программы следует распечатать обновленный формат даты.
С помощью приведенного выше кода я могу добиться этого, но я думаю, что GetDateFormat
только Speci c до windows API. Есть ли какой-либо API для достижения того же в ОС Ma c и Linux?
Обновление:
Подход 2: печатает дату в ожидаемом формате, но не уверен могу ли я использовать это на всех платформах?
/* setlocale example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, time, localtime, strftime */
#include <locale.h> /* struct lconv, setlocale, localeconv */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
struct lconv * lc;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
int twice=0;
setlocale (LC_ALL,"");
strftime (buffer,80,"%x",timeinfo);
printf ("Date is: %s\n",buffer); //prints date in expected format
return 0;
}