Solved

c urgent doubt.. pls help

Posted on 2004-09-26
3
198 Views
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.........


Jango
0
Comment
Question by:jango_ms
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 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,
Jaime.
0
 
LVL 1

Expert Comment

by:rghome
ID: 12154557
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
 

Author Comment

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



Thanks a lot

MS
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 129
Problem with form project 66 314
Using unmanaged DLL from managed application 14 287
UPD maximums on Red Hat 6 102
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now