Need SubStr() function

I am trying to pull the time out of a the following string:
"Date: Wed, 20 Jan 2010 02:57:58 GMT"

I want to extract the "02:57". I'd like to use a substr function, something like:
mytime = substr(GMD_Date, 24, 5);

But I don't see a substr() available for Arduini. How can I do this?

--Scott

Lets say your string is stored this way.

Char Date[] = "Date: Wed, 20 Jan 2010 02:57:58 GMT";

If you wanted to pull just the time from that format you would need to call the section of the string out something like this.

Date[23]

That will pull up the 0 in the 02:57:58 of the string. The "D" at the beginning of the string is zero if i'm not mistaking. So to pull the full time you'll need to use 23-30. Now how you want to use that will dictate the way the code is laid out.

if(input = time) 
{
  for (int t = 23; t < 31; t++)
  {
   Serial.print("Time is: ");
   Serial.print(Date[t]);
  }
  delay(2000);
}

Something like that should work for pulling the time from the string and outputting it to the serial monitor. Where whatever kind of input your looking for is asking what the time is from the string stored. Whatever your wanting those variables to be. But the format of the string would have to be such that the area being pulled from will be in the same place every time. In this case if it was a Thursday it would be Thurs and the time start area would be off by 2 places.

DISCLAIMER: I'm am still very much a noob at this. So i may be wrong here. This is from me pulling bits and pieces from examples i've played around with over the past few days and using what little i have learned. Also i'm currently at work so i don't have my arduino with me to be able to throw a quick test sketch together to see if it would work.

Try this:

char date_time[] = "Date: Wed, 20 Jan 2010 02:57:58 GMT";
char time[6];

    strncpy(time,(date_time+23),5);
    time[5] = '\0';

-Mike

In the contributed libraries on the Arduino site, there is one for string functions: String() - Arduino Reference.

It has a substr function, plus many others.

.andy

The features of the String library are just a dumbed-down subset of string.h: avr-libc: <string.h>: Strings

I got this working, now I am having trouble with an an IF statement comparing the time string to some text in quotes.

char date_time[] = "Date: Wed, 20 Jan 2010 02:57:58 GMT";
char time[6];

    strncpy(time,(date_time+23),5);
    time[5] = '\0';

   //Even when I Serial.print(time) and it returns 02:57 the following
   //statement does not return TRUE.  I must not be comparing time 
   //to 02:57 properly.
 
   if(time=="02:57"){
   
   }

Scott,

That doesn't do what you think it's doing. Try this:

char date_time[] = "Date: Wed, 20 Jan 2010 02:57:58 GMT";
char time[6];

    strncpy(time,(date_time+23),5);
    time[5] = '\0';

   //Even when I Serial.print(time) and it returns 02:57 the following
   //statement does not return TRUE.  I must not be comparing time
   //to 02:57 properly.

   if(strcmp(time,"02:57") == 0)  // strings match {
  
   }

Your orignal statement was comparing pointers, i.e., memory addresses. Since the 'time' array has a different address from the address of the literal string "02:27", the comparison is false.

The C programming language doesn't have any built in string functions. It's all done with library functions, or routines you write that manipulate strings character-by-character.

Regards,

-Mike

Something else to keep in mind - The offset of 23 characters assumes that all the fields before the time field are of fixed length. It looks like the day name is abbreviated to 3 characters, so that should be fixed. But the 2 digit date might only be a single digit if it falls before the 10th of the month.

"Date: Wed, 20 Jan 2010 02:57:58 GMT"
"Date: Wed, 7 Jan 2010 02:57:58 GMT"

Now I'd like to convert my time in HH:MM format into Hour and Minutes integers. If I have:
char GMTTime[6] = "10:45";
INT Hour;
INT Minute;

How do I pull the "10" and "45" out and put them into integers?

--Scott

#include <stdlib.h>

char GMTTime[6] = "10:45";
int Hour;
int Minute;

Hour = atoi(GMTTIME);
Minute = atoi(GMTTIME+3);

Regards,

-Mike

Thanks, this worked great. How does atoi() know to grab 2 characters?

--Scott

Scott,

atoi() stops at the first non-digit it encounters, so the colon (':') stopped the first conversion. The end of a string has a NUL ('\0') which, being a non-digit, stopped the second conversion.

Regards,

-Mike