Closed frsyuki closed 7 years ago
👍 for having it.
FWIW, let me share my case.
I need to calculate "N days ago from session_time (not last_session_time)" and format it as '%Y-%m-%d' in a workflow.
I did it by ${(d=new Date((session_time - (86400 * N)) * 1000) && [d.getFullYear(), ('0' + (d.getMonth()+1)).slice(-2), ('0' + d.getDate()).slice(-2)].join('-')}
. It worked but it has 4 complexity:
[d.getFullYear(), ...].join
). strftime will solve this!new Date((session_time ...) * 1000)
)session_time - (86400 * N)
almost work but roughly.(d=new Date(...)) && ....
)Other popular time library moment.js can do converting and avoid assigning variable.
Above one liner can be rewritten with moment.js as ${moment(session_time * 1000).add(-N, 'days').format('YYYY-MM-DD')}
. It's much simpler.
Probably typical user (including me) prefer strftime for formatting, but moment.js can do more work.
:+1: would also like something like this
I checked several libraries:
I first thought that simple strftime
is the best. However, I found that it can't cover following common operations well:
session_time
, next_session_time
, and last_session_time
to reformat themOn the other hand, Moment.JS seems quite feature complete:
$ jirb
> js = Java::jdk.nashorn.api.scripting.NashornScriptEngineFactory.new.getScriptEngine("--no-java", "--no-syntax-extensions", "-timezone=Asia/Tokyo")
> js.eval(File.read('moment.js')
> js.eval(%{moment("2016-09-27T00:00:00Z").format()})
=> "2016-09-27T09:00:00+09:00"
> js.eval(%{moment("2016-09-27T00:00:00Z").format("YYYY-MM-DD HH:mm:ss Z")})
=> "2016-09-27 09:00:00 +09:00"
> js.eval(%{moment("2016-09-27T00:00:00Z").utc().format("YYYY-MM-DD HH:mm:ss Z")})
=> "2016-09-27 00:00:00 +00:00"
> js.eval(%{moment("2016-09-27T00:00:00Z").utc().add(1, 'hours').format("YYYY-MM-DD HH:mm:ss Z")})
=> "2016-09-27 01:00:00 +00:00"
session_time
variable is ISO time format. Thus following code should work in digdag, for example:
echo>: ${moment(session_time).add(1, 'days').format("YYYY-MM-DD HH:00:00")}
@danielnorberg @komamitsu I think bundling Moment.js makes sense. Do you have any comments?
Moment is pretty nice. We're using it in the console.
Formatting timestamp is one of the common use cases of
${...}
expression. Vanilla JavaScript doesn't have good support for it. Having a strftime library such as https://github.com/samsonjs/strftime as a built-in library should be useful.Discussions are:
timezone:
parameter is used as the default timezone).