Results 1 to 15 of 15

Thread: Getting the time..

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Getting the time..

    Can anyone tell me how to find out what time it is on the system?

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Getting the time..

    Code:
    time_t t;
    time(&t);
    // t now contains the current time
    (google for headers and how to use time_t)

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    k thx...
    // function example
    #include <iostream>
    #include <TIME.H>
    using namespace std;


    int main ()
    {
    time_t t;
    time(&t);
    cout << t;
    system ( "pause" );
    }

    returns some crazy ass numbers..

  4. #4
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Getting the time..

    You should REALLY stop using namespaces and system() calls.... That having been said, you need to convert time_t to a FILETIME or SYSTEMTIME struct before you can get the actual time from it.
    http://msdn.microsoft.com/library/de..._file_time.asp

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    i dont get what the big deal is, it pauses my code and works fine..i cant understand whats so terrible about it..

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Getting the time..

    Assuming you're using windows, you can also do something like this:

    Code:
    #include <windows.h>
    // ...
    SYSTEMTIME cur_time;
    GetSystemTime(&cur_time); // get UTC time
    // ....
    GetLocalTime(&cur_time); // get local time
    Check out this page for info in the SYSTEMTIME struct.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    can you please give me a usage?
    like so i can actually get the date and time and such?thx

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Getting the time..

    Just click on the link in sunburnt's post, it gives all the information you need.

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    i wouldnt ask if i understood it.

  10. #10
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Getting the time..

    What don't you understand? It sais that the structure looks like:
    Code:
    typedef struct _SYSTEMTIME {
      WORD wYear;
      WORD wMonth;
      WORD wDayOfWeek;
      WORD wDay;
      WORD wHour;
      WORD wMinute;
      WORD wSecond;
      WORD wMilliseconds;
    } SYSTEMTIME, 
    *PSYSTEMTIME;
    So after running the code in sunburnt's post this will print the current date for example:
    Code:
    std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    thankyou. all i need is code, and i can mostly figure things out..except now ive never seen this error before, so im gonna post it

    VB Code:
    1. #include <iostream>
    2. #include <windows.h>
    3.  
    4. using namespace std;
    5.  
    6. int main ()
    7. {
    8. // ...
    9. SYSTEMTIME cur_time;
    10. std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20         ";
    11. system ( "pause" );
    12. }
    13.  
    14.  
    15. typedef struct _SYSTEMTIME {
    16.   WORD wYear;
    17.   WORD wMonth;
    18.   WORD wDayOfWeek;
    19.   WORD wDay;
    20.   WORD wHour;
    21.   WORD wMinute;
    22.   WORD wSecond;
    23.   WORD wMilliseconds;
    24. } SYSTEMTIME,
    25. *PSYSTEMTIME;

    C:\Program Files\Microsoft Visual Studio\VC98\Projects\Cpp1.cpp(16) : error C2011: '_SYSTEMTIME' : 'struct' type redefinition

    over typedef struct_SYSTEMTIME{

  12. #12
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Getting the time..

    You don't need to inlcude the typedef struct SYSTIME in your own code. It is already in windows.h

  13. #13

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    wow, kthx, that makes alot of sense now!
    but my result does not.

    im getting 52428-52428-52428
    instead of 2005-5-20
    VB Code:
    1. #include <iostream>
    2. #include <windows.h>
    3.  
    4. using namespace std;
    5.  
    6. int main ()
    7. {
    8. // ...
    9. SYSTEMTIME cur_time;
    10. std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20         ";
    11. }

  14. #14
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Getting the time..

    You need to get the date first using GetSystemTime(&cur_time); or GetLocalTime.

  15. #15

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Getting the time..

    i love you.

    edit**
    for reference

    Code:
    // function example
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main ()
    {
    // ...
    SYSTEMTIME cur_time;
    	GetSystemTime(&cur_time);
    std::cout << cur_time.wYear << "-" << cur_time.wMonth << "-" << cur_time.wDay; // print date as 2005-5-20         ";
    system( "pause" );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width