Skip to content

Instantly share code, notes, and snippets.

@jewel12
Created March 13, 2014 18:31
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 jewel12/9534063 to your computer and use it in GitHub Desktop.
Save jewel12/9534063 to your computer and use it in GitHub Desktop.
1時間単位で開始時間と終了時間が与えられたとき、全ての時間を1時間単位で小さいほうから列挙するリストを返す関数
# -*- coding: utf-8 -*-
# https://twitter.com/katzchang/status/444136941605224448
# is(hoge('2014-01-01 01:00', '2014-01-01 03:00'), ('2014-01-01 01:00', '2014-01-01 02:00', '2014-01-01 03:00')) より
# 1時間単位で開始時間と終了時間が与えられたとき => YYYY-mm-dd HH:00 が入力されるものだと予想
# 寝る
require 'time'
module RefinedTime
refine Time do
def to_top_of_the_time
Time.new(year, month, day, hour)
end
end
end
using RefinedTime
def 1時間単位で開始時間と終了時間が与えられたとき、全ての時間を1時間単位で小さいほうから列挙するリストを返す関数(start_time_str, end_time_str)
st = Time.parse(start_time_str).to_top_of_the_time.to_i
et = Time.parse(end_time_str).to_top_of_the_time.to_i
(st..et).step(3600).map {|t| Time.at(t).strftime('%Y-%m-%d %H:%M') }
end
require 'minitest/spec'
require 'minitest/autorun'
describe RefinedTime do
describe '#to_top_of_the_time' do
describe '時間が02:12だったとき' do
before {@d = Time.parse('2014-01-01 02:12')}
it '2時ちょうどのTimeが取得できる' do
assert_equal '2014-01-01 02:00:00', @d.to_top_of_the_time.strftime('%Y-%m-%d %T')
end
end
end
end
describe 'a func' do
describe '1時間単位で開始時間と終了時間が与えられたとき' do
it '全ての時間を1時間単位で小さいほうから列挙するリストを返す' do
expected = ['2014-01-01 01:00', '2014-01-01 02:00', '2014-01-01 03:00']
assert_equal expected, 1時間単位で開始時間と終了時間が与えられたとき、全ての時間を1時間単位で小さいほうから列挙するリストを返す関数('2014-01-01 01:00', '2014-01-01 03:00')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment