c urgent doubt.. pls help

Posted on 2004-09-26
Medium Priority
Last Modified: 2010-04-15
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.........

Question by:jango_ms
LVL 55

Accepted Solution

Jaime Olivares earned 500 total points
ID: 12154500
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,

Expert Comment

ID: 12154557

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;


Author Comment

ID: 12157641
Thanx jaime_olivares, that helped me in solving the problem.......

Thanks a lot


Featured Post

Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question