Extract a date out of a string

This test code works for me:

This compares the exact current date AND time to the date+time format that is apparently returned by the APC UPS. That format is not quite what Rule Machine wants:

  • The date needs to be separated from the time by a "T"; accordingly, the first space is replaced by "T", which also replaces the second space.
  • Fortunately, the 2nd replacement uniquely results in "T+". RM doesn't want the "T" or the "+" either, it wants a decimal point, 3 digits of fractional seconds, and a hyphen separating the time from the time zone offset. So replace "T+" with ".000-".
  • After the replacements, the "LastUpdate_string_edit" string is converted to the DateTime variable "LastUpdateDate".

Note that I used a 2nd variable, "LastUpdate_string_edit" just to be able to track the changes from "LastUpdate_string". You could dispense with the 2nd variable and substitute "LastUpdate_string" for "LastUpdate_string_edit".

NOTE: the action "Set LastUpdateDate to %LastUpdate_string_edit%" is VERY, VERY TOUCHY -- if you get the format of the datetime string wrong in the slightest, the Rule will CRASH irrecoverably. Ask my how I know.

PS: The value for "LastUpdateDate" in the Local Variables table differs by 3 hours from the computed value actually used in the IF statement -- I have no idea why, but the IF statement uses the correct value.

2 Likes