Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

c urgent doubt.. pls help

Hello Experts,
                      I have an API function which accepts time_t datatype parameter. I am giving this time as a command line argument. so I am accepting time  as char* datatype, and then converted it using


ptime = (time_t)iTime;
iTime is char* datatype. But my pTime (type time_t) is not working........

But when I am giving the time hardcoded ino the API function its working.........I printed the iTime, and I got the same time ( its unix timestamp) as I entered in command line. But when I give pTime to the API function its not working. THe API function only takes time_t paramter for processing.. Please help. Early responses in grealty appreciated.........


Jango
0
jango_ms
Asked:
jango_ms
1 Solution
 
Jaime OlivaresCommented:
Hi jango_ms,

Maybe you can show us an example of your command line call.

time_t handles a unique 32 bit integer value, well know as Unix time, it is expressed as seconds since 1/1/1970, currently is greater than a billion.
so, you can't convert a char * to an integer succefully. Assuming your command line is like this:

somecommand 1023678654

Maybe a valid convertion could be:
ptime = (time_t)atoi(iTime);

But that will depend of what you have in your command line.

Good luck,
Jaime.
0
 
rghomeCommented:
Jango,

You are making a common mistake that is often made by C programmers.

When you say "ptime = (time_t)iTime;" you are not 'converting' the char * type to a time_t type, you are 'casting' it. A cast is different from a conversion. When you cast something, you are basically saying "ignore the type that the original object is declared as and pretend that it is a the new type"; but the thing you are casting actually has really to be of the new type for it to work - casting does not do any convertsion. It's an advanced subject, and best avoided unless you really understand it, although it is usually safe to cast shorts to ints and things like that.

What you want is a date conversion function, and actually, I don't think that original 'C' libraries provided one. You could try looking for the strptime function on your system though. (see here for a description) http://www.opengroup.org/onlinepubs/007908799/xsh/strptime.htm.

After calling strptime, you need to call mktime with the result to get a time_t value.

If you don't have strptime, it is easy enough to write one. This is off the top of my head, so I can't guarn

First decide the format you are going to input the date/time. Let's assume dd/mm/yyyy hh:mm:ss. If you are an American, you might want to change this.

You then want to use the functions provided in <time.h>

#include <time.h>
#include <stdio.h>

int main(int argc, char**argv) {

    char *iTime = "03/10/2004 14:45:23";
    struct tm tm;
    time_t theTime;
   
    sscanf(iTime, "%02d/%02d/%04d %02d:%02d:%02d", &tm.tm_mday, &tm.tm_mon, &tm.tm_year, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
   
    tm.tm_mon -= 1; /* Months count from zero */
    tm.tm_year -= 1900; /* tm accepts year as years from 1900 */
    tm.tm_isdst = -1; /* The tm_isdst member is positive if daylight savings time is in effect, zero if not and negative if that information is not available */
   
     theTime = mktime(&tm);
   
     printf("%s\n", ctime(&theTime)); /* test it */
     return 0;
}

0
 
jango_msAuthor Commented:
Thanx jaime_olivares, that helped me in solving the problem.......



Thanks a lot

MS
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now