Updated on 2026-05-12 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: converts a datetime object or datetime string to a Unix timestamp.
  • Dedicated function dt_totimestamp: specifically converts a datetime object to a Unix timestamp.

Converting a Unix timestamp to a datetime object

  • Intelligent conversion function dt_parse: converts a Unix timestamp or datetime string to a datetime object.
  • Dedicated function dt_fromtimestamp: specifically converts 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: converts a datetime object, Unix timestamp, or datetime string to a datetime string in a specified format.
  • Dedicated function dt_strftime: specifically converts a datetime object to a datetime string.

Converting a datetime string to a datetime object

  • Intelligent conversion function dt_parse: converts a datetime string or Unix timestamp to a datetime object.
  • Dedicated function dt_strptime: specifically converts 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: converts a datetime string or datetime object to a Unix timestamp.

Converting a Unix timestamp to a datetime string

  • Intelligent conversion function dt_str: converts a Unix timestamp, datetime object, or datetime string to a datetime string in a specified format.
  • Dedicated function dt_strftimestamp: specifically converts 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 handle various input types, such as Unix timestamps, datetime objects, and datetime strings. They intelligently perform the conversion.

  • Dedicated functions

    Intelligent conversion functions may not meet all user requirements. For example, if a log contains a non-standard custom 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.