How can I calculate a duration from start and end time #1479
Unanswered
kontango-bot
asked this question in
Q&A
Replies: 3 comments
-
It seems that vars are evaluated when they are called, so i don't think this approach will work either way. |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is what I ended up with: vars:
start_time: { sh: date +%s }
tasks:
test:
cmds:
- sleep 5
- task: end_timer
- echo "Done."
end_timer:
cmds:
- "echo Duration: {{ .duration | duration }}"
vars:
duration: { sh: "echo $(( $(date +%s) - {{ .start_time }} ))" } |
Beta Was this translation helpful? Give feedback.
0 replies
-
I've found another solution: version: '3'
tasks:
test:
desc: "Test timing measurements of task"
cmds:
- echo "⌚️ Test timing measurements of a task ⌚️"
- echo ""
- sleep 5
- task: task_timer
vars:
start_time: '{{ now |unixEpoch }}'
silent: true
task_timer:
desc: "Calculate timing of the calling task"
# Call this task at the end of the caller task with following parameters:
# - task: task_timer
# vars:
# start_time: '{{ now | unixEpoch }}'
summary: |
This task is a helper task to calulate the duration in seconds
for the caller task
vars:
end_time: '{{now | unixEpoch}}'
requires:
vars: [start_time]
cmds:
- 'echo ⏲️ -----------------------------------------------------------------------'
# - 'echo ⏲️ StartTime: {{ .start_time }} EndTime: {{ .end_time }}'
- 'echo ⏲️ Duration: {{ sub .end_time .start_time | duration }}'
- 'echo ⏲️ -----------------------------------------------------------------------'
silent: true
internal: true |
Beta Was this translation helpful? Give feedback.
0 replies
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
here's my taskfile:
This is invalid syntax, and i've tried a few other things (e.g. without parens) that also seem to be invalid.
Part of the problem is that
.start_time
gets converted to a string, which is not suprising. I'm not sure how to convert it back into a date before passing it intonow.Sub
. Or, if there is a way to storenow
without converting it to a string, that would be ideal.Beta Was this translation helpful? Give feedback.
All reactions