MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
zoned.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_CALENDARS_ZONED_HPP
20#define MTCORE_CALENDARS_ZONED_HPP
21
22#include "mtcore.hpp"
23#include "time.hpp"
24
25namespace mtcore::calendars {
26 struct TimeZone {
27 struct OffsetHMS {
30 i8 hours = 0;
31 } offsetVal = {};
33
34 [[nodiscard]] constexpr i64 offset_in_nanoseconds() const {
35 const auto secs = offset_in_seconds();
36 return static_cast<i64>(secs) * NANOS_PER_SECOND;
37 }
38
39 [[nodiscard]] constexpr i32 offset_in_seconds() const {
40 const auto sign = (offsetVal.hours >= 0) ? 1 : -1;
41 auto total = static_cast<i32>(offsetVal.hours);
42 total *= 60;
43 total += offsetVal.minutes;
44 total *= 60;
45 total += offsetVal.seconds;
46 total *= sign;
47 return total;
48 }
49
50 [[nodiscard]] constexpr f64 offset_in_minutes() const { return static_cast<f64>(offset_in_seconds()) / 60.; }
51
52 [[nodiscard]] constexpr f64 offset_in_hours() const { return offset_in_minutes() / 60.; }
53
54 [[nodiscard]] constexpr auto offset() const { return offsetVal; }
55
56 [[nodiscard]] constexpr Result<void, TimeValidationError> try_validate() const {
57 if (offsetVal.seconds >= 60) {
59 }
60 if (offsetVal.minutes >= 60) {
62 }
63 if (offsetVal.hours >= 24) {
65 }
66 if (offsetVal.hours <= -24) {
68 }
69 return success();
70 }
71
72 [[nodiscard]] constexpr auto operator<=>(const TimeZone &tz) const {
73 if (const auto c = (offsetVal.hours <=> tz.offsetVal.hours); c != std::strong_ordering::equivalent) {
74 return c;
75 }
76 if (const auto c = (offsetVal.minutes <=> tz.offsetVal.minutes); c != std::strong_ordering::equivalent) {
77 return c;
78 }
79 if (const auto c = (offsetVal.seconds <=> tz.offsetVal.seconds); c != std::strong_ordering::equivalent) {
80 return c;
81 }
82 return std::strong_ordering::equivalent;
83 }
84 };
85
86 namespace zone {
87 constexpr auto utc_name = "UTC";
88 constexpr TimeZone utc() { return TimeZone{{}, slice_from(utc_name)}; }
89 } // namespace zone
90} // namespace mtcore::calendars
91
92#endif // MTCORE_CALENDARS_ZONED_HPP
constexpr i32 NANOS_PER_SECOND
Number of nanoseconds per second.
constexpr auto nullopt
Placeholder value for an empty Optional.
Definition optional.hpp:409
constexpr Slice< const char32_t > slice_from(char32_t *cstr)
Creates a slice from a utf32 string in the form of a c string.
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
int32_t i32
Alias for 32-bit ints.
int64_t i64
Alias for 64-bit ints.
uint8_t u8
Alias for 8-bit unsigned ints.
double f64
Alias for 64-bit floats.
int8_t i8
Alias for 8-bit ints.
constexpr auto utc_name
Definition zoned.hpp:87
constexpr TimeZone utc()
Definition zoned.hpp:88
Namespace for calendaring systems.
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Definition optional.hpp:235
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Definition result.hpp:170
constexpr f64 offset_in_hours() const
Definition zoned.hpp:52
constexpr auto offset() const
Definition zoned.hpp:54
Optional< Slice< const char > > name
Definition zoned.hpp:32
constexpr auto operator<=>(const TimeZone &tz) const
Definition zoned.hpp:72
constexpr i32 offset_in_seconds() const
Definition zoned.hpp:39
struct mtcore::calendars::TimeZone::OffsetHMS offsetVal
constexpr f64 offset_in_minutes() const
Definition zoned.hpp:50
constexpr Result< void, TimeValidationError > try_validate() const
Definition zoned.hpp:56
constexpr i64 offset_in_nanoseconds() const
Definition zoned.hpp:34