Time arithmetics on HTTP headers

I´m getting both "last-modified" (file last change timestamp) and "date" (now) headers from a GET() http request. I use collectHeaders() and I can get the values ok.

They are both in what I think is UTC format (strings):

last-modified: Sun, 04 Dec 2022 21:21:07 GMT
date: Sun, 04 Dec 2022 20:35:22 GMT

I need to perform some calculations and comparison, converting those strings to something "simpler". If converting these strings to datetime objects is not feasible, a string formatted as "YYYYMMDDhhmmss" would do the trick.

Is there any library to convert UTC to a more "usable" format that does not require parsing the string -including converting from "JanFeb...Dec" to "0102...12" ?

That would be parsing, and is not at all difficult to implement.

I recommend parsing to integers m/d/y/h/m/s, then the Arduino TimeLib library can handle the rest.

1 Like

This is doing the work in terms of date comparison (lower than, greater than) which is enough for my purposes at the time :slight_smile:

// returns a YYYYMMDDhhmmss string from a UTC formatted date (HTTP)
String UTC2Date(String UTCdate) {

  String namesList = "JanFebMarAprMayJunJulAugSepOctNovDec";
  String numbersList = "010203040506070809101112";
  int idx = namesList.indexOf(UTCDate.substring(8,11));
  if (idx == -1) { return "00000000000000"; }
  return UTCDate.substring(12,16) + numbersList.substring(int(idx / 3)*2, int(idx / 3)*2 + 2) + UTCDate.substring(5,7) + UTCDate.substring(17,19) + UTCDate.substring(20,22) + UTCDate.substring(23,25);
}

Basically parses the UTC string directly for everything but the month, which is converted from the 3-letter abbreviation to its 2-digit value.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.