Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set System Date/Time

Status
Not open for further replies.

tvign

Programmer
May 16, 2003
50
US
Does anyone know how to set the system (computer) date and time? I'm using VFP 6.0

Tony
 
Tony,
Yes, but it's not simple.
Code:
*SETTIME.PRG
LPARAMETERS tod_year, tod_month, tod_day, tod_Mins, tod_Hours, tod_Secs
Local lpTimeSet

lpTimeSet = "" ;
+ word2str(tod_year);
+ word2str(tod_month);
+ word2str(1);
+ word2str(tod_day);
+ word2str(tod_Hours);
+ word2str(tod_Mins);
+ word2str(tod_Secs);
+ word2str(0)

Declare INTEGER SetLocalTime in kernel32 STRING
l_nRetVal = SetLocalTime(lpTimeSet)

IF l_nRetVal = 0 && Error
  DECLARE INTEGER GetLastError in Kernel32
  l_nLastError = GetLastError()
  **? l_nLastError
  Declare Integer FormatMessage in kernel32 Long, Long, Long, Long, String, Long, Long
  #DEFINE FORMAT_MESSAGE_FROM_SYSTEM 0x1000
  #DEFINE LANG_NEUTRAL 0x0

  l_cBuffer = replicate(chr(0), 201)
  = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, l_nLastError, LANG_NEUTRAL, @l_cBuffer, 200, 0)
  l_cBuffer = ALLTRIM(STRTRAN(l_cBuffer, chr(0),""))
  messagebox(l_cBuffer, 0, "Please Note")
  RETURN .F.
ENDIF

* Use SendMessage to tell everybody that we've changed the system time.
DECLARE INTEGER SendMessage IN win32api ;
INTEGER WindowHandle, ;
INTEGER MESSAGE, ;
STRING Param1, ;
STRING Param2

* SendMessage constants.
#DEFINE HWND_BROADCAST 65535
#DEFINE WM_TIMECHANGE 30

* Send the message that the time has changed.
= SendMessage(HWND_BROADCAST, WM_TIMECHANGE, "", "")

**? "New Local: ", datetime()

RETURN .T.

*************************************************************
FUNCTION word2str
*************************************************************
* passed: integer value
* returns: 2-byte character string (m.longstr) in low-high ASCII format
* example:
* m.wordval = 111
* m.wordstr = word2str(m.wordval)

PARAMETERS m.wordval

PRIVATE i, m.retval

m.retval = ""
FOR i = 0 to 1
m.retval = m.retval + CHR(m.wordval % 256)
m.wordval = int(wordval / 256)
NEXT
RETURN m.retval

*!* EOP: SETTIME.PRG
Rick
 
Thanks, Rick.

It works fine. And the code is not so tough - just a lot of reformating of data for the API function.

Tony
 
Rick,

I tested SetTime and found peculiar results. For a set of files, I need to alter the time stamp by 3 hours. My method is to get each file's date/time, subtract 3 hours, set the system's clock to the new date and time, then rewrite the file. When finished, restore the original time.

Here is the test, without rewriting any file. The resulting dates are not all 3 hours earlier - some are 2 and some are 4. (Check you clock after the run!)

Any clue?
I running with Win XP (SP2) at 1.5GHz and 512MB.

Tony


*PROGRAM TestSetTime

local yr,mo,da,hr,mn,sc
local i,fi,d,dat,dnow
adir(flist,"*.PRG")

set hours to 24
dnow = datetime() && Save Now

clear
for i=1 to min(30,alen(flist,1))
fi = flist(i,1)
d = fdate(fi,1)
yr = year(d)
mo = month(d)
da = day(d)
hr = hour(d)
mn = minute(d)
sc = sec(d)
?d," "
hr = hr-3
if hr < 0 then
hr = hr+24
dat = date(yr,mo,da)-1
yr = year(dat)
mo = month(dat)
da = day(dat)
endif
settime(yr,mo,da,mn,hr,sc)
??datetime()
next i

set hours to 12
yr = year(dnow)
mo = month(dnow)
da = day(dnow)
hr = hour(dnow)
mn = minute(dnow)
sc = sec(dnow)
settime(yr,mo,da,mn,hr,sc) && Restore Now
return .T.
*======================
 
Tony,
If you want to change a file date, checkout FoxTouch() in the FoxTools.FLL. The only thing important to note is that the date and time values required are GMT (UTC) based. Using FoxTouch() you don't have to fool with your system time!

If you don't want to figure out the current conversions, Dan Freeman posted some code on the UT( in the Downloads area that does it all for you.

Rick
 
Thanks Rick.

This seems to be a much better method for accomplishing what I need to do.

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top