RealTimeClock.SetDateTime strange behavior (G400)

Windows 10
VS 2015
NetMF 4.3
SDK 2016 R1
FEZ Raptor G400
Firmware 4.3.8.0

using GHI.Processor;

...
string sDateFormat = "yyyy-MM-dd HH:mm:ss.fff";
DateTime tDateTime = DateTime.UtcNow;

Debug.Print("tDateTime " + 
            tDateTime.ToString(sDateFormat));
// tDateTime 2016-02-09 01:30:39.000
RealTimeClock.SetDateTime(tDateTime);
Debug.Print("RTC DateTime set to " + 
	    RealTimeClock.GetDateTime().ToString(sDateFormat));
// RTC DateTime set to 2016-01-30 01:30:39.001
// *** date is not updated ***
RealTimeClock.SetDateTime(new DateTime(tDateTime.Year, 
	  		tDateTime.Month, 
			tDateTime.Day, 
			tDateTime.Hour, 
			tDateTime.Minute, 
			tDateTime.Second));
Debug.Print("RTC DateTime set to " + 
	    RealTimeClock.GetDateTime().ToString(sDateFormat));
// RTC DateTime set to 2016-02-09 01:30:39.001
// *** date is updated ***
...

@ SouthernStars - What happens if you run the following:


using GHI.Processor;

string sDateFormat = "yyyy-MM-dd HH:mm:ss.fff";
DateTime tDateTime = DateTime.UtcNow;

Debug.Print("tDateTime " + tDateTime.ToString(sDateFormat));
RealTimeClock.SetDateTime(tDateTime);
Debug.Print("RTC DateTime set to " + RealTimeClock.GetDateTime().ToString(sDateFormat));
RealTimeClock.SetDateTime(tDateTime);
Debug.Print("RTC DateTime set to " + RealTimeClock.GetDateTime().ToString(sDateFormat));

int nAttempt;
DateTime tDateTime;

Debug.Print("RealTimeClock " + 
            RealTimeClock.GetDateTime().ToString("yyyy-MM-dd HH:mm:ss.ff"));
// RealTimeClock 2016-02-10 01:47:04.00
tDateTime = new DateTime(2016, 2, 1, 0, 0, 0);
nAttempt = 0;

do
{
	nAttempt++;
	RealTimeClock.SetDateTime(tDateTime);
	Debug.Print("RTC (" + nAttempt.ToString() + ") : " + 
                    RealTimeClock.GetDateTime().ToString("yyyy-MM-dd HH:mm:ss.ff"));
} while ((nAttempt < 10) && (RealTimeClock.GetDateTime().Day != 1));

// RTC (1) : 2016-02-10 00:00:00.00
// RTC (2) : 2016-02-01 00:00:00.00

It appears the time part is always set at the first attempt.
For the Date part, sometimes, although quite rarely, it may be set at the first attempt.

More tests to check if the Date is always set at least at the second attempt
or if sometimes more attempts are needed.

@ SouthernStars - Do you have other G400s you can test this on?

Yes, G400 Dev board, will give a try.

@ John
Same issue with the G400 Dev board.

Maybe you can use this for the time being : [url]https://www.ghielectronics.com/community/codeshare/entry/892[/url]

With the G400 I find I need to do the following when I set the RTC on the G400.


DateTime newDate = new DateTime(2000 + ClockTime.Year, ClockTime.Mon, ClockTime.Day, ClockTime.Hour, ClockTime.Min, ClockTime.Sec);

RealTimeClock.SetDateTime(newDate);
//
// Set NETMF time from RTC
//
Utility.SetLocalTime(RealTimeClock.GetDateTime());

With the last line I’ve found that the time would not be set within NETMF and I need to call this on power up too.

@ Dave McLaughlin -
From the examples I provide, he issue is with
RealTimeClock.SetDateTime(newDate);
not with
Utility.SetLocalTime(RealTimeClock.GetDateTime());

Duration for RealTimeClock.GetDateTime is less than 70 us (0.07 ms) and
the granularity is 1 second.
OTOH, RealTimeClock.SetDateTime is variable, and between 1 and 2 seconds

In void ProgramStarted(), the granularity is solved with


private const string SFORMATRTC = "yyyy-MM-dd HH:mm:ss.f";
private const string SFORMATSYS = "yyyy-MM-dd HH:mm:ss.fff";
...
private int m_nminTimeZone;                         // offset from UTC in minutes
private DateTime m_tStart;
...
int nCount;
int nSecond;
m_nminTimeZone = 0;
//
// synchronize NetMF clock
//
Debug.Print("RealTimeClock " + RealTimeClock.GetDateTime().ToString(SFORMATRTC));
//
// wait for second change in RTC
//
nSecond = RealTimeClock.GetDateTime().Second;
nCount = 0;

while (RealTimeClock.GetDateTime().Second == nSecond)
{
	nCount++;
	Thread.Sleep(1);
}

Debug.Print("SYS UTC before     " + DateTime.UtcNow.ToString(SFORMATSYS));
Utility.SetLocalTime(RealTimeClock.GetDateTime().AddMinutes(m_nminTimeZone));
m_tStart = DateTime.UtcNow;
Debug.Print("nCount = " + nCount.ToString());
Debug.Print("Program Started at " + m_tStart.ToString(SFORMATSYS));

RealTimeClock 2016-02-29 00:00:00.0
SYS UTC before 2011-06-01 09:07:04.597
nCount = 726
Program Started at 2016-02-29 00:00:01.001

@ SouthernStars - Sorry for the delay. Unfortunately, I was not able to reproduce this issue using your code in the third post. I tried on several G400s numerous times and each time the date was set properly on the first attempt. If the loop method eventually sets the date for you, I would use that as a work around until we can reproduce it.