Skip to content

Instantly share code, notes, and snippets.

@juno
Created July 23, 2010 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juno/487066 to your computer and use it in GitHub Desktop.
Save juno/487066 to your computer and use it in GitHub Desktop.
require 'ice_cube'
require 'active_support'
include IceCube
# 開始日を指定してScheduleインスタンスを生成
schedule = Schedule.new(Time.utc(2010, 7, 1))
# 2010年7月の5の付く日をスケジュールに追加
(5..30).step(5) do |day|
schedule.add_recurrence_date(Time.utc(2010, 7, day))
end
# スケジュールに登録された日付をすべて取得
schedule.all_occurrences
# => [2010-07-05 00:00:00 +0900,
# 2010-07-10 00:00:00 +0900,
# 2010-07-15 00:00:00 +0900,
# 2010-07-20 00:00:00 +0900,
# 2010-07-25 00:00:00 +0900,
# 2010-07-30 00:00:00 +0900]
# スケジュールから除外する日付を追加
schedule.add_exception_date(Time.utc(2010, 7, 25))
# スケジュールに登録された日付をすべて取得
schedule.all_occurrences
# => [2010-07-05 00:00:00 +0900,
# 2010-07-10 00:00:00 +0900,
# 2010-07-15 00:00:00 +0900,
# 2010-07-20 00:00:00 +0900,
# 2010-07-30 00:00:00 +0900]
# 特定の日付がスケジュールに含まれているかどうか
schedule.occurs_on?(Date.new(2010, 7, 1)) # => false
schedule.occurs_on?(Date.new(2010, 7, 5)) # => true
# 最初のn件を取得
schedule.first(2)
# => [2010-07-05 00:00:00 +0900,
# 2010-07-10 00:00:00 +0900]
require 'ice_cube'
require 'active_support'
include IceCube
# 開始日
start_time = Time.utc(2010, 7, 1)
# 「毎日」というルールで、2010/07/01から2010/07/05の間の該当日時を取得する
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.daily)
schedule.occurrences_between(start_time, start_time + ONE_DAY * 4)
# => [2010-07-01 00:00:00 UTC,
# 2010-07-02 00:00:00 UTC,
# 2010-07-03 00:00:00 UTC,
# 2010-07-04 00:00:00 UTC,
# 2010-07-05 00:00:00 UTC]
# 「隔週の月曜日と火曜日」というルールで、2010/07/01から2010/07/31の間の該当日時を取得する
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.weekly(2).day(:monday, :tuesday))
schedule.occurrences(Time.utc(2010, 7, 31))
# => [2010-07-12 00:00:00 UTC,
# 2010-07-13 00:00:00 UTC,
# 2010-07-26 00:00:00 UTC,
# 2010-07-27 00:00:00 UTC]
# 「毎月1日と最終日」というルールで、2010/07/01から2010/08/31の間の該当日時を取得する
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.monthly.day_of_month(1, -1))
schedule.occurrences(Time.utc(2010, 8, 31))
# => [2010-07-01 00:00:00 UTC,
# 2010-07-31 00:00:00 UTC,
# 2010-08-01 00:00:00 UTC,
# 2010-08-31 00:00:00 UTC]
# 「毎月第1と最終の火曜日」というルールで、2010/07/01から2010/08/31の間の該当日時を取得する
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.monthly.day_of_week(:tuesday => [1, -1]))
schedule.occurrences(Time.utc(2010, 8, 31))
# => [2010-07-06 00:00:00 UTC,
# 2010-07-27 00:00:00 UTC,
# 2010-08-03 00:00:00 UTC,
# 2010-08-31 00:00:00 UTC]
# 「2時間おき」というルールで、2010/07/01 00:00:00からの最初の3件を取得する
schedule = Schedule.new(start_time)
schedule.add_recurrence_rule(Rule.hourly(2))
schedule.first(3)
# => [2010-07-01 00:00:00 UTC,
# 2010-07-01 02:00:00 UTC,
# 2010-07-01 04:00:00 UTC]
yaml = schedule.to_yaml
IceCube::Schedule.from_yaml(yaml)
hash = schedule.to_hash
IceCube::Schedule.from_hash(hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment