Repository navigation
Expand file tree
/
Copy pathDate.rb
More file actions
104 lines (89 loc) · 3.2 KB
/
Copy pathDate.rb
File metadata and controls
104 lines (89 loc) · 3.2 KB
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
# -*- coding: utf-8 -*-
# The Date class is used to contain the data and time data (relies on the Ruby-core class Time)
# Altough not strictly enforced (def freeze in Ruby is awkward) instances of
# this class should be treated as immutable
class Date
attr_reader :date
# date : The date argument is compulsory e.g.: "2011-09-23"
# time : A time argument can be provided or is specified as nil e.g.: "08:49"
def initialize(date, time)
raise "Missing date" if date.nil? # date is compulsory
splitData = date.split(/-/)
if (time.nil?)
@date = Time.new(splitData[0], splitData[1], splitData[2], 0, 0, 0)
else
timeWrap = MyTime.new(time)
@date = Time.new(splitData[0], splitData[1], splitData[2], timeWrap.seconds/3600, timeWrap.seconds%3600/60)
end
end
# Add the given time to the date
# the given time added to the current time gets returned
# time : a string following the previously specified
# syntax or an object of class MyTime
def addTimeToDate(time)
if (!time.class.to_s.eql?("MyTime"))
time = MyTime.new(time)
end
@date += time.seconds
end
def isSameDay(givenDate)
return to_s.eql?(givenDate.to_s)
end
# Compares the date represented by 'this' with the date
# specified by the parameters
# givenDate : a string following the previously specified syntax for dates,
# or an instance of class Date (in which case givenTime should be nil).
# givenTime : a string following the previously specified syntax or nil
def compare(givenDate, givenTime)
if (givenDate.class.to_s.eql?("Date") && givenTime == nil)
return date <=> givenDate.date
else
return date <=> Date.new(givenDate, givenTime).date
end
end
# provides a string representation of the time associated with this date
# following the syntax: "HH:MM"
def time_to_s
return (date.to_s.split(/ /)[1])[0,5]
end
# provides a string representation of the date
# following the syntax: "YYYY-MM-DD"
def to_s
return date.to_s.split(/ /)[0]
end
end
#-------------------------------------------------------------------------------
# The MyTime class is used to contain the time data
class MyTime
attr_reader :seconds
# time : the time argument following the previously defined syntax e.g.: "10:30"
def initialize(time)
if (time.class.to_s.eql?("String"))
splitTime = time.split(/:/)
hours = splitTime[0]
minutes = splitTime[1]
@seconds = hours.to_i*3600 + minutes.to_i*60
end
end
# provides a string representation of this time objectµ
# following the syntax "HH:MM"
def to_s
return (seconds/3600).to_s + ":" + ((seconds%3600)/60).to_s
end
end
#-SOME-TESTS--------------------------------------------------------------------
#test1 = Date.new("2011-03-12", "10:30")
#test2 = Date.new("2011-03-11", nil)
#puts test1.date
#puts test2.date
##puts test1.addTimeToDate("17:55")
#timeToAdd = MyTime.new("17:55")
##puts test1.addTimeToDate(timeToAdd)
#puts test1.compare("2011-03-12", "10:29")
#puts test2.compare(test1, nil)
#puts timeToAdd
#puts test1
#puts test1.isSameDay(test2)
#puts test1.time_to_s
#puts test1.to_s
#-------------------------------------------------------------------------------