RelaxWithMyHorns
RelaxWithMyHorns

Reputation: 1

C++ Question: From string to time_t object?

I have a program I'm writing to take inputs from the command line or a file with the format: 9999.365 --> Fri 31 Dec 9999 Where the output needs to look like the second part of that example. I'm having trouble understanding how to go about taking a string formatted like xxxx.xxx and converting it to a proper time_t object representing a value. Anyone have advice for this?

I've tried looking up how to do so, but my C++ knowledge is not wide and I'm very new to the language so much of what I've found doesn't appear to work or I just don't understand it.

Upvotes: 0

Views: 165

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275896

You could simply use the stream parsing utilities in std::chrono.

using time_point = std::chrono::system_clock::time_point;
std::optional<time_point> parse_time( std::string date_string )
{
  std::string_stream ss(date_string);
  time_point retval;
  if (!from_stream(ss, "%Y.%j", retval))
    return std::nullopt;
  return retval;
}

for time_t we just then convert:

std::optional<time_t> parse_time_t( std::string date_string ) {
  if (auto time = parse_time(date_string))
    return std::chrono::system_clock::to_time_t(*time);
  return std::nullopt;
}

Use:

if (auto time = parse_time_t( "1999.1" )) {
  // *time is now jan 1, 1999
} else {
  // parse failed
}

you could probably make this faster by rolling your own parser, as stream-based C++ isn't all that super fast, but if you aren't doing this on a per-pixel*frame basis (or similar) I wouldn't bother. (Ie, take a string, split it on ., and check there are 2 parts, convert the first to a year, make a time point, add on the day, etc).

Adding some std::moves would be microoptimizations. Moving over to a std::string_view might also improve things, but stream-based parsing of std::string_view is C++23.

I'd discourage use of time_t unless you need it for interop with C libraries or the like. C++'s std::chrono time primitives are far less ancient in design; there are a pile of bugs that are difficult to generate with std::chrono but are trivial to hit with time_t.

Upvotes: 1

Related Questions