Thread: How to get and set windows time and date

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    How to get and set windows time and date

    Hi,

    Can you guys show me how to get and set the windows system time and date?
    Is this "time(time_t long_time)" a function to get the windows time and date?
    How about set time and date?
    Please help.
    Thanks!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To change the actual time in the system, you need to use SetSystemTime.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    If I am using SetSystemTime and GetSystemTime, I must include <windows.h> right?

    But I have this compilation error:->

    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\basetsd.h(33) : error C2371: 'INT32' : redefinition; different basic types
    Q:/code/common/std_types.h(68) : see declaration of 'INT32'

    In std_types.h:-
    typedef int32_t INT32;

    In basetsd.h:-
    typedef int INT32, *PINT32;


    How to solve this?
    Please help!
    Thanks a lot!!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I expect that q:/code/common/std_types.h is not a standard file. Who supplied this file?

    INT32 should be defined once, in one place... Although it may of course not be easy to achieve. Also, int32_t is not a standard MS type, as far as I know (it bears resemblance to Linux types - unless I'm confusing myself).

    The other option is to completely isolate the inclusion of windows.h in a separate source file that doesn't include std_types.h. However, this brings a problem of what you pass between this function and the main code that DOES include std_types.h - you can of course rely on INT32 in the two different files, whilst using different methods, achieve the same thing, and thus just use "whichever is present" in a separate header file. But you do have a possible conflict here.

    A third option is to include basetsd.h [or the parent of that file] in std_types.h, and remove any conflicts from std_types.h - this of course assumes that std_types.h is "yours to modify".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Yeah! std_types.h is a self-defined header file and it must not be excluded.
    Meaning that I must include std_types.h in my program and I am not allowed to modify this file.

    Is there any other ways to get and set the system time and date if I am not going to use GetSystemTime and SetSystemTime from "windows.h"?
    I am working on C++ Program.
    Please help.
    Thanks!

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You could try using the Windows console command 'Time'.

    May require you to call 'system' or create a console window with CreateProcess() (depending on your app)

    http://technet.microsoft.com/en-au/l.../bb491015.aspx
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by huwan View Post
    Yeah! std_types.h is a self-defined header file and it must not be excluded.
    Meaning that I must include std_types.h in my program and I am not allowed to modify this file.

    Is there any other ways to get and set the system time and date if I am not going to use GetSystemTime and SetSystemTime from "windows.h"?
    I am working on C++ Program.
    Please help.
    Thanks!
    That is completely silly. If you NEED to use windows.h, but some other file interferes with it, then you MUST raise a bug report on that component - it is broken, and you should not have such a situation. Defining your own symbols that are different from the standard ones supplied by Windows is just not right - you can have your own ones with different names, that's fine.

    If you don't know what to do, just point out to your project manager (or whoever) that you can't actually do your job because of this.

    There may be other ways to solve the problem, but what happens when you come to the next situation where you need to use windows.h, and there happens to be no alternative?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Yeah very silly!!

    Besides of SetSystemTime and GetSystemTime, are there any other c++ functions that can be used to get and set the system time?I mean alternative method.
    I have no choice! Might have to kick away SetSystemTime and GetSystemTime functions!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by huwan View Post
    Yeah very silly!!

    Besides of SetSystemTime and GetSystemTime, are there any other c++ functions that can be used to get and set the system time?I mean alternative method.
    I have no choice! Might have to kick away SetSystemTime and GetSystemTime functions!
    There is no standard way of setting the system time, and I'm not aware of any Visual Studio interface that translates to SetSystemTime at a lower level. The GetSystemTime can be replaced with time() and it's related functions, if you wish.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    I have tried with time() but somehow the compilation failed.
    It says "struct tm" and time() are undefined.
    I have included "time.h" but not sure why it failed.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    how about "settimeofday" and "gettimeofday"?
    Can I use these functions in my c++ program?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by huwan View Post
    how about "settimeofday" and "gettimeofday"?
    Can I use these functions in my c++ program?
    Those are Linux(or Posix?) functions - they do not work in Windows. But from a C++ standpoint, if you have a library with such a function, C++ will allow you to use it.

    Note that the C and C++ languages as such do not require there being any form of time in the system. Most OS's do support some form of timing, because programmers frequently wish for such things, but the language leaves it to the standard library and OS vendor to sort out how to implement the access of the time.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Argghh!! sigh...what can I do?
    I have been searching around and trying with different functions but none of them is working.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by huwan View Post
    Argghh!! sigh...what can I do?
    I have been searching around and trying with different functions but none of them is working.
    I have already suggested that you put the SetSystemTime function in a separate (OS architecture dependant) file.

    The other option is of course that you don't mess with the system time. That would also be a sensible thing to do.

    Edit: Another option is of course that you use some external library - but I couldn't find anything in the Boost library, which is otherwise a common place to find OS independent solutions of this type.

    --
    Mats
    Last edited by matsp; 05-13-2008 at 08:00 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Boost has the DateTime library, but it doesn't allow setting the time.

    What you have here are absurd conflicting requirements. I still think that changing std_types.h is the correct thing to do. It sounds like this is some course assignment, though, and teachers can be stubborn.

    You could also declare just the API functions you need. The undecorated prototypes of GetSystemTime and SetSystemTime are:
    Code:
    extern "C"
    {
    
    struct SYSTEMTIME
    {
      unsigned short wYear;
      unsigned short wMonth;
      unsigned short wDayOfWeek;
      unsigned short wDay;
      unsigned short wHour;
      unsigned short wMinute;
      unsigned short wSecond;
      unsigned short wMilliseconds;
    };
    
    void __stdcall GetSystemTime(SYSTEMTIME *pSystemTime);
    int __stdcall SetSystemTime(const SYSTEMTIME *pSystemTime);
    
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed