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

     1  ### `datetime()` {#fn-datetime}
     2  
     3  Function prototype: `fn datetime(key, precision: str, fmt: str, tz: str = "")`
     4  
     5  Function description: Convert timestamp to specified date format
     6  
     7  Function parameters:
     8  
     9  - `key`: Extracted timestamp (required parameter)
    10  - `precision`: Input timestamp precision (s, ms, us, ns)
    11  - `fmt`: date format, provides built-in date format and supports custom date format
    12  - `tz`: time zone (optional parameter), convert the timestamp to the time in the specified time zone, the default time zone of the host is used
    13  
    14  Built-in date formats:
    15  
    16  | Built-in format | date                                  | description               |
    17  | -               | -                                     | -                         |
    18  | "ANSI-C"        | "Mon Jan _2 15:04:05 2006"            |                           |
    19  | "UnixDate"      | "Mon Jan _2 15:04:05 MST 2006"        |                           |
    20  | "RubyDate"      | "Mon Jan 02 15:04:05 -0700 2006"      |                           |
    21  | "RFC822"        | "02 Jan 06 15:04 MST"                 |                           |
    22  | "RFC822Z"       | "02 Jan 06 15:04 -0700"               | RFC822 with numeric zone  |
    23  | "RFC850"        | "Monday, 02-Jan-06 15:04:05 MST"      |                           |
    24  | "RFC1123"       | "Mon, 02 Jan 2006 15:04:05 MST"       |                           |
    25  | "RFC1123Z"      | "Mon, 02 Jan 2006 15:04:05 -0700"     | RFC1123 with numeric zone |
    26  | "RFC3339"       | "2006-01-02T15:04:05Z07:00"           |                           |
    27  | "RFC3339Nano"   | "2006-01-02T15:04:05.999999999Z07:00" |                           |
    28  | "Kitchen"       | "3:04PM"                              |                           |
    29  
    30  
    31  Custom date format:
    32  
    33  The output date format can be customized through the combination of placeholders
    34  
    35  | character | example | description |
    36  | - | - | - |
    37  | a | %a | week abbreviation, such as `Wed` |
    38  | A | %A | The full letter of the week, such as `Wednesday`|
    39  | b | %b | month abbreviation, such as `Mar` |
    40  | B | %B | The full letter of the month, such as `March` |
    41  | C | %c | century, current year divided by 100 |
    42  | **d** | %d | day of the month; range `[01, 31]` |
    43  | e | %e | day of the month; range `[1, 31]`, pad with spaces |
    44  | **H** | %H | hour, using 24-hour clock; range `[00, 23]` |
    45  | I | %I | hour, using 12-hour clock; range `[01, 12]` |
    46  | j | %j | day of the year, range `[001, 365]` |
    47  | k | %k | hour, using 24-hour clock; range `[0, 23]` |
    48  | l | %l | hour, using 12-hour clock; range `[1, 12]`, padding with spaces |
    49  | **m** | %m | month, range `[01, 12]` |
    50  | **M** | %M | minutes, range `[00, 59]` |
    51  | n | %n | represents a newline character `\n` |
    52  | p | %p | `AM` or `PM` |
    53  | P | %P | `am` or `pm` |
    54  | s | %s | seconds since 1970-01-01 00:00:00 UTC |
    55  | **S** | %S | seconds, range `[00, 60]` |
    56  | t | %t | represents the tab character `\t` |
    57  | u | %u | day of the week, Monday is 1, range `[1, 7]` |
    58  | w | %w | day of the week, 0 for Sunday, range `[0, 6]` |
    59  | y | %y | year in range `[00, 99]` |
    60  | **Y** | %Y | decimal representation of the year|
    61  | **z** | %z | RFC 822/ISO 8601:1988 style time zone (e.g. `-0600` or `+0800` etc.) |
    62  | Z | %Z | time zone abbreviation, such as `CST` |
    63  | % | %% | represents the character `%` |
    64  
    65  Example:
    66  
    67  ```python
    68  # input data:
    69  #    {
    70  #        "a":{
    71  #            "timestamp": "1610960605000",
    72  #            "second":2
    73  #        },
    74  #        "age":47
    75  #    }
    76  
    77  # script
    78  json(_, a.timestamp)
    79  datetime(a.timestamp, 'ms', 'RFC3339')
    80  ```
    81  
    82  
    83  ```python
    84  # script
    85  ts = timestamp()
    86  datetime(ts, 'ns', fmt='%Y-%m-%d %H:%M:%S', tz="UTC")
    87  
    88  # output
    89  {
    90    "ts": "2023-03-08 06:43:39"
    91  }
    92  ```
    93  
    94  ```python
    95  # script
    96  ts = timestamp()
    97  datetime(ts, 'ns', '%m/%d/%y  %H:%M:%S %z', "Asia/Tokyo")
    98  
    99  # output
   100  {
   101    "ts": "03/08/23  15:44:59 +0900"
   102  }
   103  ```