Thread: Adding and subtracting time?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Question Adding and subtracting time?

    I am new to the programming field. I have been looking for examples of, or resources to the adding or subtracting of time variables in C/C++.

    For example: given the time 20:30 Monday night and 04:30 Tuesday morning what is the difference?
    Also adding 14:30 + 30:00 = 44:30.

    I'm just looking for a starting place. All the time functions I've come across only have to do with obtaining the current date and time from the operating system. I'm looking to build a program that can manipulate given times somewhat like what an employer would need for managing a timeclock (I'm not an employer).

    Thanx,

    JCCC

  2. #2
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    Use a circular class to implement it. That is, when a value like the hours goes above 24, it resets the hours to 0 and increments you day variable, if you have one.

    On a project, we had to do alot of time manipulations. We used seconds as the units so we did not have to worry with carrying over the minutes and hours.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Smile

    Does anyone have an example or a resource of samples?

    JCCC

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You need the modulo operator, this operator gets the remainder.

    7 % 3 = 1

    (7 / 3 = 2, 2 * 3 = 6, 7 - 6 = 1)

    Example for time calculations:

    20 hr + 6 hr = 26 hr
    26 hr % 24 hr = 2 hr
    26 hr / 24hr = 1 day

    59 min + 3 min = 62 min
    62 min % 60 min = 2 min
    62 min / 60 min = 1 hr

    so

    20:59 + 6:3 = 3:2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. subtracting fractions in classes
    By rushhour in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2009, 07:26 PM
  2. Adding and subtracting.
    By erikcn in forum C++ Programming
    Replies: 2
    Last Post: 06-01-2002, 05:01 AM