File: test-date-time.rb

package info (click to toggle)
ruby-gnome 4.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 26,648 kB
  • sloc: ruby: 67,701; ansic: 67,431; xml: 350; sh: 201; cpp: 45; makefile: 42
file content (126 lines) | stat: -rw-r--r-- 5,097 bytes parent folder | download
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright (C) 2016-2022  Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class TestDateTime < Test::Unit::TestCase
  include GLibTestUtils

  def test_now_local
    now = GLib::DateTime.now(:local)
    format = "%Y-%m-%d-%H-%M"
    assert_equal(Time.now.strftime(format), now.format(format))
  end

  sub_test_case "new" do
    test "unix: :local" do
      time = Time.now
      format = "%Y-%m-%d-%H-%M"
      datetime = GLib::DateTime.new(:unix => time.to_i,
                                    :timezone => :local)
      assert_equal(time.strftime(format), datetime.format(format))
    end

    test "unix: :utc" do
      time = Time.now.utc
      format = "%Y-%m-%d-%H-%M"
      datetime = GLib::DateTime.new(:unix => time.to_i,
                                    :timezone => :utc)
      assert_equal(time.strftime(format), datetime.format(format))
    end

    test "timezone: :local" do
      time = Time.now
      datetime = GLib::DateTime.new(:timezone => :local,
                                    :year => time.year,
                                    :month => time.month,
                                    :day => time.day,
                                    :hour => time.hour,
                                    :minute => time.min,
                                    :second => time.sec)
      assert_equal(time.year, datetime.year)
      assert_equal(time.month, datetime.month)
      assert_equal(time.day, datetime.day_of_month)
      assert_equal(time.hour, datetime.hour)
      assert_equal(time.min, datetime.minute)
      assert_equal(time.sec, datetime.second)
    end

    test "timezone: :utc" do
      time = Time.now.utc
      datetime = GLib::DateTime.new(:timezone => :utc,
                                    :year => time.year,
                                    :month => time.month,
                                    :day => time.day,
                                    :hour => time.hour,
                                    :minute => time.min,
                                    :second => time.sec)
      assert_equal(time.year, datetime.year)
      assert_equal(time.month, datetime.month)
      assert_equal(time.day, datetime.day_of_month)
      assert_equal(time.hour, datetime.hour)
      assert_equal(time.min, datetime.minute)
      assert_equal(time.sec, datetime.second)
    end

    test "timezone: local time zone" do
      time = Time.now
      tz = GLib::TimeZone.local
      datetime = GLib::DateTime.new(:timezone => tz,
                                    :year => time.year,
                                    :month => time.month,
                                    :day => time.day,
                                    :hour => time.hour,
                                    :minute => time.min,
                                    :second => time.sec)
      assert_equal(time.year, datetime.year)
      assert_equal(time.month, datetime.month)
      assert_equal(time.day, datetime.day_of_month)
      assert_equal(time.hour, datetime.hour)
      assert_equal(time.min, datetime.minute)
      assert_equal(time.sec, datetime.second)
    end

    test "timezone: UTC time zone" do
      time = Time.now.utc
      tz = GLib::TimeZone.utc
      datetime = GLib::DateTime.new(:timezone => tz,
                                    :year => time.year,
                                    :month => time.month,
                                    :day => time.day,
                                    :hour => time.hour,
                                    :minute => time.min,
                                    :second => time.sec)
      assert_equal(time.year, datetime.year)
      assert_equal(time.month, datetime.month)
      assert_equal(time.day, datetime.day_of_month)
      assert_equal(time.hour, datetime.hour)
      assert_equal(time.min, datetime.minute)
      assert_equal(time.sec, datetime.second)
    end
  end

  def test_format_iso8601
    only_glib_version(2, 66, 0)
    datetime = GLib::DateTime.new(year: 2022,
                                  month: 7,
                                  day: 18,
                                  hour: 23,
                                  minute: 24,
                                  second: 4.9,
                                  timezone: GLib::TimeZone.utc)
    assert_equal("2022-07-18T23:24:04.900000Z",
                 datetime.format_iso8601)
  end
end