Read and set\change system time

This is a discussion on Read and set\change system time within the C++ Programming forums, part of the General Programming Boards category; I want to be able to set the time on my comp, (saving the time it was before setting would ...

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

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Time is still supported in MSVC, but I'm not sure about 2005.

    From MSVC 6
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    ok cool, I belive the msdn fuction should help me on my way...

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 01-02-2006 at 01:26 AM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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..

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

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