I’m using the RTC widget in my project. The time is being synchronised with the Blynk server, formatted by strftime and printed as under:
tm * ts;
void requestTime() {
Blynk.sendInternal("rtc", "sync");
}
BLYNK_WRITE(InternalPinRTC) {
long t = param.asLong();
setTime(t);
Serial.print("\nUnix time: ");
Serial.print(t);
Serial.println();
rawtime=t;
char daystr[3]="";
char timestr[6]="";
char yearstr[5] = "";
if (IsFirstConnect)
{
FirstConnectTime=t;
IsFirstConnect=0;
Serial.print("First Connect: ");
Serial.print(t);
Serial.print(" :: ");
/* Needs <time.h> to be included. Can give out formatted time.*/
ts = localtime ( &rawtime );
strftime (time_stamp,80,"%a %I:%M %p %d %b %G %Z.",ts);
Serial.println(time_stamp);
firstconnect_ts=time_stamp;
}
}
The time reported by now() is in sync with the phone clock. The time stamp returns GMT as the time zone while the widget has Asia\Kolkatta as the time zone. By this, the strftime should show IST and not GMT.
It should say 8.18pm IST. If the strftime arg is %z, it gives the time offset and %Z should give the 3 char equivalent. For India, it is IST. IST = GMT +0530
Could we be sure? The RTC sync call is a Blynk call and not any other. So also is the widget. I suspect the regular time libraries would’ve been resolved of such errors before roll out.
@mohan_sundaram can I ask you to provide the same sketch but without any Blynk code i.e. using a time server and paste Serial Monitor showing IST (without any hacks at your side).
Will try. I’m not sure how well I would do this though.
Indulge me for a while please as I’m thinking aloud here. The blynk.sendinternal returns epoch time. The epoch time is converted to struct tm by localtime and then formatted for print by strftime. So, localtime or breakTime is what generates the tz. We provide the tz in the RTC widget. How does Blynk pass this on to the call so that the appropriate tz is set and the current time calculated?
If we have to set the tz ourselves in the sketch, them Blynk should give us a variable or argument to read from, based on the widget setting. Is my surmise wrong?
Edit added: This whole thing is wrong if the Blynk server returns epoch time adjusted for the timezone set in the widget. This possibly could be the most likely case. There is no other way the correct time can be returned without the timezone explicitly being set. Will check this by comparing the serial output epoch with epoch shown by sites at the same time.
@Costas. Rebooted my board and checked the epoch time on the internet at the same time. There is a difference of about 19,700… Leads me to believe that the number returned by the blynk server on rtc sync is already adjusted for the time zone. This is ok, now that I know it. However, the device should know the timezone too in some form from the blynk server.
I’ve used the Blynk.sendInternal() call to get the RTC epoch number from the server I’m unable to see the dcoumentation for this call in the docs. It is possible that I’ve not looked well enough. Seems like a call to get some server resident settings. Would like to know what else one can do with this call. I saw this in the advanced RTC sketch and did a copy-paste…
Based on the sketch in question, Blynk.sendInternal("rtc", "sync"); is simply a call that synchronises the RTC widget with the server time and calls this function to get the UI time…
BLYNK_WRITE(InternalPinRTC) {
long UnixTime = param.asLong();
}
Probably useful in advanced cases where you have a sketch that needs the full UI time instead of real world breakdown of hours, minutes, seconds, etc.
Searching through the Blynk library files shows that this command is only used with the RTC. As per WidgetRTC.h It is an internal command called from the Time Library variable type time_t AKA Unix Time