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

Data Type Conversion and Conversion Functions

Figure 1 illustrates details about the conversion between datetime strings, datetime objects, and Unix timestamps, along with the corresponding conversion functions.

Figure 1 Conversion relationship

Table 1 details the specific conversion scenarios and functions.

Table 1 Conversion description

Conversion Scenario

Conversion Function

Conversion between datetime objects and Unix timestamps

Converting a datetime object to a Unix timestamp

  • Intelligent conversion function: dt_parsetimestamp, which converts a datetime object or datetime string to a Unix timestamp.
  • Dedicated function: dt_totimestamp, which converts only a datetime object to a Unix timestamp.

Converting a Unix timestamp to a datetime object

  • Intelligent conversion function: dt_parse, which converts a Unix timestamp or datetime string to a datetime object.
  • Dedicated function: dt_fromtimestamp, which converts only a Unix timestamp to a datetime object.

Conversion between datetime objects and datetime strings

Converting a datetime object to a datetime string

  • Intelligent conversion function: dt_str, which converts a datetime object, Unix timestamp, or datetime string to a datetime string in a specified format.
  • Dedicated function: dt_strftime, which converts only a datetime object to a datetime string.

Converting a datetime string to a datetime object

  • Intelligent conversion function: dt_parse, which converts a datetime string or Unix timestamp to a datetime object.
  • Dedicated function: dt_strptime, which converts only a datetime string to a datetime object.

Conversion between datetime strings and Unix timestamps

Converting a datetime string to a Unix timestamp

Intelligent conversion function: dt_parsetimestamp, which converts a datetime string or datetime object to a Unix timestamp.

Converting a Unix timestamp to a datetime string

  • Intelligent conversion function: dt_str, which converts a Unix timestamp, datetime object, or datetime string to a datetime string in a specified format.
  • Dedicated function: dt_strftimestamp, which converts only a Unix timestamp to a datetime string.

The preceding figure and table outline six conversion types among the three data types. Each conversion type offers two methods: using an intelligent conversion function or a dedicated conversion function.

  • Intelligent conversion functions

    These functions (for example, dt_parse) can accept parameters of various types, such as Unix timestamp, datetime object, and datetime string, to intelligently perform the conversion.

  • Dedicated functions

    Intelligent conversion functions may meet all user requirements. For example, if a user specifies a unique or non-standard date format, intelligent functions like dt_parse may fail to automatically parse the log data. In such cases, a dedicated function like dt_strptime must be used to parse the specified format.