Coolguy
Coolguy

Reputation: 2285

Convert from string to long time on C language

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.

Upvotes: 1

Views: 1381

Answers (2)

loganaayahee
loganaayahee

Reputation: 819

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'");

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

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

convert a string representation of time to a time tm structure

Upvotes: 1

Related Questions