0

As part of the api response header I get time in this format "13 Jun 2023 13:00:28 GMT". as a string

I want to get local system current time and find the difference between current time and the response header time.

How can we do this in CPP? I was thinking to convert "13 Jun 2023 13:00:28 GMT" into local system timezone. Or is there a better way to do this?

RK3
  • 1,221
  • 9
  • 26
  • 37
  • https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c – iggy12345 Jun 13 '23 at 13:10
  • Does this answer your question? [How to get current time and date in C++?](https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c) – iggy12345 Jun 13 '23 at 13:11
  • No. I still don't understand how to convert GMT timezone into local time . – RK3 Jun 13 '23 at 13:14
  • I mean, that's just a subtraction of the two times, then rounding to the nearest hour difference, I don't understand why that would be so confusing? – iggy12345 Jun 13 '23 at 13:15
  • Api response in GMT and local time can be UTC or IST or any other timezone. I simply cannot find the difference – RK3 Jun 13 '23 at 13:17
  • right, it'll be the timezone of where the server is hosted, there is no syntax that tells you specifically which timezone your looking at when you receive a timestamp, so you shouldn't rely on the timestamp in the header, it's bad form – iggy12345 Jun 13 '23 at 13:18
  • instead you should stamp when you send the request and when you receive it back – iggy12345 Jun 13 '23 at 13:19
  • So, do you know a way to convert time across timezones? – RK3 Jun 13 '23 at 13:22
  • I do, see my comment above, about subtracting and rounding to the nearest hour – iggy12345 Jun 13 '23 at 13:32
  • If it's not too much, can you please give a code example? – RK3 Jun 13 '23 at 13:35

1 Answers1

0

Quick and dirty way to get the difference in hours between two different timestamps in C++

#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include <cmath>

using namespace std::chrono;

std::chrono::_V2::system_clock::time_point parse_time_string(const char* s);

const char* header_response = "13 Jun 2023 13:00:28 GMT";
const char* my_time = "13 Jun 2023 9:00:30 EDT";

int main()
{
    auto gmt = parse_time_string(header_response);
    auto local = parse_time_string(my_time);
 
    auto elapsed_seconds = (duration<double>)(gmt-local);
    double elapsed_hours = std::round((elapsed_seconds).count() / 3600.0);
 
    std::cout << "timezone difference is: " << elapsed_seconds.count() << " sec, " << elapsed_hours << " hours" << std::endl;
}

std::chrono::_V2::system_clock::time_point parse_time_string(const char* s) {
    std::tm tm = {};
    strptime(s, "%d %b %Y %T %Z", &tm);
    return std::chrono::system_clock::from_time_t(std::mktime(&tm));
}

Sources:

  1. https://stackoverflow.com/a/32083584/10804423
  2. How to parse a date string into a c++11 std::chrono time_point or similar?
  3. https://cplusplus.com/reference/ctime/strftime/
iggy12345
  • 1,233
  • 12
  • 31