How do I format and convert time strings?

The strftime() function takes unix epoch format as input and can format the time using the standard strftime templates.

1
2
3
4
5
6
7
  bundle agent main
  {
    reports:

        "$(with)"
          with => strftime( localtime, "%FT%R:%S%Z", now() );
  }
R: 2018-01-27T15:47:14CST

Unfortunately there is currently no native way to convert from other inputs formats. You can however, use an external command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  bundle agent main
  {
    vars: 

        "mock_sys_last_policy_update"
          string => "Sat Jan 27 20:18:37 2018",
          comment => "This is the same format that sys.last_policy_update uses";

    reports:

        "$(with) == $(mock_sys_last_policy_update)"
          with => execresult( 'date --date="$(mock_sys_last_policy_update)" "+%s"', useshell);
  }
R: 1517105917 == Sat Jan 27 20:18:37 2018