C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-01-2006, 06:14 PM   #1
Registered User
 
Join Date: Oct 2003
Posts: 104
Read and set\change system time

I want to be able to set the time on my comp, (saving the time it was before setting would be nice too!), I looked around and found stuff with time.h, the most usefull thing I found had sys/time.h in its directives, but when I try to put sys/time.h in my compiler Visual studio 2005 which I must use, it says that isn't found.

All I want to be able to do is set the time to something like the year 1999, { MAYBE if it's easy then ... let me enter the date, 12 June, so anything like that and make my time set.
Hexxx is offline   Reply With Quote
Old 01-01-2006, 06:25 PM   #2
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
I'm pretty sure the solution depends on what OS you're running. For windows look at the msdn time functions

There's even an example of setting the system time.
__________________
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)

"Work hard and it might happen."
-XSquared
JaWiB is offline   Reply With Quote
Old 01-01-2006, 07:17 PM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
Time is still supported in MSVC, but I'm not sure about 2005.

From MSVC 6
Quote:
Time Management
Use these functions to get the current time and convert, adjust, and store it as necessary. The current time is the system time.

The _ftime and localtime routines use the TZ environment variable. If TZ is not set, the run-time library attempts to use the time-zone information specified by the operating system. If this information is unavailable, these functions use the default value of PST8PDT. For more information on TZ, see _tzset; also see _daylight, timezone, and _tzname.

Time Routines

Function Use
asctime, _wasctime Convert time from type struct tm to character string
clock Return elapsed CPU time for process
ctime, _wctime Convert time from type time_t to character string
difftime Compute difference between two times
_ftime Store current system time in variable of type struct _timeb
_futime Set modification time on open file
gmtime Convert time from type time_t to struct tm
localtime Convert time from type time_t to struct tm with local correction
mktime Convert time to calendar value
_strdate, _wstrdate Return current system date as string
strftime, wcsftime Format date-and-time string for international use
_strtime, _wstrtime Return current system time as string
time Get current system time as type time_t
_tzset Set external time variables from environment time variable TZ
_utime, _wutime Set modification time for specified file using either current time or time value stored in structure


Note In all versions of Microsoft C/C++ except Microsoft C/C++ version 7.0, and in all versions of Microsoft Visual C++, the time function returns the current time as the number of seconds elapsed since midnight on January 1, 1970. In Microsoft C/C++ version 7.0, time returned the current time as the number of seconds elapsed since midnight on December 31, 1899.


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
I'm sure 2005 still allows you to use the old <time.h> if you want to.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 01-01-2006, 08:25 PM   #4
Registered User
 
Join Date: Aug 2005
Posts: 1,265
for MS-Windows, use win32 api functions GetSystemTime() and SetSystemTime(). see MSDN for function details. Both use SYSTEMTIME structore, and does NOT use anything in time.h

Last edited by Ancient Dragon; 01-01-2006 at 08:28 PM.
Ancient Dragon is offline   Reply With Quote
Old 01-01-2006, 09:10 PM   #5
Registered User
 
Join Date: Oct 2003
Posts: 104
ok cool, I belive the msdn fuction should help me on my way...
Hexxx is offline   Reply With Quote
Old 01-01-2006, 10:13 PM   #6
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
And for MFC you can use the CTime class which encapsulates a lot of the nitty gritty stuff and really makes it a lot simpler to work with time and time structures.

My point here is that all 3 methods still work as designed. C, Win32, and MFC all work.
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 01-01-2006, 10:44 PM   #7
carry on
 
JaWiB's Avatar
 
Join Date: Feb 2003
Location: Seattle, WA
Posts: 1,971
But is it possible to set the system time using only standard functions? Correct me if I'm wrong, but I thought you could only get the time, not change it.
__________________
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)

"Work hard and it might happen."
-XSquared
JaWiB is offline   Reply With Quote
Old 01-02-2006, 01:24 AM   #8
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
I believe you can only get the time, not change it. The reasons are quite obvious. Why would you want to change the date and time of the machine you are working on? The time is gathered from the BIOS and the only way it gets messed up is if the battery dies or some other malfunction occurs.

This is probably why they do not let you set the time. And Windows does NOT alter the time of the BIOS, it simply adjusts for the locale settings of the user (BIOS +/- adjust_hours) so even it does not change the time. Last function that actually changed the time I believe was in DOS using time and date at the command prompt. I don't remember if those changed the actual BIOS time however.

You can do this programatically through interrupts and by interfering with a certain interrupt which I'll not name here, but why do this when you can just go into the CMOS and alter the BIOS time?

Also if you go changing the time in Windows it is possible that it will flag the year as a leap year and/or reset or unset daylight savings time. Of course this will change the minute you alter it to the current date and time.

I just fail to see any use for code that does this.
__________________
If you aim at everything you will hit something but you won't know what it is.

Last edited by Bubba; 01-02-2006 at 01:26 AM.
Bubba is offline   Reply With Quote
Old 01-02-2006, 05:24 AM   #9
Registered User
 
Join Date: Aug 2005
Posts: 1,265
Quote:
Why would you want to change the date and time of the machine you are working on?
Several reasons to do that -- mainly to snychronize the date/time with some other computer because the clocks on many computers are not all that accurate and to insure all the computers on a network are reporting the same date/time..
Ancient Dragon is offline   Reply With Quote
Old 01-02-2006, 07:11 AM   #10
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
Bubba, how about an NTP client?

Anyway, on Unix, changing the system time requires root privileges. Off the top of my head, the CRT seems to contain nothing that would require root privileges by default on some system.
__________________
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
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 10:15 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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