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