Problem with atoi() parsing a string

Hi All

I have been using atoi() to get some time data from a string sent from a modem (e.g. +CCLK: “15/01/02,09:37:48+42”)

This works as expected:

void SyncTime(){
  sendATcommand(AT_CCLK, "+CCLK:  ", 500);                //Read the network time from the GSM/3G Module
    Serial.println(ATcomResp);
  char* valPos = ATcomResp + 19;                        //Find the position in the response string ( +CCLK: "15/01/02,09:37:48+42")
  TimeHours = atoi(valPos);                         //Find & read HOURS Value
  TimeMinutes = atoi(valPos + 3);                       //Find and read the MINUTES value
  TimeSeconds = atoi(valPos + 3);                       //Find and read the SECONDS value
  
  

  Serial.println(TimeHours);
  Serial.println(TimeMinutes);
  Serial.println(TimeSeconds);
}

Then I tried to change this to also get the year, month & day from the reply.

This just gives me the correct value for year, but just “1” for everything else!

What am I missing (apart from more hair!)?

void SyncTime(){
  sendATcommand(AT_CCLK, "+CCLK:  ", 500);                //Read the network time from the GSM/3G Module
    Serial.println(ATcomResp);
  char* valPos = ATcomResp + 10;                        //Find the position in the response string ( +CCLK: "15/01/02,09:37:48+42")
  TimeYear = atoi(valPos);                              //Read YEAR value
  TimeMonth = atoi(valPos + 3);                         //Find & read MONTH value 
  TimeDay = atoi(valPos + 3);                           //Find & read DAY value 
  TimeHours = atoi(valPos + 3);                         //Find & read HOURS Value
  TimeMinutes = atoi(valPos + 3);                       //Find and read the MINUTES value
  TimeSeconds = atoi(valPos + 3);                       //Find and read the SECONDS value
  
  //TimeYear = (TimeYear + 2000);                         // should be right for the next 80 odd years...... 
  
  Serial.println(TimeYear);
  Serial.println(TimeDay);
  Serial.println(TimeMonth);
  Serial.println(TimeHours);
  Serial.println(TimeMinutes);
  Serial.println(TimeSeconds);
}

"valPos + 3" has the same value in every call to atoi() so it just evaluates the same number over and over again.
i.e. (valPos) is the year, (valPos+3) is the month, and you don't change it for any other field so it keeps re-evaluating the month which is 1.

Pete

But in the first example that works and returns the hours, minutes & seconds correctly.

(valPos + 3) would also increment the position of valPos ready for the next line?

You are correct - I changed the values and it now works - thank you

What it doesn’t explain is why the first example returns the correct values for hours, minutes & seconds!

Bah.........yes i am wrong - I wasn't printing the SECONDS value in the first example and have now found it matched the minutes one :roll_eyes:

I misunderstood what was happening with valPos each time - I thought it was incrementing the position. Now i understand it a bit better I hope!

Other ways

My response #12