Updated on 2025-12-04 GMT+08:00

Conversion Between Datetime Objects and Datetime Strings

  1. Processing functions
    • Intelligent conversion function: dt_parse, which converts a datetime string or Unix timestamp to a datetime object.
    • dt_astimezone function, which returns a datetime object with new time zone information.
  2. Scenario 1: Convert a datetime string without time zone information to a datetime object in a specified time zone.

    For example, the datetime string 2019-06-02 18:41:26 without time zone information can be converted between different time zones based on the Unix timestamp. Convert the datetime in the Los Angeles time zone to the datetime in the Shanghai time zone.

    • Raw log: The value of the time field is Los Angeles time.
      {
          "time" : "2019-06-04 2:41:26"
      }
    • Processing rule
      e_set("timestamp", dt_parsetimestamp(v("time"), tz="America/Los_Angeles"))
      e_set("Shanghai_time", dt_parse(v("timestamp"), tz="Asia/Shanghai"))
    • Processing result
      {
      	"Shanghai_time": "2019-06-04 17:41:26+08:00",
      	"time": "2019-06-04 2:41:26",
      	"timestamp": 1559641286
      }
  3. Scenario 2: Convert a datetime string without a time zone to a datetime object with a time zone
    • Raw log
      {
          "time" : "2019-07-10 06:58:19"
      }
    • Processing rule
      e_set("new_time", dt_parse(v("time"), tz="Asia/Shanghai"))
    • Processing result
      {
      	"new_time": "2019-07-10 06:58:19+08:00",
      	"time": "2019-07-10 06:58:19"
      }
  4. Scenario 3: Convert a datetime string with time zone information to a datetime object in the target time zone.
    • Raw log
      {
          "time" : "2019-06-04 2:41:26+08:00"
      }
    • Processing rule
      e_set("new_time",dt_astimezone(v("time"), "America/Los_Angeles"))
    • Processing result
      {
      	"new_time": "2019-06-03 11:41:26-07:00",
      	"time": "2019-06-04 2:41:26+08:00"
      }