Thread: Getting local time from window using C++

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    Getting local time from window using C++

    I'm trying to get the local time from window using C++.

    I tired using systemtime and systemdate, but it seems they were off. I think it's using GMT-0 if i'm not mistaken.

    I manage to get the local date using #include <ctime>

    Code:
    #include <iostream>
    #include <ctime>
    using namespace std ;
    int main( )
    {
        char buffer[BUFSIZ] = { '\0' } ;
        time_t now = time( &now ) ;
        struct tm* local_time = new tm( ) ;
        local_time = localtime( &now ) ;
        strftime( buffer, BUFSIZ, "%d / %m / %Y", local_time ) ;
        system("PAUSE\n");
        cout << buffer  ;
        cout << "\n";
        system("PAUSE");
    }
    however i'm not sure where to begin to look for getting the local time. Could someone give me some advice? Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If you know or can obtain from the system the time difference, a multiplication and an addition will do.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    I thought of that before, however then i would be faced with the problem of running into different timezones if i have a set multiplicator. I don't want my program to be asking the user for his/her time zone.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Hmm, constructing a time_t value by interpreting the struct tm returned by localtime() is also possible, and probably the most portable way.
    Devoted my life to programming...

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Lookup strftime in your favourite reference and plug in the specific character after the percent sign.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Thanks, it works now.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could take a look at Boost's Time/Date library. Very easy to use, contrast to the C functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting local and greenwich time
    By xyzt in forum C Programming
    Replies: 2
    Last Post: 08-01-2010, 09:07 AM
  2. Convert a local time to a timezone's bias
    By NightCode in forum Windows Programming
    Replies: 1
    Last Post: 03-09-2010, 11:45 AM
  3. getting local time without daylight savings
    By underthesun in forum C Programming
    Replies: 5
    Last Post: 06-30-2009, 09:33 AM
  4. Local Time Program
    By Ronzel in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2009, 07:19 AM
  5. Comparing local time
    By zach0616 in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2005, 08:55 AM