1

'''

int main()
{
struct std::tm tm;
std::istringstream ss("25JUN20");
ss >> std::get_time(&tm, "%e%b%y"); // or just %T in this case
std::time_t time = mktime(&tm);
std::cout << tm.tm_year << std::endl;

}

'''

I tried using this code, but my year gets skewed. Any help would be appreciated.

Thanks

  • `std::cout << tm.tm_year + 1900 << std::endl;` – 273K Sep 14 '21 at 06:29
  • @mediocrevegetable1 I have to convert the string into timeformat, so i can re-generate it to the format I want it to be. – sparsh jain Sep 14 '21 at 06:41
  • @S.M. 60675957 This is the result I get after 1900 to it, i still can't make sense of how its equivalent to 2020 – sparsh jain Sep 14 '21 at 06:42
  • @sparshjain I believe the issue is that it can't parse `JUN` (Though I'm not 100% sure). Can you make it `Jun`? – mediocrevegetable1 Sep 14 '21 at 06:48
  • Have you tried `struct std::tm tm{};`? What is your locale? **b** The month, using the locale's month names. – 273K Sep 14 '21 at 06:51
  • @mediocrevegetable1 Although in my application, i get strings in the presented way, but for example sake i converted it and tm.tm_year +1900 resulted in -93777 Also, the month is wokring fine on printing the month, its just the year – sparsh jain Sep 14 '21 at 06:53
  • @S.M. My locale is en_IN and i didn't get you, what do you mean by trying tm? as i said, the month is working fine. i am getting the result 6 for the current query – sparsh jain Sep 14 '21 at 06:58
  • @mediocrevegetable1 is right. Jun works https://ideone.com/b5Fccl – 273K Sep 14 '21 at 07:00
  • Even a simpler solution to my problem would able be appreciated @S.M. – sparsh jain Sep 14 '21 at 07:05

1 Answers1

0

Even a simpler solution to my problem would able be appreciated @S.M. – sparsh jain

This is very simple using Howard Hinnant's C++20 chrono preview library (open source, header-only).

#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>

int
main()
{
    std::istringstream ss("25JUN20");
    date::year_month_day ymd;
    ss >> date::parse("%d%b%y", ymd);
    std::cout << date::format("%Y%m%d", ymd) << '\n';
}

Output:

20200625

format returns a std::string, so you can do whatever you need to with that result.

I recommend compiling with the configuration macro ONLY_C_LOCALE=1. On gcc and clang this is most easily done with -DONLY_C_LOCALE=1 on the command line. Using VS you can set macros in the IDE.

The reason I recommend this macro is that the gcc and VS std libs usually don't parse month names in a case-insensitive manner, and ONLY_C_LOCALE=1 tells date.h to work around that bug. If you're using LLVM's libc++, this workaround is unnecessary.

This code will port to C++20 with a just a few minor changes:

#include <chrono>
#include <format>
#include <iostream>
#include <sstream>

int
main()
{
    std::istringstream ss("25JUN20");
    std::chrono::year_month_day ymd;
    ss >> std::chrono::parse("%d%b%y", ymd);
    std::cout << std::format("{:%Y%m%d}", ymd) << '\n';
}
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577