Skip to content

Commit 046f9a3

Browse files
authored
Refactor get_time function to simplify format specifier parsing and improve error handling (#7)
* Refactor get_time function to simplify format specifier parsing and improve error handling * Add trunk ignore rules for specific directories and files; update test and header files to ignore clang-format * Update trunk.yaml to ignore 'scripts/**' instead of 'workspaces/**' in linting configuration
1 parent ad11986 commit 046f9a3

File tree

4 files changed

+28
-59
lines changed

4 files changed

+28
-59
lines changed

.trunk/trunk.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ lint:
3030
3131
3232
33+
34+
ignore:
35+
- linters: [ALL]
36+
paths:
37+
- build/**
38+
- tests/**
39+
- scripts/**
40+
- .vscode/**
41+
- .devcontainer/**
42+
- .github/**
43+
- .trunk/**
44+
- .git/**
45+
3346
actions:
3447
disabled:
3548
- trunk-announce

include/mgutility/chrono/parse.hpp

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#ifndef MGUTILITY_CHRONO_PARSE_HPP
2626
#define MGUTILITY_CHRONO_PARSE_HPP
2727

28+
// trunk-ignore-all(clang-format)
29+
2830
#include "mgutility/std/charconv.hpp"
2931
#include "mgutility/std/string_view.hpp"
3032

@@ -304,25 +306,13 @@ MGUTILITY_CNSTXPR auto parse_timezone_offset(detail::tm &result, string_view dat
304306
MGUTILITY_CNSTXPR auto get_time(detail::tm &result, string_view format,
305307
string_view date_str) -> std::errc {
306308
int32_t count{0};
307-
uint32_t begin{0}, end{0};
309+
std::size_t begin{0}, end{0};
308310

309311
// Find the positions of format specifiers
310-
for (auto i{0}; i < format.size(); ++i) {
311-
switch (format[i]) {
312-
case '{':
313-
begin = i;
314-
++count;
315-
break;
316-
case '}':
317-
end = i;
318-
--count;
319-
break;
320-
}
321-
if (begin < end)
322-
break;
323-
else if (count != 0 && end < begin)
324-
break;
325-
}
312+
begin = format.find('{');
313+
end = format.find('}');
314+
if (begin == string_view::npos || end == string_view::npos || begin >= end)
315+
return std::errc::invalid_argument;
326316

327317
if (format[begin + 1] != ':' || (end - begin < 3 || count != 0))
328318
return std::errc::invalid_argument;
@@ -340,83 +330,44 @@ MGUTILITY_CNSTXPR auto get_time(detail::tm &result, string_view format,
340330
switch (format[i + 1]) {
341331
case 'Y': // Year with century (4 digits)
342332
error = parse_year(result, date_str, next);
343-
if (error != std::errc{}) {
344-
return error;
345-
}
346333
break;
347334
case 'm': // Month (01-12)
348335
error = parse_month(result, date_str, next);
349-
if (error != std::errc{}) {
350-
return error;
351-
}
352336
break;
353337
case 'd': // Day of the month (01-31)
354338
error = parse_day(result, date_str, next);
355-
if (error != std::errc{}) {
356-
return error;
357-
}
358339
break;
359340
case 'F': { // Full date (YYYY-MM-DD)
360341
error = parse_year(result, date_str, next);
361-
if (error != std::errc{}) {
362-
return error;
363-
}
364342
error = parse_month(result, date_str, next);
365-
if (error != std::errc{}) {
366-
return error;
367-
}
368343
error = parse_day(result, date_str, next);
369-
if (error != std::errc{}) {
370-
return error;
371-
}
372344
} break;
373345
case 'H': // Hour (00-23)
374346
error = parse_hour(result, date_str, next);
375-
if (error != std::errc{}) {
376-
return error;
377-
}
378347
break;
379348
case 'M': // Minute (00-59)
380349
error = parse_minute(result, date_str, next);
381-
if (error != std::errc{}) {
382-
return error;
383-
}
384350
break;
385351
case 'S': // Second (00-59)
386352
error = parse_second(result, date_str, next);
387-
if (error != std::errc{}) {
388-
return error;
389-
}
390353
break;
391354
case 'T': { // Full time (HH:MM:SS)
392355
error = parse_hour(result, date_str, next);
393-
if (error != std::errc{}) {
394-
return error;
395-
}
396356
error = parse_minute(result, date_str, next);
397-
if (error != std::errc{}) {
398-
return error;
399-
}
400357
error = parse_second(result, date_str, next);
401-
if (error != std::errc{}) {
402-
return error;
403-
}
404358
} break;
405359
case 'f': // Milliseconds (000-999)
406360
error = parse_fraction(result, date_str, next);
407-
if (error != std::errc{}) {
408-
return error;
409-
}
410361
break;
411362
case 'z': { // Timezone offset (+/-HHMM)
412363
error = parse_timezone_offset(result, date_str, next);
413-
if (error != std::errc{}) {
414-
return error;
415-
}
416364
} break;
417365
default:
418366
return std::errc::invalid_argument;
419367
}
368+
if (error != std::errc{}) {
369+
return error;
370+
}
420371
} break;
421372
case ' ': // Space separator
422373
case '-': // Dash separator

include/mgutility/std/charconv.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ SOFTWARE.
2323
*/
2424
#ifndef MGUTILITY_STD_CHARCONV_HPP
2525
#define MGUTILITY_STD_CHARCONV_HPP
26+
27+
// trunk-ignore-all(clang-format)
28+
2629
#include "mgutility/_common/definitions.hpp"
2730
#include <system_error>
2831

tests/test_chrono_parse.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "mgutility/chrono/parse.hpp"
44
#include <chrono>
55

6+
// trunk-ignore-all(clang-format)
7+
68
template <typename T>
79
auto to_milliseconds(T time_point) -> std::chrono::milliseconds {
810
return std::chrono::duration_cast<std::chrono::milliseconds>(time_point.time_since_epoch());

0 commit comments

Comments
 (0)