Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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)

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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

688 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