C K
C K

Reputation: 47

How to get time from string H:M:S in C

I need to extract hours, minutes and seconds from a string formatted as e.g: "00:53:12" to variables a, b and c.

How would I go about this in C?

Thanks in advance!

Upvotes: 2

Views: 386

Answers (2)

werewindle
werewindle

Reputation: 3029

Use standard function strptime:

strptime(timestr,"%H:%M:%S", ret)

Upvotes: 1

Cédric Julien
Cédric Julien

Reputation: 80761

You can use strptime

struct tm tm;

if (strptime("00:53:12", "%H:%M:%S", &tm) != NULL)
   printf("hour: %d; minutes: %d; seconds: %d;\n",
      tm.tm_hour, tm.tm_min, tm.tm_sec);

Upvotes: 7

Related Questions