Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

ehs5 RTC & System.currentTimeMillis() problem | Telit Cinterion IoT Developer Community

December 11, 2016 - 4:23pm, 3289 views

Hi,

This problem was at a forum, but adequately not answered.

https://iot-developer.thalesgroup.com/threads/real-time-clock-drafts-ehs6

Test program

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class probe extends MIDlet {
	
	int i = 0;
	boolean tick = false; 
	
	public probe() 
	{

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		notifyDestroyed();

	}

	protected void pauseApp() 
	{
	
	}

	protected void startApp() throws MIDletStateChangeException
	{
		TimerTask tmrtsk = new TimerTask() {
			
			public void run() {
				tick = true;
			}
		};
		
		Timer tmr = new Timer();
		tmr.schedule(tmrtsk, 5000, 5000);
		
		System.out.println("Start Step 1. ---------------------------------------");
		
		while (i < 24)
		{
			tick = false;
			while (!tick) ;
			System.out.println("Step 1. Tick=" + i +"   Time=" + new Date().toString());
			
			i++;
		}

		i = 0;
		
		long st= System.currentTimeMillis();

		System.out.println("Start Step 2. ---------------------------------------");

		while (i < 60)
		{
			while (System.currentTimeMillis() - st < 5000) ;
			
			st = System.currentTimeMillis();
			
			System.out.println("Step 2. Tick=" + i +"   Time=" + new Date().toString());
			
			i++;
		}
		
		i = 0;
		
		System.out.println("Start Step 3. ---------------------------------------");
		
		while (i < 24)
		{
			tick = false;
			while (!tick) ;
			System.out.println("Step 3. Tick=" + i +"   Time=" + new Date().toString());
			
			i++;
		}
		
		System.out.println("Test completed.");
		
		destroyApp(true);
	}

}
 

Result

<<11.12.2016 17:14:39:  Start Step 1. ---------------------------------------
<<11.12.2016 17:14:44:  Step 1. Tick=0   Time=Sun Dec 11 17:14:44 GMT+00:00 2016
........
<<11.12.2016 17:16:40:  Step 1. Tick=23   Time=Sun Dec 11 17:16:40 GMT+00:00 2016
Start Step 2. ---------------------------------------
<<11.12.2016 17:16:45:  Step 2. Tick=0   Time=Sun Dec 11 17:16:45 GMT+00:00 2016
<<11.12.2016 17:16:50:  Step 2. Tick=1   Time=Sun Dec 11 17:16:50 GMT+00:00 2016
........
<<11.12.2016 17:21:12:  Step 2. Tick=58   Time=Sun Dec 11 17:21:52 GMT+00:00 2016
<<11.12.2016 17:21:15:  Step 2. Tick=59   Time=Sun Dec 11 17:21:57 GMT+00:00 2016
Start Step 3. ---------------------------------------
<<11.12.2016 17:21:16:  Step 3. Tick=0   Time=Sun Dec 11 17:21:57 GMT+00:00 2016
..............
<<11.12.2016 17:23:11:  Step 3. Tick=23   Time=Sun Dec 11 17:23:53 GMT+00:00 2016

Test completed.

The test shows: Frequent reading system time by a call of System.currentTimeMillis() leads to shift of real time clock .   The shift has made 42 seconds in 5 minutes!    (Seconds, not milliseconds)

This test works at the TC65T terminal without changes of real time clock.

How it is possible to solve this problem?