Time Shield Library
C++ library for working with time
Toggle main menu visibility
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_HEADER_TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
4
#define TIME_SHIELD_HEADER_TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
5
8
9
#include "
config.hpp
"
10
#include "
types.hpp
"
11
#include "
constants.hpp
"
12
13
#include <string>
14
15
namespace
time_shield
{
16
22
struct
TimeZoneStruct
{
23
int
hour
;
24
int
min
;
25
bool
is_positive
;
26
};
27
34
inline
TimeZoneStruct
create_time_zone_struct
(
35
int
hour,
36
int
min,
37
bool
is_positive =
true
) {
38
return
TimeZoneStruct
{hour, min, is_positive};
39
}
40
41
//------------------------------------------------------------------------------
42
47
inline
TimeZoneStruct
to_time_zone_struct
(
tz_t
offset) {
48
const
int64_t off =
static_cast<
int64_t
>
(offset);
49
const
int64_t abs_val = (off < 0) ? -off : off;
50
51
const
int
hour =
static_cast<
int
>
(abs_val /
static_cast<
int64_t
>
(
SEC_PER_HOUR
));
52
const
int
min =
static_cast<
int
>
((abs_val %
static_cast<
int64_t
>
(
SEC_PER_HOUR
)) /
53
static_cast<
int64_t
>
(
SEC_PER_MIN
));
54
55
return
TimeZoneStruct
{hour, min, off >= 0};
56
}
57
58
62
inline
TimeZoneStruct
to_tz
(
tz_t
offset) {
63
return
to_time_zone_struct
(offset);
64
}
65
66
//------------------------------------------------------------------------------
67
72
inline
std::string
time_zone_struct_to_string
(
const
TimeZoneStruct
& tz) {
73
char
sign = tz.
is_positive
?
'+'
:
'-'
;
74
return
std::string(1, sign) + (tz.
hour
< 10 ?
"0"
:
""
) + std::to_string(tz.
hour
) +
":"
+ (tz.
min
< 10 ?
"0"
:
""
) + std::to_string(tz.
min
);
75
}
76
80
inline
std::string
to_string
(
const
TimeZoneStruct
& tz) {
81
return
time_zone_struct_to_string
(tz);
82
}
83
87
inline
std::string
to_str
(
const
TimeZoneStruct
& tz) {
88
return
time_zone_struct_to_string
(tz);
89
}
90
91
//------------------------------------------------------------------------------
92
97
TIME_SHIELD_CONSTEXPR
inline
tz_t
time_zone_struct_to_offset
(
const
TimeZoneStruct
& tz)
noexcept
{
98
return
tz.is_positive
99
?
static_cast<
tz_t
>
(
static_cast<
tz_t
>
(tz.hour) *
static_cast<
tz_t
>
(
SEC_PER_HOUR
)
100
+
static_cast<
tz_t
>
(tz.min) *
static_cast<
tz_t
>
(
SEC_PER_MIN
) )
101
:
static_cast<
tz_t
>
(-(
static_cast<
tz_t
>
(tz.hour) *
static_cast<
tz_t
>
(
SEC_PER_HOUR
)
102
+
static_cast<
tz_t
>
(tz.min) *
static_cast<
tz_t
>
(
SEC_PER_MIN
) ));
103
}
104
108
TIME_SHIELD_CONSTEXPR
inline
tz_t
tz_to_offset
(
const
TimeZoneStruct
& tz)
noexcept
{
109
return
time_zone_struct_to_offset
(tz);
110
}
111
115
TIME_SHIELD_CONSTEXPR
inline
tz_t
to_offset
(
const
TimeZoneStruct
& tz)
noexcept
{
116
return
time_zone_struct_to_offset
(tz);
117
}
118
119
};
// namespace time_shield
120
121
#endif
// TIME_SHIELD_HEADER_TIME_SHIELD_TIME_ZONE_STRUCT_HPP_INCLUDED
config.hpp
Configuration macros for the library.
constants.hpp
Header file with time-related constants.
time_shield::SEC_PER_HOUR
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
Definition
constants.hpp:107
time_shield::SEC_PER_MIN
constexpr int64_t SEC_PER_MIN
Seconds per minute.
Definition
constants.hpp:100
time_shield::to_str
const std::string & to_str(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
Definition
enums.hpp:70
time_shield::to_string
const std::string to_string(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp to string with custom format.
Definition
time_formatting.hpp:425
time_shield::to_offset
TIME_SHIELD_CONSTEXPR tz_t to_offset(const TimeZoneStruct &tz) noexcept
Alias for time_zone_struct_to_offset.
Definition
time_zone_struct.hpp:115
time_shield::time_zone_struct_to_offset
TIME_SHIELD_CONSTEXPR tz_t time_zone_struct_to_offset(const TimeZoneStruct &tz) noexcept
Convert a TimeZoneStruct to a numeric UTC offset (seconds).
Definition
time_zone_struct.hpp:97
time_shield::to_tz
TimeZoneStruct to_tz(tz_t offset)
Alias for to_time_zone_struct function.
Definition
time_zone_struct.hpp:62
time_shield::tz_to_offset
TIME_SHIELD_CONSTEXPR tz_t tz_to_offset(const TimeZoneStruct &tz) noexcept
Alias for time_zone_struct_to_offset.
Definition
time_zone_struct.hpp:108
time_shield::to_time_zone_struct
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
Definition
time_zone_struct.hpp:47
time_shield::time_zone_struct_to_string
std::string time_zone_struct_to_string(const TimeZoneStruct &tz)
Converts a TimeZoneStruct to a string representation.
Definition
time_zone_struct.hpp:72
time_shield::create_time_zone_struct
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
Definition
time_zone_struct.hpp:34
time_shield::tz_t
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition
types.hpp:61
time_shield
Main namespace for the Time Shield library.
time_shield::TimeZoneStruct
Structure to represent time zone information.
Definition
time_zone_struct.hpp:22
time_shield::TimeZoneStruct::hour
int hour
Hour component of time (0-23).
Definition
time_zone_struct.hpp:23
time_shield::TimeZoneStruct::min
int min
Minute component of time (0-59).
Definition
time_zone_struct.hpp:24
time_shield::TimeZoneStruct::is_positive
bool is_positive
True if the time zone offset is positive, false if negative.
Definition
time_zone_struct.hpp:25
types.hpp
Type definitions for time-related units and formats.
include
time_shield
time_zone_struct.hpp
Generated by
1.17.0