Working with times
The to_time_unit
filter allows to convert times from a human-readable string to a unit. For example, '4h 30min 12second' | community.general.to_time_unit('hour')
gives the number of hours that correspond to 4 hours, 30 minutes and 12 seconds.
There are shorthands to directly convert to various units, like to_hours
, to_minutes
, to_seconds
, and so on. The following table lists all units that can be used:
Unit name |
Unit value in seconds |
Unit strings for filter |
Shorthand filter |
---|---|---|---|
Millisecond |
1/1000 second |
|
|
Second |
1 second |
|
|
Minute |
60 seconds |
|
|
Hour |
60*60 seconds |
|
|
Day |
24*60*60 seconds |
|
|
Week |
7*24*60*60 seconds |
|
|
Month |
30*24*60*60 seconds |
|
|
Year |
365*24*60*60 seconds |
|
|
Note that months and years are using a simplified representation: a month is 30 days, and a year is 365 days. If you need different definitions of months or years, you can pass them as keyword arguments. For example, if you want a year to be 365.25 days, and a month to be 30.5 days, you can write '11months 4' | community.general.to_years(year=365.25, month=30.5)
. These keyword arguments can be specified to to_time_unit
and to all shorthand filters.
- name: Convert string to seconds
debug:
msg: "{{ '30h 20m 10s 123ms' | community.general.to_time_unit('seconds') }}"
- name: Convert string to hours
debug:
msg: "{{ '30h 20m 10s 123ms' | community.general.to_hours }}"
- name: Convert string to years (using 365.25 days == 1 year)
debug:
msg: "{{ '400d 15h' | community.general.to_years(year=365.25) }}"
This produces:
TASK [Convert string to seconds] **********************************************************
ok: [localhost] => {
"msg": "109210.123"
}
TASK [Convert string to hours] ************************************************************
ok: [localhost] => {
"msg": "30.336145277778"
}
TASK [Convert string to years (using 365.25 days == 1 year)] ******************************
ok: [localhost] => {
"msg": "1.096851471595"
}