Dear Jurs
I am sorry but I can no make working your code. It worked until I remove
char timestamp[]="16/12/21,00:01:23+08";
to get the time from the parameter.
I modified as the following
if ti is "16/12/22,23:09:16+04"
I got it converted as -1033158260
char buff[23];
long posix;
fona.getTime(buff, 23);
get_posix(buff);
Serial.print(F("posix:"));
Serial.println(posix);
void get_posix(const char * ti)
{
Serial.print(F("to posix:"));
Serial.println(ti);
// gsm_time format: "16/12/21,00:01:23+08";
//char ti[] = "16/12/21,00:01:23+08";
// char ti[23];
//strncpy(ti, t, 23);
char format[23]="%d/%d/%d,%d:%d:%d%d"; // if I remove 23, it bugs
int year,month,day,hour,minute,second,timezone;
sscanf(ti,format,&year,&month,&day,&hour,&minute,&second,&timezone);
if (year<100) year+=2000;
Serial.print(F("to p4"));
/*
Serial.println(year);
Serial.println(month);
Serial.println(day);
Serial.println(hour);
Serial.println(minute);
Serial.println(second);
Serial.println(timezone);
*/
long epoch=0;
for (int yr=1970;yr<year;yr++)
if (isLeapYear(yr)) epoch+=366*86400L;
else epoch+=365*86400L;
for(int m=1;m<month;m++) epoch+=daysInMonth(year,m)*86400L;
epoch+=(day-1)*86400L;
epoch+=hour*3600L;
epoch +=minute*60;
epoch +=second;
epoch-=timezone*3600L;
//Serial.println(epoch);
posix = epoch;
}
byte daysInMonth(int yr,int m)
{
byte days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if (m==2 && isLeapYear(yr)) return 29;
else return days[m-1];
}
bool isLeapYear(int yr)
{
if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) return true;
else return false;
}
What would you suggest me?
I do nt understand, it seam not so complicated!
NB: Idealy, it's to have posix in double but without decimal