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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time change 1

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
Hi,
I coded one small VB6 program - it performs certain operations based on the current time:

If (Hour(Time) >= 0 And Hour(Time) <= 6) Then
...
End If

It works fine, but I need either to prevent user to change the time manually or if this happen change it back or work with the "original" time.

Any suggestion please?
 
Set a network policy that prevents the users from being able to change their clocks. Sync all clocks to a central server.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
well, the program runs on stand alone computer (Windows XP, one general user, no login screen)
 
My question to you is "How do you know the user has already changed the time when the program is first executed?". You're obviously testing for a time between Midnight and 7am (6:59). VB has a Timer event you could use to set up your own clock once the program is started, but if the user has already changed the time and it's wrong, then how will you know when it's midnight etc? (You won't) Hmmm...I think we need to know a little more about your application and the reason for testing the time. As previously suggested, the best bet is to prevent the user from changing the time using a policy setting. (In a business environment each user should have their own login anyway.)
Regards,
Walter
 
If your stand alone computer has access to the internet, you could use something like DSClock which uses atomic time servers to keep your PC time accurate.
[gray]Experience is something you don't get until just after you need it.[/gray]
 
How do you know the user has already changed the time when the program is first executed?"
The program starts as Windows boots up. Let's assume the time at start is correct.
Meantime, I installed Li`l Atomic Clock It helps, but I would rather to have "time control" included in the program.
 
So as not to overlook the obvious, maybe it's just as simple as calling the Hour(Time) functions once upon program initialization and storing the result in a variable. All subsequent tests can then be made against the variable, rather than the repeated tests of Time.
 
It would seem to me that your only options are to remove the permissions to change the clock from every login except Admin and the Lil Atomic Clock program. If the time changes while the program is not running, you have no way of really knowing when it was the Atomic Clock or another User that made the change. Additionally there is no way to tell if the clock has been changed unless you poll another source and compare it to that source. Also, keep in mind that when you poll another source, the two computers will "Drift" apart over time, and depending on how much traffic is on the net, can delay reporting back. In turn, there will almost always be about a one second or more gap and the two times will almost never truly match.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Does your application use a database stored on the server? If so (and depending on the type of database), it may be easy to get the server's time. In my experience, it's relatively easy to change your local time, but people are a lot less willing to change the time on the server.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
<you have no way of really knowing when it was the Atomic Clock or another User that made the change>
DSClock writes to a log file whenever it adjusts the PC clock.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Postharebrain (MIS)
<So as not to overlook the obvious, maybe it's just as simple as calling the Hour(Time) functions once upon program initialization and storing the result in a variable. All subsequent tests can then be made against the variable, rather than the repeated tests of Time.>
I was thinking about this option but I don't know how to make/keep this "internal time" running and check time for example every 10 minutes.
 
Hi ttuser4,
As I advised in my previous post VB has a Timer control. You can use this control to keep track of time manually.
Here is a simple example. Create a new project. Put a label on the form (and call it Label1). Also add a Timer Control (Timer1). Change the Interval of the Timer control to 1000. Now add the code below to the form:

Option Explicit

Dim intHour As Integer

Private Sub Form_Load()
intHour = Hour(Time)
Label1 = "The Hour is now " & CStr(intHour)
End Sub

Private Sub Timer_Timer()
Static lngSecs As Long
lngSecs = lngSecs + 1
If lngSecs >= 3600 Then
lngSecs = 0
intHour = intHour + 1
Label1 = "The Hour is now " & CStr(intHour)
End If
End Sub

To see this in operation, change the "3600" in the Timer event to "2" (without the quotes) and run the project. You'll see the "Hour" change every 2 seconds. By changing the "2" back to "3600" the hour will change every hour (3600 seconds).

HTH
Walter
 
well, this program doesn't have any form, it's just module so I cannot use Timer control.
 
Sure you can. Just declare a variable as the Timer. All you need to do is make sure you have a Reference define in the project for it. It'll open up the control to use.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
thanks, i didn't know that, my knowledge of VB is quite limited :-(
 
And ousoonjerie has unfortunately not really expanded your knowledge of VB in a meaningful way for this; sure, you can declare a variable as a Timer without a form as ousoonjerie suggests - but, unless you declare it in a class module (or in a form, which is is just a class module with a built-in GUI) you won't be able to receive any events from the Timer control.

But consider this - a formless program can have a form, it just doesn't need to show it ...

Or you could consider the SetTimer API. A number of examples of how to more-or-less duplicate the core functionaolity of the Timer control using this API in a bas module can be found by doing a keyword search of this forum
 
Or, given that your usage of L'il Atomic CLock confirms that you have an internet connection available and that you "would rather to have 'time control' included in the program", how about this NTPless way of looking up the current local time for your timezone. Do your check against this looked up time rather than against whatever time the PC is set to. The following just needs to be dropped into a bas module:code][blue]Option Explicit

Public Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION, lpUniversalTime As SYSTEMTIME, lpLocalTime As SYSTEMTIME) As Long
Public Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As Long

Public Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Public Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type

' NTPless way of getting UTC as a local time. There will be slight inaccuracy
Public Function GetInternetUTC() As Date
Dim wombat As TIME_ZONE_INFORMATION
Dim UTC As SYSTEMTIME
Dim LocalTime As SYSTEMTIME
Dim result As String
Dim dresult As Date



' Get UTC from the US Navy ...
result = Split(Form1.Inet1.OpenURL(" "<BR>")(1)
dresult = Left(Replace(Replace(result, ",", ""), ".", ""), Len(result) - 6)

' Now convert UTC into local time for user's timezone
GetTimeZoneInformation wombat
With UTC
.wHour = CLng(Hour(dresult))
.wMinute = CLng(Minute(dresult))
.wSecond = CLng(Second(dresult))
.wDay = CLng(Day(dresult))
.wMonth = CLng(Month(dresult))
.wYear = CLng(Year(dresult))
End With

SystemTimeToTzSpecificLocalTime wombat, UTC, LocalTime
GetInternetUTC = CDate(LocalTime.wHour & ":" & LocalTime.wMinute & ":" & LocalTime.wSecond & " " & LocalTime.wDay & " " & LocalTime.wMonth & " " & LocalTime.wYear)
End Function[/blue][/code]
 
fyi - i used strongm's code in my program and it works just fine. i had to add a form, but it isn't visible and i am periodically testing time from that web site and do other task accordingly.
thank for your input everybody!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top