Skip to content

Commit

Permalink
datetime: Add UTC suffix to ISO-8601 format
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Jan 19, 2024
1 parent 8ab3dfc commit ce5715a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 108 deletions.
7 changes: 4 additions & 3 deletions poseidon/base/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ parse_iso8601_partial(const char* str)
do_match(rptr, tm.tm_min, s_2digit, 2);
do_match(rptr, ":", 1);
do_match(rptr, tm.tm_sec, s_2digit, 2);
do_match(rptr, " UTC", 4);

// Accept nothing if any of the operations above has failed.
if(rptr == nullptr)
Expand Down Expand Up @@ -342,7 +343,7 @@ parse(chars_view str)
if(size_t aclen = this->parse_cookie_partial(str.p))
return aclen;

if(str.n >= 19)
if(str.n >= 23)
if(size_t aclen = this->parse_iso8601_partial(str.p))
return aclen;

Expand Down Expand Up @@ -478,7 +479,7 @@ print_iso8601_partial(char* str) const noexcept
::tm tm;
::gmtime_r(&tp, &tm);

// `1994-11-06 08:49:37`
// `1994-11-06 08:49:37 UTC`
xmemrpcpy(wptr, s_2digit[(uint32_t) tm.tm_year / 100 + 19], 2);
xmemrpcpy(wptr, s_2digit[(uint32_t) tm.tm_year % 100], 2);
xstrrpcpy(wptr, "-");
Expand All @@ -491,7 +492,7 @@ print_iso8601_partial(char* str) const noexcept
xmemrpcpy(wptr, s_2digit[(uint32_t) tm.tm_min], 2);
xstrrpcpy(wptr, ":");
xmemrpcpy(wptr, s_2digit[(uint32_t) tm.tm_sec], 2);
xstrrpcpy(wptr, "");
xstrrpcpy(wptr, " UTC");

// Return the number of characters that have been written.
return (size_t) (wptr - str);
Expand Down
8 changes: 4 additions & 4 deletions poseidon/base/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class DateTime
parse_cookie_partial(const char* str);

// Try parsing a general date/time in the ISO 8601 format. An example is
// `1994-11-06 08:49:37`. The date and time parts shall be separated by a
// space. This function returns the number of characters that have been
// accepted, which is 19 upon success, and 0 upon failure.
// `1994-11-06 08:49:37 UTC`. The date and time parts shall be separated by
// a space. This function returns the number of characters that have been
// accepted, which is 23 upon success, and 0 upon failure.
size_t
parse_iso8601_partial(const char* str);

Expand Down Expand Up @@ -131,7 +131,7 @@ class DateTime
// Converts this timestamp to its ISO 8601 format, with a null terminator.
// There shall be at least 20 characters in the buffer that `str` points to.
// This function returns the number of characters that have been written,
// excluding the null terminator, which is always 19.
// excluding the null terminator, which is always 23.
size_t
print_iso8601_partial(char* str) const noexcept;

Expand Down
Loading

0 comments on commit ce5715a

Please sign in to comment.