time-helper 1.3 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +4 -0
- data/lib/time-helper.rb +35 -7
- metadata +4 -15
data/README
CHANGED
@@ -2,6 +2,7 @@ This gem adds a few methods to Time, both instance and class.
|
|
2
2
|
It's not great, but makes my life easier on a few cucumber based projects.
|
3
3
|
There's a method Time.strtotime which takes a date(time) string and turns it into a time object.
|
4
4
|
Also method add and substract to the instance. (Important, these methods do not account for leap years and months, etc, one month is 30 days and 1 year is 365 days.)
|
5
|
+
=~ to see if two Time objects are "close enough"
|
5
6
|
|
6
7
|
Usage examples:
|
7
8
|
|
@@ -41,6 +42,9 @@ Time.parse does not work well with non-American formats, if like me, you're work
|
|
41
42
|
mat like: DD/MM/YYYY Time.parse will not do. In fact, if you rewrite strtotime so it just calls parse a few of the tests
|
42
43
|
included will fail.
|
43
44
|
|
45
|
+
New in 1.3.1
|
46
|
+
Added =~ method which allows you to see if two time objects are close enough, by default it compares up till the hour, for instance, a =~ b will be true if a and b are both objects with the same year, month, day and hour.
|
47
|
+
Removed all the aborts! Replaced be real errors inside the class! Sorry for the mess.
|
44
48
|
|
45
49
|
New in 1.3:
|
46
50
|
Added support for time strings in the format you'd usualy pass to Time.parse
|
data/lib/time-helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
#encoding: utf-8
|
2
|
+
#@version 1.3.1
|
2
3
|
#@author Arthur
|
3
4
|
class Time
|
4
5
|
require 'time'
|
@@ -92,7 +93,7 @@ class Time
|
|
92
93
|
separators = string.match( /\d{2,4}(.)?\d{2}(.)?\d{2,4}/ )
|
93
94
|
return nil unless separators[1]
|
94
95
|
|
95
|
-
|
96
|
+
raise GenericInputError.new 'separators must be equal' unless separators[2] == separators[1]
|
96
97
|
return separators[2]
|
97
98
|
end
|
98
99
|
#@param string [String] datetime string
|
@@ -108,7 +109,7 @@ class Time
|
|
108
109
|
return nil if string.match /\d{14}/
|
109
110
|
separators = string.match(/(.)\d{2}:\d{2}:\d{2}$/)
|
110
111
|
return separators[1] if separators[1]
|
111
|
-
|
112
|
+
raise GenericInputError.new 'could not find date time separator'
|
112
113
|
end
|
113
114
|
#see Time#get_order
|
114
115
|
def get_dt_order string
|
@@ -132,7 +133,7 @@ class Time
|
|
132
133
|
elsif year.match( dy ) and day.match( yd )
|
133
134
|
return :ymd
|
134
135
|
else
|
135
|
-
|
136
|
+
raise GenericInputError.new 'can\'t find order'
|
136
137
|
end
|
137
138
|
else
|
138
139
|
return no_separator_order string
|
@@ -150,7 +151,7 @@ class Time
|
|
150
151
|
elsif string[0..3].to_i >= 1970 and string[4..5].to_i and string[6..7].to_i <= 31
|
151
152
|
return :ymd
|
152
153
|
end
|
153
|
-
|
154
|
+
raise GenericInputError.new 'can\'t find order'
|
154
155
|
end
|
155
156
|
end
|
156
157
|
|
@@ -196,6 +197,33 @@ class Time
|
|
196
197
|
return seconds
|
197
198
|
end
|
198
199
|
|
199
|
-
|
200
|
-
|
200
|
+
public
|
201
|
+
#compares if two Time objects are close enough to each other
|
202
|
+
#@param other_time [Time] time to be compared to
|
203
|
+
#@param smallest_unit [Symbol] smallest unit of time to check if it's the same
|
204
|
+
# @example
|
205
|
+
# a = Time.now
|
206
|
+
# b = Time.now.add(:day = 1)
|
207
|
+
# a =~ b
|
208
|
+
# => false
|
209
|
+
# a.=~ b,:month
|
210
|
+
# => true
|
211
|
+
#@note to compare up to the hour you just need var =~ other_var,
|
212
|
+
# but to pass the second argument you must use var.=~(other_var, :min)
|
213
|
+
#@note minute is :min seconds is :sec
|
214
|
+
def =~(other_time, smallest_unit = :hour)
|
215
|
+
raise InvalidArgument.new('Argument must be an instance of Time') unless other_time.class == Time
|
216
|
+
equal = true
|
217
|
+
[:year, :month, :day, :hour, :min, :sec].each do |method|
|
218
|
+
self_unit, other_unit = self.method(method).call(), other_time.method(method).call()
|
219
|
+
equal = false unless self_unit == other_unit
|
220
|
+
break if method == smallest_unit
|
221
|
+
end
|
222
|
+
return equal
|
223
|
+
end
|
224
|
+
|
225
|
+
##### Errors #####
|
226
|
+
class GenericInputError < StandardError; end
|
227
|
+
class InvalidArgument < GenericInputError; end
|
228
|
+
class InvalidTimeStringFormat < GenericInputError; end
|
201
229
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Silva
|
@@ -9,22 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-20 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: " Adds a few methods to the Time class, more significant:\n\n strtotime which returns a Time object based on a string.\n\n also methods add and substract that allow you to do some easy date math (doesn't take leap months/years into account)\n also tomorrow and yesterday methods witch are like Time.now +- 1 day\n\n\
|
17
|
-
**********************************************\n\
|
18
|
-
*How is this diferent than just \"Time.parse\"?*\n\
|
19
|
-
**********************************************\n\n\
|
20
|
-
Time.parse does not work well with non-American formats, if like me, you're working with strings that have dates in a format like: DD/MM/YYYY Time.parse will not do. In fact, if you rewrite strtotime so it just calls parse a few of the tests included will fail.\n\n\n\
|
21
|
-
New in 1.3:\n\
|
22
|
-
Added support for time strings in the format you'd usualy pass to Time.parse\n\
|
23
|
-
Added utc_parse method for the lazy, it parses the date and converts it to utc\n\n\
|
24
|
-
New in 1.2\n\
|
25
|
-
Tomorrow and Yesterday methods\n\n\
|
26
|
-
New in 1.1 \n\
|
27
|
-
I skiped this version.... because I suck at versioning \n\n"
|
16
|
+
description: " Adds a few methods to the Time class, more significant:\n\n strtotime which returns a Time object based on a string.\n\n also methods add and substract that allow you to do some easy date math (doesn't take leap months/years into account)\n also tomorrow and yesterday methods witch are like Time.now +- 1 day\n\n =~ to see if two Time objects are \"close enough\"\n"
|
28
17
|
email: awls99@gmail.com
|
29
18
|
executables: []
|
30
19
|
|
@@ -62,6 +51,6 @@ rubyforge_project:
|
|
62
51
|
rubygems_version: 1.3.5
|
63
52
|
signing_key:
|
64
53
|
specification_version: 3
|
65
|
-
summary: A few helper functions for the Time class
|
54
|
+
summary: A few helper functions for the Time class.
|
66
55
|
test_files: []
|
67
56
|
|