DateTime.DateFormat = "dd/MM/yyyy"
Log(DateTime.GetTimeZoneOffsetAt(DateTime.DateParse("04/03/2020")))
Log(DateTime.GetTimeZoneOffsetAt(DateTime.DateParse("01/04/2020")))
I am in the USA.
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim prevTime As Long
Dim currTime As Long
Dim tmr As Timer
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("main") 'Load the layout file.
tmr.Initialize("tmrTimeCheck",1000*60*60)
tmr.Enabled=True
End Sub
Sub tmrTimeCheck_Tick
currTime=DateTime.Now
If Floor(Abs(currTime-prevTime)/DateTime.TicksPerHour)<>1 Then
Log("time changed")
End If
prevTime=currTime
End Sub
yes. something like this
B4X:Sub Process_Globals Private fx As JFX Private MainForm As Form Dim prevTime As Long Dim currTime As Long Dim tmr As Timer End Sub Sub AppStart (Form1 As Form, Args() As String) MainForm = Form1 MainForm.RootPane.LoadLayout("main") 'Load the layout file. tmr.Initialize("tmrTimeCheck",1000*60*60) tmr.Enabled=True End Sub Sub tmrTimeCheck_Tick currTime=DateTime.Now If Floor(Abs(currTime-prevTime)/DateTime.TicksPerHour)<>1 Then Log("time changed") End If prevTime=currTime End Sub
#if java
import java.util.*;
import java.time.ZoneId;
import java.time.zone.ZoneOffsetTransitionRule;
public static void DST(){
ZoneId zid = ZoneId.systemDefault();
// 0 is start 1 is end
ZoneOffsetTransitionRule DSTstarts = zid.getRules().getTransitionRules().get(0);
ZoneOffsetTransitionRule DSTends = zid.getRules().getTransitionRules().get(1);
// just to see the rules for current year
//System.out.println(DSTstarts);
//System.out.println(DSTends);
// pretty print the info
System.out.println("Daylight saving starts on "+ DSTstarts.getDayOfWeek() + " " + DSTstarts.getDayOfMonthIndicator() +
" " + DSTstarts.getMonth() + " at " + DSTstarts.getLocalTime() +" clocks goes forward to "+ DSTstarts.getLocalTime().plusHours(1));
System.out.println("Daylight saving ends on "+ DSTends.getDayOfWeek() + " " +DSTends.getDayOfMonthIndicator() +
" " + DSTends.getMonth() + " at " + DSTends.getLocalTime().plusHours(1) +" clocks goes back to "+ DSTends.getLocalTime());
}
#End If
So i tried this, and it has a small problem. This code keeps triggering as time changed, but I also have the code set to check every 10 seconds instead of 1 hour.
Sub CheckForTimeZoneChanges
Dim CurrentTimeZone As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
Do While True
Dim c As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
If c <> CurrentTimeZone Then
TimeZoneChanged
CurrentTimeZone = c
End If
Sleep(1000)
Loop
End Sub
Sub TimeZoneChanged
'do whatever you need
End Sub
Java knows the time and date of daylight saving changes in advance, so having a timer running all the time to check seems (to me) a bit wasteful.
You could at the start of your program simply create a task on the ScheduledExecutorService that fires when a change occurs and then update the time on the external device.
Just a thought.
code to see the dates & times
B4X:#if java import java.util.*; import java.time.ZoneId; import java.time.zone.ZoneOffsetTransitionRule; public static void DST(){ ZoneId zid = ZoneId.systemDefault(); // 0 is start 1 is end ZoneOffsetTransitionRule DSTstarts = zid.getRules().getTransitionRules().get(0); ZoneOffsetTransitionRule DSTends = zid.getRules().getTransitionRules().get(1); // just to see the rules for current year //System.out.println(DSTstarts); //System.out.println(DSTends); // pretty print the info System.out.println("Daylight saving starts on "+ DSTstarts.getDayOfWeek() + " " + DSTstarts.getDayOfMonthIndicator() + " " + DSTstarts.getMonth() + " at " + DSTstarts.getLocalTime() +" clocks goes forward to "+ DSTstarts.getLocalTime().plusHours(1)); System.out.println("Daylight saving ends on "+ DSTends.getDayOfWeek() + " " +DSTends.getDayOfMonthIndicator() + " " + DSTends.getMonth() + " at " + DSTends.getLocalTime().plusHours(1) +" clocks goes back to "+ DSTends.getLocalTime()); } #End If
Call CheckForTimeZoneChanged when your app starts:
B4X:Sub CheckForTimeZoneChanges Dim CurrentTimeZone As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now) Do While True Dim c As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now) If c <> CurrentTimeZone Then TimeZoneChanged CurrentTimeZone = c End If Sleep(1000) Loop End Sub Sub TimeZoneChanged 'do whatever you need End Sub
Sub CheckForTimeZoneChanges
Dim CurrentTimeZone As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
Do While True
Dim c As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
If c <> CurrentTimeZone Then
Log("New time zone: " & c & ", Previous time zone: " & CurrentTimeZone)
TimeZoneChanged
CurrentTimeZone = c
End If
Sleep(1000)
Loop
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?