How to get and set windows time and date

This is a discussion on How to get and set windows time and date within the Windows Programming forums, part of the Platform Specific Boards category; Originally Posted by CornedBee Boost has the DateTime library, but it doesn't allow setting the time. What you have here ...

  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,892
    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,892
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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