0

I have a time_t value from which I need to extract the values for year, month, dayOfMonth, hourOfDay, minute & second (as integers). If possible I'd like to minimize my reliance on libraries, but if there is a library function you can be reasonably certain will always be available then that'd be fine. The time_t value should already have been adjusted for timezone, so no worry about that.

Due to working in a rather large company project and using a remote server to build the application, I actually don't know which version of C++ we are using but it's most likely C++11 (possibly C++98 but I doubt it, and almost certainly not C++17).

Anju Maaka
  • 263
  • 1
  • 10
  • If on linux, you could use [`localtime`](https://linux.die.net/man/3/localtime) (or `gmtime`). If you can live without the `time_t` variable, have a look at [here](https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c). – Aconcagua Oct 29 '19 at 10:14
  • @yaodav Not really a duplicate. *There* they just want to get time by *any* means, *here* we've introduded a `time_t` already. Could be an alternative, though (so at least related), but only if we *can* drop the `time_t`. – Aconcagua Oct 29 '19 at 10:20
  • @yaodav That answer did not cover my question. I do not want to GET the time. I HAVE the time and I want to EXTRACT the individual fields. I do not want to just write out a String with a readable date/time format; I want to get different integers for each field value (year, month, day, hour, minute, second). Essentially; I could get the 'second' field value by using modulo60 on the time_t value, but that doesn't help me with handling leap-years and such – Anju Maaka Oct 29 '19 at 10:53
  • to get the C++ version just check the [`__cplusplus` macro](https://stackoverflow.com/q/2324658/995714) – phuclv Nov 28 '20 at 00:32

1 Answers1

1

If you have a time_t (which is from <time.h>), you don't need any additional library. You can just use the tm struct and localtime function that are also in <time.h>:

#include <iostream>
#include <time.h>
int main() {
    time_t t;
    t = time(NULL); // or some other value
    tm *timeNow = localtime(&t);
    std::cout << "Year: " << timeNow->tm_year+1900 << std::endl;
    std::cout << "Month: " << timeNow->tm_mon << std::endl;
    std::cout << "Day: " << timeNow->tm_mday << std::endl;
    std::cout << "Hour: " << timeNow->tm_hour << std::endl;
    std::cout << "Minute: " << timeNow->tm_min << std::endl;
    std::cout << "Second: " << timeNow->tm_sec << std::endl;
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • This looks promising. The 'year' integer value you get has to have 1900 added to it in order to actually match the year? Is that because negative values in time_t goes back before 1970? – Anju Maaka Oct 29 '19 at 10:55
  • Well, it's simply because according to [the documentation](https://en.cppreference.com/w/c/chrono/tm), `tm_year` is "years since 1900". It's not specified what exactly `time_t` has to be other than "Real type capable of representing times" so I suppose it's possible that it can have a negative value if the implementation allows it. – Blaze Oct 29 '19 at 11:02
  • As far as I understand it, it's a serious problem in a lot of systems that once we hit some time in 2036 the number of seconds since 1970 will exceed what can be stored in a signed integer, and it will loop back to beginning of 1900s due to integer overflow. Apparently, negative numbers do count backwards from 1970 due to the need for some systems to work with dates before that. – Anju Maaka Oct 29 '19 at 13:28