Android Question Time is not stable and not accurate as well !!!

Alhootti

Active Member
I'm using an app in my Tv Box working in Android platform.
the issue is each day i get +4 seconds extra.
even i switch off it then switch on it the same issue.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If (FirstTime) Then
        PWS.KeepAlive(True) ' Screen on always
        Timer1.Initialize("tmr",1000)
        Timer1.Enabled = True
    End If
    Activity.LoadLayout("Layout")
    InitCanvas
    InitLabel
    End Sub
 
    Sub tmr_tick
    DateTime.TimeFormat = "HH:mm:ss"
    Label1.Text = DateTime.Time(DateTime.Now)
    End Sub

Note : I don't want to sync it with internet (WiFi)
How i can solve this issue ?
 
Last edited:

Brian Dean

Well-Known Member
Licensed User
Longtime User
If you want your TV to keep time with atomic clock accuracy then it will need access to a UTC time source. That will mean either a GPS signal (unlikely in a TV) or access to the internet. Without this it will have to rely on its internal crystal oscillator which, apparently, is out by four seconds a day - 0.00463% accurate - not great but not unusual.
 
Upvote 0

Alhootti

Active Member
Thanks Brain
that mean there is noway with code totally.
The solution either with GPS or WiFi connection.
i just want to recap all that.
do you propose for me the most accurate internal crystal oscillator type i will change it to get rid of this issue.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Is it possible to set the system time by code?
Every 6 hours (at 6, 12, 18 and 00) set the system time decreasing the actual one by 1 second.
Of course this would be a quite weird patch.
 
Upvote 0

emexes

Expert
Licensed User
Of course this would be a quite weird patch.

From memory, something akin to this is done in Android Arduino, like to convert a 1024 Hz tick to 1000 Hz.

If it is not possible to programmatically nudge the system time back one second four times a day, then instead keep track of the current offset between system time and "television time", and increment decrement that by one second four times a day (or by one millisecond four thousand times a day?).
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Or maybe setting the Timer Tick to (just as an example) 998 instead of 1000.
It would be needed to make some calculation to determine the right value.
 
Upvote 0

Alhootti

Active Member
Or maybe setting the Timer Tick to (just as an example) 998 instead of 1000.
It would be needed to make some calculation to determine the right value.
1000x60x24 = 1440000
1440000 - 4x1000 = 1436000
Accurate Tick = 1436000 / (24/60)
Accurate Tick = 997.2
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The timer will fire (usually) at rhe set time, but, there is a time difference between the event fire and the ui update.
The later will only be done when the OS has the time for it (I'm not a pro dev so I don't know or really care about mnemonics)
The best would be to, upon timer event fire, save the ticks to a variable and the use that for calculating the offset needed.
The ticks value is, as far as I know, comes from an (hardware based?) ndependent timer, so nothing the OS does, will make it offset
 
Upvote 0

Alhootti

Active Member
The best would be to, upon timer event fire, save the ticks to a variable and the use that for calculating the offset needed.
The ticks value is, as far as I know, comes from an (hardware based?) ndependent timer, so nothing the OS does, will make it offset
could you please write the code here
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Sorry, no coding for 2 weeks.
It's summer vacation time!
 
Upvote 0

emexes

Expert
Licensed User
1000x60x24 = 1440000
1440000 - 4x1000 = 1436000
Accurate Tick = 1436000 / (24/60)
Accurate Tick = 997.2

Ticks per day = 1000x60x60x24 = 86400000 (not 1440000)

but the rest of the calculation looks correct.

Personally I would have two calibration numbers:

1/ the time (ticks) at which the system clock was last correct
2/ the ticks per day to add (or negative to subtract) eg if system clock 4 seconds fast per day, then -4000

and then:

B4X:
'LastCorrectDateTimeNow = the DataTime.Now at which the system clock was set to correct time
'AddTicksPerDay = -4000 if system clock is 4 seconds fast per day

Dim HoldDateTimeNow As Long = DateTime.Now    'so that one same value is used in next calculation
Dim CorrectDateTimeNow As Long = HoldDateTimeNow + AddTicksPerDay * (HoldDateTimeNow - LastCorrectDateTimeNow) / 86400000
 
Upvote 0

Alhootti

Active Member
This is the result :
B4X:
Dim AddTicksPerDay As Long = -4000  ' if system clock is 4 seconds fast per day
Dim HoldDateTimeNow As Long = DateTime.Now    'so that one same value is used in next calculation
Dim CorrectDateTimeNow As Long = HoldDateTimeNow + AddTicksPerDay * (HoldDateTimeNow - DateTime.Now) / 86400000
Log(CorrectDateTimeNow)
B4X:
1723380651466
1723380652469
1723380653469
1723380654475
1723380655475
1723380656491
1723380657506
1723380658523
1723380659523
1723380660539
1723380661554
1723380662555
1723380663556
1723380664571
1723380665586
1723380666587
1723380667604
1723380668620
1723380669634
1723380670650
1723380671652
1723380672665
1723380673667
1723380674668
1723380675669
1723380676684
1723380677686
1723380678686
1723380679700
1723380680719
1723380681719
1723380682734
1723380683750
1723380684751
1723380685753
1723380686767
1723380687767
1723380688773
1723380689772
1723380690789
1723380691791
1723380692790
1723380693807
1723380694821
1723380695822
 
Last edited:
Upvote 0

Alhootti

Active Member
After modification :
B4X:
Sub Process_Globals
    Dim LastCorrectDateTimeNow As Long
    End Sub

Sub tmr_tick
    Dim AddTicksPerDay As Long = -4000  ' if system clock is 4 seconds fast per day
    Dim HoldDateTimeNow As Long = DateTime.Now    'so that one same value is used in next calculation
    Dim CorrectDateTimeNow As Long = HoldDateTimeNow + AddTicksPerDay * (HoldDateTimeNow - LastCorrectDateTimeNow) / 86400000
    Label1.Text = DateTime.Time(CorrectDateTimeNow)
    LastCorrectDateTimeNow = DateTime.Now
     End Sub
 
Upvote 0

emexes

Expert
Licensed User
Example in B4J console app, but should work in B4A too.

Assume system clock was last set to correct time on New Year's Day 2024 just after noon, and that system clock is running 4 seconds further ahead each day.

From New Year's Day until now (middle of August) = 7 full months plus half of August = 7.5 * 30 days/month = 225 days. Multiply by 4 seconds per day = approximately 900 seconds ahead, ie need to deduct that from the system clock time. Approximately every 20 seconds (4000 times per day) this deduction increases by one millisecond.

B4X:
Sub Process_Globals
    Dim LastCorrectDateTimeNow As Long
    Dim AddTicksPerDay As Int
    Dim ExampleTimer As Timer
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")

    DateTime.DateFormat = "MM/dd/yyyy"
    DateTime.timeformat = "HH:mm:ss"
   
    LastCorrectDateTimeNow = DateTime.DateTimeParse("01/01/2024", "12:34:56")

    Log(DateTime.Date(LastCorrectDateTimeNow) & " " & DateTime.Time(LastCorrectDateTimeNow))

    AddTicksPerDay = -4000
   
    ExampleTimer.Initialize("ExampleTimer", 6000)
    ExampleTimer.Enabled = True
   
    StartMessageLoop    'needed in B4J to stop program from terminating early
End Sub

Sub CorrectTime(ClockTime As Long) As Long
    Return ClockTime + AddTicksPerDay * (ClockTime - LastCorrectDateTimeNow) / 86400000
End Sub

Sub ExampleTimer_Tick
    Dim TempClockTime As Long = DateTime.Now
    Dim TempCorrectTime As Long = CorrectTime(TempClockTime)
   
    Dim Adjustment As Long = TempCorrectTime - TempClockTime
   
    Log(                                       _
        DateTime.Time(TempClockTime) & TAB &   _
        DateTime.Time(TempCorrectTime) & TAB & _
        NumberFormat(Adjustment / 1000, 1, 3)  _
    )
End Sub
Log Output:
Waiting for debugger to connect...
Program started.
Hello world!!!
01/01/2024 12:34:56
11:59:35    11:44:39    -896.069
11:59:41    11:44:45    -896.069
11:59:47    11:44:51    -896.07
11:59:53    11:44:57    -896.07
11:59:59    11:45:03    -896.07
12:00:05    11:45:09    -896.07
12:00:11    11:45:15    -896.071
12:00:17    11:45:21    -896.071
12:00:23    11:45:27    -896.071
12:00:29    11:45:33    -896.072
12:00:35    11:45:39    -896.072
12:00:41    11:45:45    -896.072
 
Upvote 0

emexes

Expert
Licensed User
The deference between TempClockTime and TempCorrectTime = 15 minutes to much.
it should be in second.

The example only got to 15 minutes out because the last example time that the Android system clock was set to correct time was 7.5 months ago ie New Year's Day 2024.

For testing, using an example value, you can choose any example time that the Android system clock was set to correct time that you wish.

If you were to set the Android system clock to correct time monthly, then it would gradually creep up to being 2 minutes out before you next reset the Android system clock back to correct time the following month.

If you were to set the Android system clock to correct time weekly (eg, every Monday morning), then it would gradually creep up to being 28 seconds out before you next reset the Android system clock back to correct time the following week.
 
Upvote 1
Top