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#pragma once
4#ifndef _TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
5#define _TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
6
7#include "types.hpp"
8#include "constants.hpp"
9
10namespace time_shield {
11
17 int hour;
18 int min;
20 };
21
28 const int& hour,
29 const int& min,
30 const bool& is_positive = true) {
31 return TimeZoneStruct{hour, min, is_positive};
32 }
33
34//------------------------------------------------------------------------------
35
40 const tz_t& offset) {
41 int abs_val = std::abs(offset);
42 int hour = abs_val / SEC_PER_HOUR;
43 int min = abs_val % SEC_PER_MIN;
44 bool is_positive = (offset >= 0);
45 return TimeZoneStruct{hour, min, is_positive};
46 }
47
50 inline const TimeZoneStruct to_tz(const tz_t& offset) {
51 return to_time_zone_struct(offset);
52 }
53
54//------------------------------------------------------------------------------
55
59 inline const std::string time_zone_struct_to_string(const TimeZoneStruct& tz) {
60 char sign = tz.is_positive ? '+' : '-';
61 return std::string(1, sign) + (tz.hour < 10 ? "0" : "") + std::to_string(tz.hour) + ":" + (tz.min < 10 ? "0" : "") + std::to_string(tz.min);
62 }
63
66 inline const std::string to_string(const TimeZoneStruct& tz) {
68 }
69
72 inline const std::string to_str(const TimeZoneStruct& tz) {
74 }
75
76//------------------------------------------------------------------------------
77
82 return (tz.is_positive ? 1 : -1) * (tz.hour * SEC_PER_HOUR + tz.min * SEC_PER_MIN);
83 }
84
87 inline const tz_t tz_to_offset(const TimeZoneStruct& tz) {
89 }
90
93 inline const tz_t to_offset(const TimeZoneStruct& tz) {
95 }
96
97}; // namespace time_shield
98
99#endif // _TIME_SHIELD_TIME_STRUCT_HPP_INCLUDED
Header file with time-related constants.
Main namespace for the Time Shield library.
Definition constants.hpp:12
const std::string & to_str(const Weekday &value, const FormatType &format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
Definition enums.hpp:63
const TimeZoneStruct create_time_zone_struct(const int &hour, const int &min, const bool &is_positive=true)
Creates a TimeZoneStruct instance.
const std::string to_string(const std::string &format_str, const T &timestamp, const tz_t &utc_offset=0)
Convert timestamp to string with custom format.
const TimeZoneStruct to_time_zone_struct(const tz_t &offset)
Converts an integer to a TimeZoneStruct.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
Definition constants.hpp:28
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
Definition constants.hpp:30
const tz_t time_zone_struct_to_offset(const TimeZoneStruct &tz)
Converts a TimeZoneStruct to a single integer representation.
const tz_t tz_to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
const std::string time_zone_struct_to_string(const TimeZoneStruct &tz)
Converts a TimeZoneStruct to a string representation.
const TimeZoneStruct to_tz(const tz_t &offset)
Alias for to_time_zone_struct function.
const tz_t to_offset(const TimeZoneStruct &tz)
Alias for time_zone_struct_to_offset function.
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.
Header file with type definitions.