how to get local hh_mm_ss for current local time?

Hi,

I need a local now point that will give me current time zone values. For instance if i am in Kansas, the local time would be the value of GMT-6. Using system_clock::now() i get UTC time.... see below:

1
2
3
4
5
   std::chrono::local_seconds timestamp() {
   auto now_point = std::chrono::system_clock::now();
   auto r = std::chrono::local_time<std::chrono::seconds>(now_point);
   auto point = std::chrono::time_point_cast<std::chrono::seconds>(now_point);
   return point;


this obviously does not compile but how to get local_time???




The classes in <chrono> have no understanding of human times, they're designed just for timestamps and time deltas. A time_point just represents an absolute point in time, not the reading of a clock set to a particular timezone.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// C++20 
#include <iostream>
#include <chrono>

int main()
{
    // https://en.cppreference.com/w/cpp/chrono/time_zone/to_local
    const auto local_time = std::chrono::current_zone()->to_local( std::chrono::system_clock::now() ) ;
    std::cout << local_time << '\n' ;

    // https://en.cppreference.com/w/cpp/chrono/zoned_time
    const std::chrono::zoned_time zoned_time { std::chrono::current_zone(), std::chrono::system_clock::now() } ;
    std::cout << zoned_time << '\n' ;

    const std::chrono::zoned_time zoned_time_jp { "Asia/Tokyo", std::chrono::system_clock::now() } ;
    std::cout << zoned_time_jp << '\n' ;
}
Topic archived. No new replies allowed.