Home > Back-end >  How to deal with time in C ?
How to deal with time in C ?

Time:10-18

Basically I need to write a program that will ask the user to enter arrival and departure time in the 24 hour format and it needs to calculate the difference between them. I know how to do everything else, but i'm not sure how to make it so that it lets you enter the time.

CodePudding user response:

You can use std::get_time to parse the input into a std::tm structure. You can use std::chrono::system_clock::from_time_t to convert the std::tm object into a time point. You can subtract the time points to calculate the difference.

In C 20, you may streamline the code using std::chrono::parse.

CodePudding user response:

  1. Validate your input with a regex like this regex time.
  2. Use Posix time to create arrival and departure objects.
  3. Use diffTime to get the difference. Example here
  • Related