Parse a date & time string

I'm looking for the simplest way to parse a date/time string (+CCLK: “00/06/09,17:34:23”)to separate the date and time values (like date = MM/DD/YYYY, time = HH:MM) each to a variable.

I had posted this question in a C++ forum, but I received one reply suggesting regex. I could find no basic examples close enough to understand, it was very complex, I wasn't sure if Arduino uses regex.

Any suggestions or examples appreciated.

strtok() may provide you with the answer for the location of the separators (in your case " and , and " again). Then you can strncpy() out the sections starting at the first 2 locations, length given by the difference to the location of the nect token.

Ok

Based on this example:
http://www.cplusplus.com/reference/cstring/strtok/

I’m testing this code in C++:

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "+CCLK: "00/06/09,17:34:23"";  // = +CCLK: "00/06/09,17:34:23";   produced more errors
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

But its producing the following errors:
C:\Documents and Settings\Sid\Desktop\C Projects\strtok Example\main.cpp||In function int main()':| C:\Documents and Settings\Sid\Desktop\C Projects\strtok Example\main.cpp|10|error: expected ,’ or `;’ before numeric constant|
C:\Documents and Settings\Sid\Desktop\C Projects\strtok Example\main.cpp|10|invalid digit “9” in octal constant|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|

The example works perfect AS-IS, but doesn’t like my string: +CCLK: “00/06/09,17:34:23”

A few issues here: 1. You cannot have a main() in the Arduino world as that is in the library. Put the code in setup() if you want to run it once. 2. You have a " in the string, which also happens to be the token defining the beginning and end of the string in C. You need to 'escape' this token at compile time by (from memory here) putting it in twice "" so your string should become

"+CCLK: ""00/06/09,17:34:23""";

If you were receiving the string thru a serial link or other comms, then there is no need to escape as the string is not being interpreted by the compiler.

If the "string" you are receiving is always the same, you don't need to parse it at all using tokens or anything else.

Your month is always the 8th and 9th char in the array. Your day is always the 11th and 12th char. and so on.

Yes, I understand what your saying about receiving the string directly through serial link and not being compiled. I'll run some tests using Hyperterminal to see what the exact string characters received are. Or I may setup a I2C/TWI Serial 2004 20x4 LCD Module to display from myUno.

If the "string" you are receiving is always the same, you don't need to parse it at all using tokens or anything else. Your month is always the 8th and 9th char in the array. Your day is always the 11th and 12th char. and so on.

I'll determine if the character position is the same at all times, I believe it should be, then I'll have other options?

If you have affixed format string then you can just pick out the characters and remap them into your required sequence. For example, you might take the 10 th character of the string and put it in the first position of your string, etc.

However, if the string changes by just one character position at any time you will be out.

char str[] = "+CCLK: "00/06/09,17:34:23"";

should be

char str[] = "+CCLK: \"00/06/09,17:34:23\"";