Diter
diter
is a date iteration command.
Installation
$ gem install diter
Or download the script:
$ curl https://raw.githubusercontent.com/ojima-h/diter/master/bin/diter > bin/diter
$ chmod +x bin/diter
Usage
Basic usage:
$ diter 2016-01-01 2016-01-03
2016-01-01
2016-01-02
2016-01-03
Run command:
$ diter 2016-01-01 2016-01-03 -- echo date={}
date=2016-01-01
date=2016-01-02
date=2016-01-03
Reverse order:
$ diter 2016-01-03 2016-01-01 -- echo date={}
date=2016-01-03
date=2016-01-02
date=2016-01-01
Date format:
$ diter 2016-01-01 2016-01-03 -F %Y/%m/%d
2016/01/01
2016/01/02
2016/01/03
Date format in command:
$ diter 2016-01-01 2016-01-03 -- echo date=%Y/%m/%d
date=2016/01/01
date=2016/01/02
date=2016/01/03
Refer to Date#strftime for possible date formats.
Parallel execution (with xargs):
$ diter 2016-01-01 2016-01-03 | xargs -I{} -P2 -t echo date={}
echo date=2016-01-01
echo date=2016-01-02
date=2016-01-01
date=2016-01-02
echo date=2016-01-03
date=2016-01-03
Loop:
$ for lang in en ja fr; do
> for date in `diter 2016-01-01 2016-01-03`; do
> echo lang=$lang date=$date
> done
> done
lang=en date=2016-01-01
lang=en date=2016-01-02
lang=en date=2016-01-03
lang=ja date=2016-01-01
lang=ja date=2016-01-02
lang=ja date=2016-01-03
lang=fr date=2016-01-01
lang=fr date=2016-01-02
lang=fr date=2016-01-03
Filter by the day of week:
$ diter 2016-01-01 2016-01-31 --wday=1
2016-01-04
2016-01-11
2016-01-18
2016-01-25
Filter by the day of month:
$ diter 2016-01-01 2016-04-01 --mday=15
2016-01-15
2016-02-15
2016-03-15
A negative integer is assumed to be relative to the end of month:
$ diter 2016-01-01 2016-04-01 --mday=-1
2016-01-31
2016-02-29
2016-03-31
In a Ruby script:
require 'diter'
from = Date.new(2016, 1, 1)
to = Date.new(2016, 1, 31)
Diter[from, to, wday: 3].each do |date|
puts date.strftime('%Y/%m/%d')
end
#=>
# 2016/01/06
# 2016/01/13
# 2016/01/20
# 2016/01/27
Development
Run rake test
to run the tests.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ojima-h/diter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.