Skip to content

Commit 382c3ad

Browse files
authored
Update README and refactor template parameters for improved type safety in parsing functions (#10)
1 parent 170037e commit 382c3ad

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
parse date and times with {fmt} style into`std::chrono::time_point` {WIP}
44

5-
## [Usage](https://godbolt.org/z/PE3s1Y35d)
5+
## [Usage](https://godbolt.org/z/cTK4977vP)
66

77
```C++
88
#include "mgutility/chrono/parse.hpp"

include/mgutility/chrono/parse.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct tm : std::tm {
5959
* @return int32_t The parsed integer.
6060
* @throws std::invalid_argument if the value is not convertible.
6161
*/
62-
template <typename T = int32_t>
62+
template <typename T>
6363
MGUTILITY_CNSTXPR auto parse_integer(T &result, mgutility::string_view str,
6464
uint32_t len, uint32_t &next,
6565
uint32_t begin_offset = 0) -> std::errc {
@@ -79,8 +79,8 @@ MGUTILITY_CNSTXPR auto parse_integer(T &result, mgutility::string_view str,
7979
* @param max The maximum acceptable value.
8080
* @throws std::out_of_range if the value is out of range.
8181
*/
82-
template <typename T = int32_t>
83-
MGUTILITY_CNSTXPR auto check_range(const T& value, const int32_t& min, const int32_t& max) -> std::errc {
82+
template <typename T>
83+
MGUTILITY_CNSTXPR auto check_range(const T& value, const T& min, const T& max) -> std::errc {
8484
if (value < min || value > max) {
8585
return std::errc::result_out_of_range;
8686
}
@@ -93,7 +93,7 @@ MGUTILITY_CNSTXPR auto check_range(const T& value, const int32_t& min, const int
9393
* @param year The year to check.
9494
* @return bool True if the year is a leap year, false otherwise.
9595
*/
96-
auto MGUTILITY_CNSTXPR is_leap_year(uint32_t year) -> bool {
96+
auto MGUTILITY_CNSTXPR is_leap_year(int32_t year) -> bool {
9797
return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0);
9898
}
9999

@@ -129,7 +129,7 @@ MGUTILITY_CNSTXPR auto mktime(std::time_t &result, std::tm &time_struct) -> std:
129129
// Add the days for the current year
130130
for (auto i{0}; i < time_struct.tm_mon; ++i) {
131131
//NOLINTNEXTLINE
132-
result += num_of_days[is_leap_year(time_struct.tm_year)][i];
132+
result += num_of_days[is_leap_year(time_struct.tm_year)][static_cast<std::size_t>(i)];
133133
}
134134

135135
result += time_struct.tm_mday - 1; // nth day since 1970
@@ -260,7 +260,7 @@ MGUTILITY_CNSTXPR auto parse_fraction(detail::tm &result, string_view date_str,
260260
if (error != std::errc{}) {
261261
return error;
262262
}
263-
error = check_range(result.tm_ms, 0, 999);
263+
error = check_range(result.tm_ms, 0U, 999U);
264264
return error;
265265
}
266266

0 commit comments

Comments
 (0)