I could solve my problem as the following but I still have a small problem
Here is the code. I opted for that code because there is no need of an additonally library.
First I initiate a variabl
uint32_t posix
I used that code, (thank Jurs)
void get_posix(char * buff)
{
// gsm_time format: "16/12/21,00:01:23+08";
char format[23]="%d/%d/%d,%d:%d:%d+%d";
int year,month,day,hour,minute,second,timezone;
sscanf(buff,format,&year,&month,&day,&hour,&minute,&second,&timezone);
if (year<100) year+=2000;
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; // That seams to be wrong
// epoch -=timezone*900L;
posix = (uint32_t)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;
}
I get the posix time as the following
void get_posix()
{
char buff[23];
if(fona.getTime(buff, 23))
{
// Remove first " and last " (it is not charmly)
buff[0] = buff[1];// " 1
buff[1] = buff[2];// 1 6
buff[2] = buff[3];// 6 /
buff[3] = buff[4];// / 1
buff[4] = buff[5];// 1 2
buff[5] = buff[6];// 2 /
buff[6] = buff[7];// / 2
buff[7] = buff[8];// 2 2
buff[8] = buff[9];// 2 ,
buff[9] = buff[10];// , 0
buff[10] = buff[11];// 0 1
buff[11] = buff[12];// 1 :
buff[12] = buff[13];// : 1
buff[13] = buff[14];// 1 1
buff[14] = buff[15];// 1 :
buff[15] = buff[16];// : 0
buff[16] = buff[17];// 0 0
buff[17] = buff[18];// 0 +
buff[18] = buff[19];// + 0
buff[19] = buff[20];// 0 4
buff[20] = buff[21];// 4 \0
buff[20]='\0';
buff[21]='\0';
buff[22]='\0';
Serial.print(F("DEBUG buff:")); Serial.println(buff);
get_posix(buff);
Serial.print(F("DEBUG posix:")); Serial.println(posix);
}
else
{
sprintln(F("Error getting posix time"),2);
}
/*
* Another way to get posix time, but need #include <time.h>
*/
/*
Serial.println(buff);
char posixe[]="16/12/22,00:01:23+04";
struct tm tm;
time_t epochUTC;
if ( strptime(posixe, "%y/%m/%d,%T", &tm) != NULL ) {
epochUTC = mktime(&tm); // I ignore the +04 piece may be you want to take this into account?
} else {
Serial.println(F("you have a bug with POSIX"));
}
Serial.print(F("POSIX time1:")); Serial.println(epochUTC);
*/
}
Serial.print(F("DEBUG posix:")); display that
1482494931
Regarding my problem with ajson. ajson accpeted only int ou double. For posix with need a uint32_t.
I modified the ajson.cpp file and I add a new fonction. I also addapted ajson.h
aJsonObject*
aJsonClass::createItem(uint32_t num)
{
aJsonObject *item = newItem();
if (item)
{
item->type = aJson_Int;
item->valueint = (uint32_t) num;
}
return item;
}
void
aJsonClass::addNumberToObject(aJsonObject* object, const char* name, uint32_t n)
{
addItemToObject(object, name, createItem(n));
}
Now the last problem is the following and I think it because I commented
// epoch -=timezone*3600L;
I commented it because it gave a wrong a time. 2 hour more than the reality. I timezone is a quarter of an hour, it should be?
// epoch -=timezone*900L;
Any way, I want to have UTC.
The problem is the following. As I wrote, the returned posix time is
1464661369
and Ubidots want to have
1464661369000
I supposed it because the timezone is not added.
I supposed to solve my problem, I could do this
epoch += 000; // I want to have UTC time
void get_posix(char * buff)
{
// gsm_time format: "16/12/21,00:01:23+08";
char format[23]="%d/%d/%d,%d:%d:%d+%d";
int year,month,day,hour,minute,second,timezone;
sscanf(buff,format,&year,&month,&day,&hour,&minute,&second,&timezone);
if (year<100) year+=2000;
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*900L;
epoch += 000; // I want to have UTC time
posix = (uint32_t)epoch;
}
what do you suggest me, for the best?
Thank a lot!