You can't override the OS's date and time functions. This worked back in the DOS/Win95/98 days, won't work in NT. Same goes for Linux.
All kinds of strange and unfortunate things would happen if you were able to this. Certificates would fail, Updates would fail, AV software would freak, chaos would ensue.
New to QB64pe? Visit the QB64 tutorial to get started. QB64 Tutorial
09-07-2024, 07:57 AM (This post was last modified: 09-07-2024, 07:59 AM by SMcNeill.)
Code: (Select All)
' some code to attempt to change the system date.
' returns error:
' a required privilege is not held by the client.
DefLng A-Z
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
Declare Dynamic Library "kernel32"
Function GetLocalTime& (lpSystemTime As SYSTEMTIME)
Function SetLocalTime& (lpSystemTime As SYSTEMTIME)
Function GetSystemTime& (lpSystemTime As SYSTEMTIME)
Function SetSystemTime& (lpSystemTime As SYSTEMTIME)
Function GetLastError& ()
Function FormatMessageA& (ByVal f As Long, f$, Byval e As Long, Byval d As Long, g$, Byval s As Long, h$)
End Declare
Dim Shared SysTime As SYSTEMTIME
Dim Shared ErrorBuffer As String * 260
Print "Current date: "; Date$
Print "Enter date(mm-dd-yyyy)";
Input z$
z$ = LTrim$(RTrim$(z$))
If Len(z$) Then
z = ValidDate(z$)
If z Then
' mm-dd-yyyy
x = GetLocalTime(SysTime)
SysTime.wMonth = Int(Val(Left$(z$, 2)))
SysTime.wDay = Int(Val(Mid$(z$, 4, 2)))
SysTime.wYear = Int(Val(Right$(z$, 4)))
x = SetLocalTime(SysTime)
If x = 0 Then
Print "Error Date reset: "; z$
Print DisplayWinError$(x)
Else
Print "Date reset to: "; z$
End If
Else
Print "Invalid date."
End If
End If
End
Function ValidDate (Var$)
' mm-dd-yyyy
Var$ = RTrim$(Var$)
If Len(Var$) <> 10 Then
ValidDate = False
Exit Function
End If
For Var = 1 To 10
V$ = Mid$(Var$, Var, 1)
Select Case Var
Case 1, 2, 4, 5, 7, 8, 9, 10
If V$ >= "0" And V$ <= "9" Then
Eat$ = ""
Else
ValidDate = False
Exit Function
End If
Case Else
If V$ <> "-" Then
ValidDate = False
Exit Function
End If
End Select
Next
M = Int(Val(Mid$(Var$, 1, 2)))
D = Int(Val(Mid$(Var$, 4, 2)))
Y = Int(Val(Mid$(Var$, 7, 4)))
If M >= 1 And M <= 12 Then
If D >= 1 And D <= 31 Then
If Y >= 1980 And Y <= 2079 Then
L = 0
If Y / 4 = Y \ 4 Then
L = -1
End If
If Y / 100 = Y \ 100 Then
L = 0
End If
If Y / 400 = Y \ 400 Then
L = -1
End If
Select Case M
Case 1, 3, 5, 7, 8, 10, 12
If D <= 31 Then
ValidDate = -1
Exit Function
End If
Case 4, 6, 9, 11
If D <= 30 Then
ValidDate = -1
Exit Function
End If
Case 2
If L Then
If D <= 29 Then
ValidDate = -1
Exit Function
End If
End If
If D <= 28 Then
ValidDate = -1
Exit Function
End If
End Select
End If
End If
End If
ValidDate = 0
End Function
' display windows error message
Function DisplayWinError$ (x)
' define error message value
v& = GetLastError
' call windows error message routine
x& = FormatMessageA&(&H1200, "", v&, 0, ErrorBuffer$, 260, "")
If x& Then
DisplayWinError$ = Left$(ErrorBuffer$, x& - 2)
Else
DisplayWinError$ = "Unknown error 0x" + Hex$(v&) + "."
End If
x = -1
End Function
Compile, Run as Admin. Lasts X seconds until system sync occurs on the net. Unplug your internet connection when trying it, I guess.
09-07-2024, 08:57 PM (This post was last modified: 09-07-2024, 09:02 PM by Pete.)
It's a temp thing these days, because MS control your life. So if you want to change the date and time about all it's good for is creating or saving a program with a manipulated date and time stamp. Oh, and of course time travel. Look, it's 1992 again!
Pete
- Win 3.1 Forever!
Edit: On another note of interest, why can't the boneheads who sync the time function get it right? The older a system becomes, the more minutes your system clock goes off. Mine is 10 minutes behind.
Shoot first and shoot people who ask questions, later.
09-07-2024, 11:01 PM (This post was last modified: 09-07-2024, 11:04 PM by SMcNeill.)
(09-07-2024, 08:57 PM)Pete Wrote: It's a temp thing these days, because MS control your life. So if you want to change the date and time about all it's good for is creating or saving a program with a manipulated date and time stamp. Oh, and of course time travel. Look, it's 1992 again!
Pete
- Win 3.1 Forever!
Edit: On another note of interest, why can't the boneheads who sync the time function get it right? The older a system becomes, the more minutes your system clock goes off. Mine is 10 minutes behind.
If you go into the registry, hunt for: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\W32Time\TimeProviders\NtpClient.
Right click on the SpecialPollInterval key, then click Modify.
That's the time -- in seconds -- between when syncing happens. You can set the date for 01-01-0001, and it won't matter. In XXXXX number of seconds, you'll sync and reset the time, or else a vast majority of windows based software won't work properly anymore.
If you want to disable time syncing, simply go into Firewall and Disable UDP port 123 -- that's the one which Network Time Protocol uses to connect and sync times with. (NTP Services)
So ...
1) block UDP port 123
2) compile my code above
3) run as admin
4) change your time
OR..
1) Change it via Windows, like most normal folks would.
And if looking at the above doesn't make folks realize WHY QB64 doesn't perform such actions, I dunno what to tell ya. Personally, I don't think I'd want a basic programming language, or a 1-line command, screwing with such deeply embedded policies and settings and such on my stuff.
Windows really cracks me up. So I go into the time settings. The sync has always been on, but get this. The last time the clock was synced was in October, 2019!
I'm really surprised the programmers didn't include a feedback loop so at reasonable time intervals the system checks its time with its time servers time when you go online, and sync itself, if necessary.