1

We normally convert long time to string of date and time using mktm() or mktime(). Is there anyway we can do the reverse? I want to convert string of date and time to long time. But I could not find any from the internet. For your info, long time means number of seconds from 01/01/1980 00:00:00.

For example, if long time 1045872000 convert to string time using mktm() or mktime() we can get 21/02/2013 00:00:00. Is there any function for reverse? Convert 21/02/2013 00:00:00 to 1045872000.

Coolguy
  • 2,225
  • 12
  • 54
  • 81
  • http://stackoverflow.com/questions/7524543/convert-a-string-in-to-time-format-in-c is this what you are looking for... basically getdate() function reference: http://pubs.opengroup.org/onlinepubs/007908799/xsh/getdate.html. – Anshul Feb 22 '13 at 08:29
  • Aren't getdate() is the same function as mktm() or mktime()? – Coolguy Feb 22 '13 at 08:39

2 Answers2

1

You have strptime but I dont think it is a standard.

convert a string representation of time to a time tm structure

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

Strftime function is convert the Normaltime to Epoch time

    #include<stdio.h>
   #include<stdlib.h>
    #include<time.h>
    int
   main(void)
   {
       struct tm tm;
       char buf[255];

       strptime("2001-11-13 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
       strftime(buf, sizeof(buf), "%s", &tm);
       puts(buf);
       exit(EXIT_SUCCESS);
   }

This time 2001-11-13 18:31:01 Epoch time is 1005570061

(or)

system function using convert the epoch time.

     system("date -d 'Mon Feb 25 18:05:57 IST 2013' +'%s'");
loganaayahee
  • 809
  • 2
  • 8
  • 13