time-helper 1.3.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +9 -0
- data/lib/time-helper.rb +8 -17
- metadata +17 -17
data/README
CHANGED
@@ -33,6 +33,10 @@ using .add or .substract
|
|
33
33
|
| Time.local(2012,05,01,12,20,31) | substract | {:month => 2, :day => 3} | Time.local(2012,05,01,12,20,31) - (60*60*24*30*2 + 60*60*24*3)|
|
34
34
|
| Time.local(2011,12,24,22,10,54) | add | {:day => 1, :hour => 3} |Time.local(2011,12,24,22,10,54) + (60*60*24 + 60*60*3) |
|
35
35
|
|
36
|
+
using =~
|
37
|
+
this method allows you to get "close enough" times, defaults to 300 seconds (5 minutes) of acceptable difference
|
38
|
+
time_obj =~ other_time # true or false if the difference between the objects is less than 5 minutes
|
39
|
+
time_obj.=~ other_time, 360 # true or false if the difference is less than 6 minutes
|
36
40
|
|
37
41
|
**********************************************
|
38
42
|
*How is this diferent than just "Time.parse"?*
|
@@ -42,6 +46,11 @@ Time.parse does not work well with non-American formats, if like me, you're work
|
|
42
46
|
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
|
43
47
|
included will fail.
|
44
48
|
|
49
|
+
New in 2.0.0
|
50
|
+
Completelly changed =~ method, the second argument is now an acceptable difference in seconds - more reliable.
|
51
|
+
Removed the require 'time' std lib (which should reduce memory of this gem ) and as such it no longers has the parse method included in the strtotime method, if you want to use parse, use it!
|
52
|
+
As such, with these differences this version is not retro-compatible, if you were using a previous version it may fail on this one.
|
53
|
+
|
45
54
|
New in 1.3.1
|
46
55
|
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
56
|
Removed all the aborts! Replaced be real errors inside the class! Sorry for the mess.
|
data/lib/time-helper.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
#encoding: utf-8
|
2
|
-
#@version
|
2
|
+
#@version 2.0.0
|
3
3
|
#@author Arthur
|
4
4
|
class Time
|
5
|
-
require 'time'
|
6
5
|
#class methods
|
7
6
|
class << self
|
8
7
|
#@param string [String] a datetime string with many possible formats
|
@@ -10,11 +9,7 @@ class Time
|
|
10
9
|
#@return [Time] set in the date of the string passed
|
11
10
|
def strtotime string
|
12
11
|
unless valid_datetime? string
|
13
|
-
|
14
|
-
return Time.parse(string)
|
15
|
-
rescue
|
16
|
-
raise InvalidTimeStringFormat
|
17
|
-
end
|
12
|
+
raise InvalidTimeStringFormat
|
18
13
|
end
|
19
14
|
h,m,s = 0,0,0
|
20
15
|
|
@@ -200,26 +195,22 @@ class Time
|
|
200
195
|
public
|
201
196
|
#compares if two Time objects are close enough to each other
|
202
197
|
#@param other_time [Time] time to be compared to
|
203
|
-
#@param
|
198
|
+
#@param acceptable_diff [Fixnum]
|
204
199
|
# @example
|
205
200
|
# a = Time.now
|
206
201
|
# b = Time.now.add(:day = 1)
|
207
202
|
# a =~ b
|
208
203
|
# => false
|
209
|
-
# a.=~ b
|
204
|
+
# a.=~ b, 86400
|
210
205
|
# => true
|
211
206
|
#@note to compare up to the hour you just need var =~ other_var,
|
212
207
|
# but to pass the second argument you must use var.=~(other_var, :min)
|
213
208
|
#@note minute is :min seconds is :sec
|
214
|
-
def =~(other_time,
|
209
|
+
def =~(other_time, acceptable_diff = 300 )
|
215
210
|
raise InvalidArgument.new('Argument must be an instance of Time') unless other_time.class == Time
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
equal = false unless self_unit == other_unit
|
220
|
-
break if method == smallest_unit
|
221
|
-
end
|
222
|
-
return equal
|
211
|
+
raise InvalidArgument.new('Argument must be fix number') unless acceptable_diff.class == Fixnum
|
212
|
+
diff = self.to_i - other_time.to_i
|
213
|
+
return ( diff.abs <= acceptable_diff )
|
223
214
|
end
|
224
215
|
|
225
216
|
##### Errors #####
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 2.0.0
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
|
-
- Arthur Silva
|
8
|
+
- Arthur Silva
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2012-
|
13
|
-
default_executable:
|
13
|
+
date: 2012-11-04 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
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"
|
@@ -22,9 +22,8 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
-
- lib/time-helper.rb
|
26
|
-
- README
|
27
|
-
has_rdoc: true
|
25
|
+
- lib/time-helper.rb
|
26
|
+
- README
|
28
27
|
homepage: https://github.com/awls99/time-helper
|
29
28
|
licenses: []
|
30
29
|
|
@@ -32,25 +31,26 @@ post_install_message:
|
|
32
31
|
rdoc_options: []
|
33
32
|
|
34
33
|
require_paths:
|
35
|
-
- lib
|
34
|
+
- lib
|
36
35
|
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
37
|
requirements:
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
version:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
42
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
43
|
requirements:
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
version:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
48
47
|
requirements: []
|
49
48
|
|
50
49
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.
|
50
|
+
rubygems_version: 1.8.15
|
52
51
|
signing_key:
|
53
52
|
specification_version: 3
|
54
53
|
summary: A few helper functions for the Time class.
|
55
54
|
test_files: []
|
56
55
|
|
56
|
+
has_rdoc:
|