1

I am using this GetDateFormat method from MFC C++ to get the current date format from the system.

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    SYSTEMTIME st, lt;
    TCHAR szTime[256];

    GetSystemTime(&st);
    GetLocalTime(&lt);
    GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, szTime, 250); // prints current date format from system

    cout << szTime << endl;
    return 0;
}

Scenario: If I change the date manually in my system from YYYY-DD-MM to DD-M-YY, then it should be print the updated date format if I run the program again.

With the above code I am able to achieve it but I think GetDateFormat is only specific to windows API. Is there any API to achieve the same in Mac OS and Linux?

Update:

Approach 2: Prints date in expected format but not sure if I can use this in all platforms?

/* 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;
}
kittu
  • 6,662
  • 21
  • 91
  • 185
  • Does this answer your question? [How do I get the current "localized pattern" for the date and time of an std::locale](https://stackoverflow.com/questions/34750954/how-do-i-get-the-current-localized-pattern-for-the-date-and-time-of-an-stdlo) – ChrisMM Apr 24 '20 at 13:55
  • @ChrisMM I have already tried it but it doesn't output the expected date format from system – kittu Apr 24 '20 at 14:08
  • Please confirm you tried this with just "%x" in the format string. – Spencer Apr 24 '20 at 14:53
  • @Spencer No. Where do I add "%x"? I am new to c++ – kittu Apr 24 '20 at 14:56
  • @Spencer You meant this line: `tmput.put(s, s, ' ', my_time, '%x');` ? – kittu Apr 24 '20 at 14:58
  • Using `tmput.put(s, s, ' ', my_time, '%x');` gives: `user settings: 04/24/20 C settings: 04/24/20` But the date format in system is `DD.M.YY` – kittu Apr 24 '20 at 14:59
  • How did you set the system date format? through a shell command? A config file? – Spencer Apr 24 '20 at 16:14
  • Did you set the language/locale and use the default format from that? – Spencer Apr 24 '20 at 16:24
  • @Spencer I didn't set any local/language. I just went and updated the date in my system from short date to long date and ran the program. It works – kittu Apr 24 '20 at 16:30
  • @Spencer But not sure if this code(approach) can be compiled and run in different platforms? – kittu Apr 24 '20 at 16:31
  • @kittu By "It works" do you mean the `date` command in a Linux shell returns the date in the right format? – Spencer Apr 24 '20 at 16:33
  • @Spencer I tested this code on windows for now. Its C++ code right and C++ is cross platform so I guess I should be able to compile this code and run on Linux etc? – kittu Apr 24 '20 at 16:39
  • Why anybody isn't answering about my approach 2 whether its work on all platforms? – kittu Apr 24 '20 at 17:24

1 Answers1

2
#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);

    // If you want to set a specific local then use the appropriate local object.
    std::locale lJP("ja_JP");
    std::cout.imbue(lJP);
    std::cout << std::put_time(&tm, "%Ec") << "\n";

    // To pull the system local used by your system then use the empty string.
    std::locale lSY("");
    std::cout.imbue(lSY);
    std::cout << std::put_time(&tm, "%x") << "\n";
}

Running this:

> ./a.out
金  4/24 09:42:27 2020
Fri Apr 24 09:42:27 2020

You can find the standard valid conversions here:

kittu
  • 6,662
  • 21
  • 91
  • 185
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • terminate called after throwing an instance of 'std::runtime_error' what(): locale::facet::_S_create_c_locale name not valid – kittu Apr 24 '20 at 16:57
  • what about approach 2 I have tried? Will it work on any standard complaint compiler ? – kittu Apr 24 '20 at 16:58
  • @kittu Just leave out the four lines where the Japanese locale is set and instead use the "to pull the system local" part where `locale("")` is set. You tagged this question [tag:c++] so Martin is giving you a C++ approach rather than a C approach. – Spencer Apr 24 '20 at 16:59
  • @Spencer I have tagged C also but some one removed it – kittu Apr 24 '20 at 17:01
  • @Martin I edited your post to reduce it to just the stuff OP wants. Feel free to revert it. – Spencer Apr 24 '20 at 17:10
  • @Martin Removed 4 lines and ran. It prints empty line – kittu Apr 24 '20 at 17:16
  • @kittu If it throws then you don't have the specific local installed on your machine. In this example I specifically use `ja_JP` as an example as I happen to have this locale. If you don't then it will crash. But the locale "" will pull the current system locale which will never crash. – Martin York Apr 24 '20 at 18:35
  • @Martin That is fine but I'm not able to get it to print the system locale. – kittu Apr 24 '20 at 23:11
  • This doesn't work as expected. This just prints the locale date instead of current system locale date that is set in the computer as per user desired date format – kittu Apr 26 '20 at 16:53