classes.cpp (1042B)
1 #include <chrono> 2 #include <ctime> 3 #include <format> 4 #include <string> 5 6 auto sec_since_epoch(int64_t sec) 7 { 8 return std::chrono::time_point_cast<std::chrono::seconds>( 9 std::chrono::system_clock::from_time_t(time_t {0}) 10 + std::chrono::seconds(sec)); 11 } 12 13 auto get_time_now() 14 { 15 return std::chrono::current_zone() 16 ->to_local(std::chrono::system_clock::now()) 17 .time_since_epoch() 18 / std::chrono::seconds(1); 19 } 20 21 namespace hemplate::atom { 22 23 std::string format_time(int64_t sec) 24 { 25 static constexpr const char* rfc3339_f = "{:%FT%H:%M:%SZ}"; 26 return std::format(rfc3339_f, sec_since_epoch(sec)); 27 } 28 29 std::string format_time_now() 30 { 31 return format_time(get_time_now()); 32 } 33 34 } // namespace hemplate::atom 35 36 namespace hemplate::rss { 37 38 std::string format_time(int64_t sec) 39 { 40 static constexpr const char* rfc882_f = "{:%a, %d %b %Y %H:%M:%S %z}"; 41 return std::format(rfc882_f, sec_since_epoch(sec)); 42 } 43 44 std::string format_time_now() 45 { 46 return format_time(get_time_now()); 47 } 48 49 } // namespace hemplate::rss