Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 9th, 2003, 03:50 PM
 samlab
Contributing User

Join Date: Sep 2003
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 11
Help!! How to convert string to t_time or tm

I am Using C. ok, heres my problem. i have two strings that are in the format: mm/dd/yyyy hh:mm:ss . i need to subtract one from the other and get an acurate result. after looking through the time.h library i came across diftime(t_time, t_time). so to use this i need to convert my string to either a type t_time; or convert it to type tm then to t_time. does anyone know how to do this? is there an easier way to do what i am trying to do?

i tryed to convert from the string to all integers and then to the tm pointer like this:
(NOTE: out_hour, out_min, out_sec, etc. are holding the appropriate strings)

int outhour, outmin, outsec, inhour, inmin, insec, outday, outmonth, outyear, inday, inmonth, inyear;

tm *out_timeptr, *in_timeptr;

outhour = atoi(out_hour);
outmin = atoi(out_min);
outsec = atoi(out_sec);
inhour = atoi(in_hour);
inmin = atoi(in_min);
insec = atoi(in_sec);
inday = atoi(in_day);
inmonth = atoi(in_month);
inyear = atoi(in_year);
outday = atoi(out_day);
outmonth = atoi(out_month);
outyear = atoi(out_year);

in_timeptr.tm_sec = insec;
in_timeptr.tm_min = inmin;
in_timeptr.tm_hour = inhour;
in_timeptr.tm_mday = inday;
in_timeptr.tm_mon = inmonth;
in_timeptr.tm_year = inyear;
out_timeptr.tm_sec = outsec;
out_timeptr.tm_min = outmin;
out_timeptr.tm_hour = outhour;
out_timeptr.tm_mday = outday;
out_timeptr.tm_mon = outmonth;
out_timeptr.tm_year = outyear;

when i try to assign to the tm pointers i get errors: must have class/struct/union type
thanks for any help

#2
September 9th, 2003, 04:09 PM
 samlab
Contributing User

Join Date: Sep 2003
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 11
ok, i tryed changing it to:

in_timeptr->tm_sec = insec;
in_timeptr->tm_min = inmin;
in_timeptr->tm_hour = inhour;
in_timeptr->tm_mday = inday;
in_timeptr->tm_mon = inmonth;
in_timeptr->tm_year = inyear;
out_timeptr->tm_sec = outsec;
out_timeptr->tm_min = outmin;
out_timeptr->tm_hour = outhour;
out_timeptr->tm_mday = outday;
out_timeptr->tm_mon = outmonth;
out_timeptr->tm_year = outyear;

/* convert from tm to time_t */
out_time = mktime(out_timeptr);
in_time = mktime(in_timeptr);

now it compiles, but i get a runtime error as soon as i get to the first line of this -- acces violation

#3
September 9th, 2003, 04:17 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,538
Time spent in forums: 2 Months 3 Days 8 h 51 m 52 sec
Reputation Power: 4106
Bet you didn't initialize in_timeptr or out_timeptr
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#4
September 9th, 2003, 04:29 PM
 samlab
Contributing User

Join Date: Sep 2003
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 11
woops sorry heres my initilizations

struct tm *out_timeptr, *in_timeptr;
time_t out_time, in_time;

#5
September 9th, 2003, 04:35 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,538
Time spent in forums: 2 Months 3 Days 8 h 51 m 52 sec
Reputation Power: 4106
Like I said, you haven't initialized (i.e. set the pointer values) of outtime_ptr and intime_ptr. It is not sufficient to merely declare the variables. You must point them somewhere. However, instead of using pointers, just declare variables of type struct tm and set the values like this:
Code:
```time_t out_time, in_time;
struct tm invar, outvar;

invar.tm_sec = insec;
invar.tm_min = inmin;
..
..
outvar.tm_year = tmyear;

intime = mktime(&invar);
outtime = mktime(&outvar);```

#6
September 9th, 2003, 04:42 PM
 samlab
Contributing User

Join Date: Sep 2003
Posts: 33
Time spent in forums: < 1 sec
Reputation Power: 11
Awsome! thank you. I didnt see that

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help!! How to convert string to t_time or tm