#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep 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

    :(
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11
    woops sorry heres my initilizations

    struct tm *out_timeptr, *in_timeptr;
    time_t out_time, in_time;
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    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);
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    33
    Rep Power
    11
    :D Awsome! thank you. I didnt see that

IMN logo majestic logo threadwatch logo seochat tools logo