summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/helpers/datetime.rb
blob: 4cb57bdaa16de44572c43153c82e5435c4d8b284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Object
  # The new_datetime helper makes writing DateTime specs more simple by
  # providing default constructor values and accepting a Hash of only the
  # constructor values needed for the particular spec. For example:
  #
  #   new_datetime :hour => 1, :minute => 20
  #
  # Possible keys are:
  #   :year, :month, :day, :hour, :minute, :second, :offset and :sg.

  def new_datetime(opts={})
    require 'date'

    value = {
      :year   => -4712,
      :month  => 1,
      :day    => 1,
      :hour   => 0,
      :minute => 0,
      :second => 0,
      :offset => 0,
      :sg     => Date::ITALY
    }.merge opts

    DateTime.new value[:year], value[:month], value[:day], value[:hour],
      value[:minute], value[:second], value[:offset], value[:sg]
  end

  def with_timezone(name, offset = nil, daylight_saving_zone = "")
    zone = name.dup

    if offset
      # TZ convention is backwards
      offset = -offset

      zone += offset.to_s
      zone += ":00:00"
    end
    zone += daylight_saving_zone

    old = ENV["TZ"]
    ENV["TZ"] = zone

    begin
      yield
    ensure
      ENV["TZ"] = old
    end
  end

end