How to decrease a time string by a given value

This is a discussion on How to decrease a time string by a given value within the C Programming forums, part of the General Programming Boards category; Hi, I have a string containing a time in format: "yyyy-mm-dd:HH:MM:SS" I have to decrease this time value by a ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    How to decrease a time string by a given value

    Hi,

    I have a string containing a time in format: "yyyy-mm-dd:HH:MM:SS"

    I have to decrease this time value by a 3-digit hour value or 3-digit day value.

    What could be the best of doing it? Only way I can think of is:
    1) convert the string's time into seconds
    2) convert the specified value into seconds
    3) subtract them
    4) convert the result back to a string.

    Is there a better/more efficient way of doing it???

    Thanks,
    - Ruchikar.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Why not get the time, store its values as integers or something so you can do the manipluation, then convert them to a string by using itoa or something for display?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Read up on the mktime function

    You can say things like
    tm.tm_day += 3;

    Then use mktime to normalise the time back into the correct ranges.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Or you could create a struct and write functions to manipulate with it.

    >yyyy-mm-dd:HH:MM:SS

    Something like this perhaps.

    Code:
    struct TimeStruct
    {
        int yyyy;
        int mm;
        int dd;
        int hh;
        int mm;
        int ss;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21