Thread: How to get and set windows time and date

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    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);
    
    }
    That _IS_ a solution, I did consider it, but thought it "ugly enough" that I didn't want to suggest that. One consequence is that IF at any time, Get/SetSystemTime's interface is modified [unlikely, I know], it will not cause any compile time problem, but rather a run-time problem. It is also subject to compiler wide settings for alignment within structs, which is normally well defined by the Windows.h

    --
    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.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    True. But I'm relying on MS not screwing themselves. Any ABI change in this function would break endless amounts of code.
    The packing thing is the only problem. You can fix that by adding packing pragmas.
    Code:
    // Before the struct:
    #pragma pack(push, 1)
    // After the struct:
    #pragma pack(pop)
    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

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Fair enough.

    However, I still think this is a poor solution - the correct thing to do would be to fix up whatever needs to be fixed to make Windows.h possible to include (at least into a separate source file).

    What happens when the original poster needs another Windows function, where do you draw the limit for having your own declarations of the Windows API? 3, 5, 10 or 100 functions?

    --
    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.

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course it's a poor solution. It's a poor situation - forced to use an unmodifiable header that has a useless typedef that conflicts with windows.h.
    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