1

This is what I am trying to do:

  1. Get the local time (from the system);
  2. Convert that time to the UTC format and associate it with some member variable of current object.
  3. Later on, given the timezone of the user, I wish to convert it into the correct local time and display it to the user.

Looking up few things on SO and CppReference, I could come up with the following snippet:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>

using namespace std;

int main()
{
    time_t lt = std::time(0);
    //time(0) gives current time, but LTime shows the UTC time (not local time)
    string LTime = std::ctime(&lt); //localtime() gives error
    cout<<LTime;

    //timestamp = mktime(&tm) - timezone;
    //time_t timestamp = mktime(&tm) - _timezone;
    //std::cout << "timestamp: " << std::put_time(timestamp, "%c %Z") << '\n';

    return 0;
}
  1. The example on cppreference.com illustrates how the value can be printed using put_time(); but how to store it in a variable?
  2. How to convert the UTC time format to current timezone (given some timezone as the input)? I tried using the commented code above as per this link but it does not take any parameter.
  • consider C++'s chrono library. – Charles Sep 15 '17 at 01:31
  • @Charles, can we work with timezones using that? How do we go about changing the timezones in it? –  Sep 15 '17 at 01:34
  • Here's something called [std::localtime](http://en.cppreference.com/w/cpp/chrono/c/localtime) – Charles Sep 15 '17 at 01:36
  • And there's this answer: https://stackoverflow.com/questions/25170287/c-timegm-conversion-dst-to-a-certain-timezone-at-a-given-time-in-the-future – Charles Sep 15 '17 at 01:38
  • @Charles, yes. If you notice, my code is very similar to that. However, they are using `put_time()` to display the time; I wanna store the time in some format and later convert this into a date format again (taking into account the time zone). –  Sep 15 '17 at 01:38
  • Why _store_ as a string? – Charles Sep 15 '17 at 01:39
  • @Charles, sorry, any format would work. I edited my earlier comment. Apologies. –  Sep 15 '17 at 01:39
  • If you're interested, here is a library that will do this without setting a global like `TZ`: https://howardhinnant.github.io/date/tz.html – Howard Hinnant Sep 15 '17 at 13:30

1 Answers1

1

You can use local time get the local time and gmt time for UTC You can set the Time zone using the list Time zone wiki

#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
    std::time_t result = std::time(nullptr);
    auto local = std::asctime(std::localtime(&result));
    std::cout <<local;
    std::cout << "UTC:   " << std::put_time(std::gmtime(&result), "%c %Z") << '\n';
    putenv("TZ=Asia/Singapore");
    local = std::asctime(std::localtime(&result));
    std::cout <<"Asia/Singapore Time "<<local;

}

Output

Thu Sep 14 21:59:37 2017
UTC:   Fri Sep 15 01:59:37 2017 UTC
Asia/Singapore Time Fri Sep 15 09:59:37 2017
Program ended with exit code: 0
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
  • Thank you. And then how to convert the UTC time to local time, dynamically (like say, with the timezone passed as one of the parameters)? –  Sep 15 '17 at 01:41
  • I think OP wants more control over timezones – Charles Sep 15 '17 at 01:41
  • @Charles, yes, using something like `TimeZone timeZone = TimeZone.getTimeZone("Europe/Copenhagen");` in Java to convert to target timezone. –  Sep 15 '17 at 01:44
  • Are you ok to use any library like tz? – Hariom Singh Sep 15 '17 at 01:46
  • @HariomSingh, yes, I can use that. –  Sep 15 '17 at 01:58
  • @UmedhSinghBundela I was just confused with moment.js .I used long ago for timezone ..But finally some wiki helped to get the answer – Hariom Singh Sep 15 '17 at 02:01
  • @HariomSingh, thank you, that works like a charm. One qq though - when I run the code on Ideone.com, the local time printed is the same as the UTC time. Do you think it is so because the ideone server is in that time zone? –  Sep 15 '17 at 02:16