Time conversions

This is a discussion on Time conversions within the C++ Programming forums, part of the General Programming Boards category; I am generating a clock for my MFC application that figures out what the current time is based on the ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Time conversions

    I am generating a clock for my MFC application that figures out what the current time is based on the selected timezone. What I need to do is calculate an offset using the system time. I get GMT time and figure out what the time is based on an offset, for instance I will calculate what MST is and update the clock to display the current time in the MST timezone.

    Is there a function that will allow me to do this? For example in Java the "Calandar" object has a member function "add", Calendar.add that takes two values ( Calendar.HOURS, offset) and does the addition for you.
    Example: Calendar cal;
    cal.add(Calendar.HOURS, offset);

    THIS IS WHAT I HAVE USED FOR MY C++ CODE SO FAR


    Code:
                   time_t now;
    	struct tm *tm_now;
    	int hour;
    	int minutes;
    	int new_hour;
    
    	now = time ( NULL );
    	tm_now = gmtime ( &now );
                    hours = tm_now->tm_hour;

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    assuming what you have works and gives hours in range 0 - 23, then this sequence should get hours adjusted by offset.

    hours += offset;
    if(hours < 0)
    hours = 24 + hours;
    if(hours >= 24)
    hours = hours - 24;
    //for AM/PM
    if(hours == 0)
    hours = 12
    else(hours > 12)
    hours -= 12;

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    6

    Thank YOU

    Thank You Elad!

    I will use this aglorithm.

    Greatly Apperciative

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  2. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  3. Checking parts of a structure
    By DocDroopy in forum C Programming
    Replies: 11
    Last Post: 08-05-2002, 07:45 AM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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