All Products
Search
Document Center

Simple Log Service:Examples of time field conversion

Last Updated:Jun 25, 2024

During query and analysis, the system may need to process time fields in logs, such as converting a timestamp to a specific format. This topic provides examples on how to convert time fields.

Time fields

Convert __time__ to a timestamp

You can use the from_unixtime function to convert the value of the __time__ field from a UNIX timestamp to a datetime expression that can return a timestamp value.

* | select from_unixtime(__time__) 

Display __time__ in a specific format

You can use the date_format function to convert the value of the __time__ field from a datetime expression that can return a timestamp value to a specific format.

* | select date_format(__time__, '%Y-%m-%d %H:%i:%S') 

Convert the original time field in a log to a specific format

You can perform the following steps to convert the original time field in a log from a string to a specific format.

  1. Use the date_parse function to convert the time field from a string to the Year-Month-Day Hour:Minute:Second format.

  2. Use the date_format function to extract the Year-Month-Day part.

  3. Use group by to group data.

  • Sample log:

    __topic__:  
    body_byte_sent:  307
    hostname:  example.com
    http_user_agent:  Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 QQ/192.0.2.1 V1_IPH_SQ_7.1.8_1_APP_A Pixel/750 Core/UIWebView NetType/WIFI QBWebViewType/1
    method:  GET
    referer:  www.example.com
    remote_addr:  192.0.2.0
    request_length:  111
    request_time:  2.705
    status:  200
    upstream_response_time:  0.225582883754
    url:  /?k0=v9&
    time:2017-05-17 09:45:00
  • Sample SQL statement:

    * | select date_format (date_parse(time,'%Y-%m-%d %H:%i:%S'), '%Y-%m-%d') as day, count(1) as uv group by day order by day asc