fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int timestr2sec(char *c) {
  5. int i, a, n;
  6. int len, offset;
  7.  
  8. len = strlen(c);
  9. offset = len - 8;
  10.  
  11. n = (int)(c[7 + offset] - '0') +
  12. (int)((c[6 + offset] - '0') * 10) +
  13. (int)((c[4 + offset] - '0') * 60) +
  14. (int)((c[3 + offset] - '0') * 600) +
  15. (int)((c[1 + offset] - '0') * 3600) +
  16. (int)((c[0 + offset] - '0') * 36000);
  17. a = 360000;
  18. for (i = offset - 1; i >= 0; i--) {
  19. n += (int)((c[i] - '0') * a);
  20. a *= 10;
  21. }
  22. return n;
  23. }
  24.  
  25. int main(void) {
  26. // your code goes here
  27. char *t1 = "01:23:45";
  28. char *t2 = "333:33:33";
  29. char *t3 = "99:59:59";
  30. char *t4 = "1000:00:00";
  31.  
  32. printf("%s = %d sec\n", t1, timestr2sec(t1));
  33. printf("%s = %d sec\n", t2, timestr2sec(t2));
  34. printf("%s = %d sec\n", t3, timestr2sec(t3));
  35. printf("%s = %d sec\n", t4, timestr2sec(t4));
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 4488KB
stdin
Standard input is empty
stdout
01:23:45 = 5025 sec
333:33:33 = 1200813 sec
99:59:59 = 359999 sec
1000:00:00 = 3600000 sec