1

I have a string that contains a data, "2013-04-20T13:50:47.390" like this.

I was thinking of methods to work with the string values. I would like to use sscanf, since it seems an efficient way to do so, but I don't know the commands I need to add in the sscanf so it know the separators like '-', 'T', ':', '.'. Any suggestion?

Btw, is there any efficient way to cut the string, I would like to save in another string the part after the T.

Thanks for the time.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Pedro Lima
  • 387
  • 2
  • 14
  • 1
    What have you tried? A pointer to the digit after the ‘T’ would give you the remainder. You could use `strdup()` (a POSIX standard function, but not a standard C function) to copy and save it if need be. – Jonathan Leffler Apr 22 '18 at 16:01
  • 2
    Possible duplicate of [How to read / parse input in C? The FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – Yunnosch Apr 22 '18 at 16:05
  • 3
    Note that both the answers currently available would accept `" 12-4-  3T  13:  50:  4.      1"` as valid input, and would report the milliseconds as `1` in both that and `"2013-04-20T13:50:47.1"`, rather than as 100 milliseconds, which is more accurate. That doesn't automatically mean the answers are wrong. It does mean it is harder than you'd like to parse a date string with `sscanf()`. – Jonathan Leffler Apr 22 '18 at 18:42

2 Answers2

3

You can do it like this:

int year, month, day, hour, min, sec, msec;
int ret;
ret = sscanf(str, "%4d-%2d-%2dT%2d:%2d:%2d.%d", 
             &year, &month, &day, &hour, &min, &sec, &msec);
if (ret != 7) {
    printf("Error while parsing time");
}

EDIT: As mentioned by @DavidBowling in comments, the 2 in format specifier %2d tells sscanf to read up to 2 digits for the associated integer.

medalib
  • 937
  • 1
  • 6
  • 7
  • What does the '04' '02' represent? Byte size? – Pedro Lima Apr 22 '18 at 16:13
  • 1
    Maybe you should add an explanation for the field width specifier to your answer; that would be helpful to OP. You don't need the `0` in `%02`, though. In fact, it might be a little bit confusing because: 1) in most other places `02` would be an octal integer constant, and 2) zeroes are meaningful in `fprintf()` family format strings. – ad absurdum Apr 22 '18 at 16:32
  • @DavidBowling, Thank you for your explanation. I thought the `0` may remove the confusion about octal numbers, now after testing, I figured out you are right :). I will edit my post. – medalib Apr 22 '18 at 16:34
  • @DavidBowling: actually, the `0` prefix in the source string would be meaningful for a `%i` conversion specifier where it would indeed indicate an octal encoded number. Both `%d` and `%u` expect decimal encoded integers (`d` is for decimal). The size indicators in the format string are only needed to reject a source string where some of the fields would have too many digits. If there is no need for a stricter parser, `"%d-%d-%dT%d:%d:%d.%d"` could be used instead. – chqrlie Apr 22 '18 at 18:32
  • @chqrlie -- Sure. I was only talking about the format string, where (unless I am missing something) the `fscanf()` functions interpret the number following `%` and the optional assignment suppression character (`*`) as a decimal integer. Of course, zero prefixes in the source string (or elsewhere in the code) may be meaningful. The original answer had a format string like: `"%4d-%02d-%02dT%02d:%02d:%02d.%d"`. That, and the erroneous suggestion that here `0` tells `sscanf()` about possible leading zeros, is what the comments were directed at. – ad absurdum Apr 22 '18 at 18:47
  • @DavidBowling: we are in complete agreement. I had not seen the original answer. My comment was primarily trying to teach other readers about the lesser known `%i` conversion specifier. – chqrlie Apr 22 '18 at 18:50
  • @chqrlie -- it is always good to point out that `%i` may behave a little differently than naively expected: so many nuances to come to grips with in using `fscanf()`! I was similarly interested in commenting on the peculiar appearance of a decimal value that at first glance might appear to be octal ;) – ad absurdum Apr 22 '18 at 18:54
0

Thanks, i found how to do it. I just needed to add the separatores to the sscanf:

    sscanf(stringTest,"%d-%d-%dT%d:%d:%d.%d", &year,&month,&day,&hour,&min,&sec,&ms);
Pedro Lima
  • 387
  • 2
  • 14