Deal all,
I found program where it converting UTC to Local. But my time is in local time . i wanted to convert to UTC time and then write to RTC. At present it take time utc time convert it to local time . Now my out put will be in local time. Suppose i wanna edit date and time . my time will be in local so i need to convert it back to UTC to write . how can i do it
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
static int MNTH_DAYS[12]={
31,28,31,30,31,30,31,31,30,31,30,31};
float tzone=5.5;
static long tzone_offset_s = tzone*3600UL;
static int dd;
static int mm;
static int yy;
static int s;
static int h;
static int m;
static int wkDay;
int CENTURY=2000;
static int local_day;
static int local_month;
static int local_year;
int local_s;
static int local_h;
static int local_m;
void setup(){
Wire.begin();
Serial.begin(9600);
s=10;
m=9;
h=5;
yy=14;
mm=3;
dd=15;
wkDay;
rtcWrite();
}
void loop(){
getRTCDateTime();
convertUTCtoLocal();
Print_Date_time();
delay(1000);
}
void rtcWrite(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(s));
Wire.write(decToBcd(m));
Wire.write(decToBcd(h));
Wire.write(decToBcd(wkDay));
Wire.write(decToBcd(dd));
Wire.write(decToBcd(mm));
Wire.write(decToBcd(getLastTwoDigOfYear(yy)));
Wire.write(zero); //start
Wire.endTransmission();
}
int getLastTwoDigOfYear(int y){
return(y%1000);
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void getRTCDateTime()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
s = bcdToDec(Wire.read());
m = bcdToDec(Wire.read());
h = bcdToDec(Wire.read() & 0b111111); //24 hour time
wkDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
dd = bcdToDec(Wire.read());
mm = bcdToDec(Wire.read());
yy = CENTURY + bcdToDec(Wire.read());
}
void convertUTCtoLocal(){
long no_s_day_start= h*3600UL + m*60 + s; //no of seconds since day start
long day_change_cutoff_s = 86400UL-tzone_offset_s;//max utc before changing date
long additional_s;//additional seconds after day change
local_year = yy;
if(no_s_day_start>day_change_cutoff_s){
if(dd+1 > MNTH_DAYS[mm-1]){
local_day = 1;
if(mm==12){
local_month=1;
local_year = yy+1;
}
else
local_month = mm+1;
}
else{
local_day=dd+1;
local_month=mm;
}
additional_s = no_s_day_start - day_change_cutoff_s;
convertSecondsToLocalHMS(additional_s);
}
else{
local_day = dd;
local_month = mm;
convertSecondsToLocalHMS(no_s_day_start+tzone_offset_s);
}
}
void convertSecondsToLocalHMS(long secs){
local_h = secs/3600; //hour
local_m=(secs%3600)/60;//min
local_s = secs - local_h*3600UL - local_m*60;
}
int sign(int v)
{
return v >= 0 ? 1 : -1;
}
void Print_Date_time(){
Serial.print("Local Date:");
Serial.print(local_day);
Serial.print("/");
Serial.print(local_month);
Serial.print("/");
Serial.print(local_year);
Serial.print("\t");
Serial.print("local_time is:");
Serial.print(local_h);
Serial.print("-");
Serial.print(local_m);
Serial.print("-");
Serial.println(local_s);
}