Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_zone_struct.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
5
8
9#include "types.hpp"
10#include "constants.hpp"
11
12#include <string>
13
14namespace time_shield {
15
22 int hour;
23 int min;
25 };
26
34 int hour,
35 int min,
36 bool is_positive = true) {
37 return TimeZoneStruct{hour, min, is_positive};
38 }
39
40//------------------------------------------------------------------------------
41
47 int abs_val = std::abs(offset);
48 int hour = abs_val / SEC_PER_HOUR;
49 int min = (abs_val % SEC_PER_HOUR) / SEC_PER_MIN;
50 bool is_positive = (offset >= 0);
51 return TimeZoneStruct{hour, min, is_positive};
52 }
53
57 inline TimeZoneStruct to_tz(tz_t offset) {
58 return to_time_zone_struct(offset);
59 }
60
61//------------------------------------------------------------------------------
62
67 inline std::string time_zone_struct_to_string(const TimeZoneStruct& tz) {
68 char sign = tz.is_positive ? '+' : '-';
69 return std::string(1, sign) + (tz.hour < 10 ? "0" : "") + std::to_string(tz.hour) + ":" + (tz.min < 10 ? "0" : "") + std::to_string(tz.min);
70 }
71
75 inline std::string to_string(const TimeZoneStruct& tz) {
77 }
78
82 inline std::string to_str(const TimeZoneStruct& tz) {
84 }
85
86//------------------------------------------------------------------------------
87
93 return (tz.is_positive ? 1 : -1) * (tz.hour * SEC_PER_HOUR + tz.min * SEC_PER_MIN);
94 }
95
99 inline tz_t tz_to_offset(const TimeZoneStruct& tz) {
101 }
102
106 inline tz_t to_offset(const TimeZoneStruct& tz) {
108 }
109
110}; // namespace time_shield
111
112#endif // _TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
Header file with time-related constants.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
const std::string & to_str(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
Definition enums.hpp:70
const std::string to_string(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp to string with custom format.
tz_t time_zone_struct_to_offset(const TimeZoneStruct &tz)
Converts a TimeZoneStruct to a single integer representation.
tz_t tz_to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
TimeZoneStruct to_tz(tz_t offset)
Alias for to_time_zone_struct function.
tz_t to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
std::string time_zone_struct_to_string(const TimeZoneStruct &tz)
Converts a TimeZoneStruct to a string representation.
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:58
Main namespace for the Time Shield library.
Structure to represent time zone information.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
bool is_positive
True if the time zone offset is positive, false if negative.
Type definitions for time-related units and formats.