github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/md/adjust_timezone.en.md (about)

     1  ### `adjust_timezone()` {#fn-adjust-timezone}
     2  
     3  Function prototype: `fn adjust_timezone(key: int, minute: int)`
     4  
     5  Function parameters:
     6  
     7  - `key`: Nanosecond timestamp, such as the timestamp obtained by the `default_time(time)` function
     8  - `minute`: The return value allows the number of minutes (integer) beyond the current time, the value range is [0, 15], the default value is 2 minutes
     9  
    10  Function description: Make the difference between the incoming timestamp minus the timestamp of the function execution time within (-60+minute, minute] minutes; it is not applicable to data whose time difference exceeds this range, otherwise it will result in wrong data being obtained. Calculation process:
    11  
    12  1. Add hours to the value of key to make it within the current hour
    13  2. At this time, calculate the difference between the two minutes. The value range of the two minutes is [0, 60), and the difference range is between (-60,0] and [0, 60)
    14  3. If the difference is less than or equal to -60 + minute, add 1 hour, and if the difference is greater than minute, subtract 1 hour
    15  4. The default value of minute is 2, and the range of the difference is allowed to be (-58, 2], if it is 11:10 at this time, the log time is 3:12:00.001, and the final result is 10:12:00.001; if at this time is 11:59:1.000, the log time is 3:01:1.000, and the final result is 12:01:1.000
    16  
    17  Example:
    18  
    19  ```json
    20  # input data 1 
    21  {
    22      "time":"11 Jul 2022 12:49:20.937", 
    23      "second":2,
    24      "third":"abc",
    25      "forth":true
    26  }
    27  ```
    28  
    29  Script:
    30  
    31  ```python
    32  json(_, time)      # Extract the time field (if the time zone in the container is UTC+0000)
    33  default_time(time) # Convert the extracted time field into a timestamp
    34                     # (Use local time zone UTC+0800/UTC+0900... parsing for data without time zone)
    35  adjust_timezone(time)
    36                     # Automatically (re)select time zone, calibrate time offset
    37  
    38  ```
    39  
    40  Execute `datakit pipeline -P <name>.p -F <input_file_name>  --date`:
    41  
    42  ```json
    43  # output 1
    44  {
    45    "message": "{\n    \"time\":\"11 Jul 2022 12:49:20.937\",\n    \"second\":2,\n    \"third\":\"abc\",\n    \"forth\":true\n}",
    46    "status": "unknown",
    47    "time": "2022-07-11T20:49:20.937+08:00"
    48  }
    49  ```
    50  
    51  local time: `2022-07-11T20:55:10.521+08:00`
    52  
    53  The times obtained by using only `default_time` and parsing according to the default local time zone (UTC+8) are:
    54  
    55  - Output result of input 1: `2022-07-11T12:49:20.937+08:00`
    56  
    57  After using `adjust_timezone` will get:
    58  
    59  - Output result of input 1: `2022-07-11T20:49:20.937+08:00`