time-helper 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +21 -0
- data/lib/time-helper.rb +32 -17
- metadata +14 -3
data/README
CHANGED
@@ -31,3 +31,24 @@ using .add or .substract
|
|
31
31
|
| Time.local(2011,12,24,22,10,54) | add | {:year => 2, :month => 1 } | Time.local(2011,12,24,22,10,54) + (2*60*60*24*365 + 60*60*24*30) |
|
32
32
|
| 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)|
|
33
33
|
| 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) |
|
34
|
+
|
35
|
+
|
36
|
+
**********************************************
|
37
|
+
*How is this diferent than just "Time.parse"?*
|
38
|
+
**********************************************
|
39
|
+
|
40
|
+
Time.parse does not work well with non-American formats, if like me, you're working with strings that have dates in a for
|
41
|
+
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
|
+
included will fail.
|
43
|
+
|
44
|
+
|
45
|
+
New in 1.3:
|
46
|
+
Added support for time strings in the format you'd usualy pass to Time.parse
|
47
|
+
Added utc_parse method for the lazy, it parses the date and converts it to utc
|
48
|
+
|
49
|
+
New in 1.2
|
50
|
+
Tomorrow and Yesterday methods
|
51
|
+
|
52
|
+
New in 1.1
|
53
|
+
I skiped this version.... because I suck at versioning
|
54
|
+
|
data/lib/time-helper.rb
CHANGED
@@ -1,26 +1,33 @@
|
|
1
|
-
#@version 1.
|
1
|
+
#@version 1.3.0
|
2
2
|
#@author Arthur
|
3
3
|
class Time
|
4
|
+
require 'time'
|
4
5
|
#class methods
|
5
6
|
class << self
|
6
|
-
#@param string [String] a datetime string with many possible formats
|
7
|
+
#@param string [String] a datetime string with many possible formats
|
7
8
|
#@see Time#valid_datetime? or the feature file for valid format
|
8
9
|
#@return [Time] set in the date of the string passed
|
9
10
|
def strtotime string
|
10
|
-
|
11
|
+
unless valid_datetime? string
|
12
|
+
begin
|
13
|
+
return Time.parse(string)
|
14
|
+
rescue
|
15
|
+
raise InvalidTimeStringFormat
|
16
|
+
end
|
17
|
+
end
|
11
18
|
h,m,s = 0,0,0
|
12
19
|
|
13
20
|
if date_or_dt( string ) == :datetime
|
14
21
|
separator = dt_get_separator string
|
15
22
|
date, time = 0, 0
|
16
|
-
if separator
|
23
|
+
if separator
|
17
24
|
date, time = string.split( separator )
|
18
25
|
else
|
19
26
|
date, time = string[0..7], string[8..13]
|
20
27
|
end
|
21
28
|
h,m,s = get_time_array time
|
22
29
|
string = date
|
23
|
-
end
|
30
|
+
end
|
24
31
|
separator = get_separator string
|
25
32
|
order = get_order string
|
26
33
|
if separator
|
@@ -28,7 +35,7 @@ class Time
|
|
28
35
|
if order == :dmy
|
29
36
|
return Time.local( yd, mo, dy,h,m,s )
|
30
37
|
elsif order == :ymd
|
31
|
-
return Time.local( dy, mo, yd,h,m,s)
|
38
|
+
return Time.local( dy, mo, yd,h,m,s)
|
32
39
|
end
|
33
40
|
end
|
34
41
|
if order == :dmy
|
@@ -36,12 +43,12 @@ class Time
|
|
36
43
|
elsif order == :ymd
|
37
44
|
return Time.local( string[0..3].to_i, string[4..5].to_i, string[6..7].to_i,h,m,s )
|
38
45
|
end
|
39
|
-
|
46
|
+
|
40
47
|
end
|
41
48
|
#@param string [String] datetime string
|
42
|
-
#@return [Boolean]
|
49
|
+
#@return [Boolean]
|
43
50
|
def valid_datetime? string
|
44
|
-
return false unless string.match
|
51
|
+
return false unless string.match /^\d{2,4}.?\d{2}.?\d{2,4}(?:.?\d{2}:?\d{2}:?\d{2})?$/
|
45
52
|
return true
|
46
53
|
end
|
47
54
|
#@return [Time] Time object set 24 hours ago
|
@@ -52,6 +59,13 @@ class Time
|
|
52
59
|
def yesterday
|
53
60
|
return Time.now.substract(:day => 1 )
|
54
61
|
end
|
62
|
+
#Converts a Date in to utc format
|
63
|
+
#
|
64
|
+
#@param value [String]
|
65
|
+
#@return [Time] in UTC
|
66
|
+
def utc_parse value
|
67
|
+
Time.strtotime(value).utc
|
68
|
+
end
|
55
69
|
private
|
56
70
|
def get_time_array time
|
57
71
|
h,m,s = 0,0,0
|
@@ -99,12 +113,12 @@ class Time
|
|
99
113
|
#see Time#get_order
|
100
114
|
def get_dt_order string
|
101
115
|
separator = dt_get_separator string
|
102
|
-
if separator
|
116
|
+
if separator
|
103
117
|
return get_order( string.split( separator )[0] )
|
104
118
|
end
|
105
119
|
return get_order( string[0..7] )
|
106
120
|
end
|
107
|
-
|
121
|
+
|
108
122
|
#@param string [String] date string
|
109
123
|
#@return [symbol] order dmy or ymd
|
110
124
|
def get_order string
|
@@ -124,14 +138,14 @@ class Time
|
|
124
138
|
return no_separator_order string
|
125
139
|
end
|
126
140
|
end
|
127
|
-
|
141
|
+
|
128
142
|
#@param string [String] date string
|
129
143
|
#@return [symbol] order dmy or ymd
|
130
144
|
#@note called when no separator is found on string
|
131
145
|
#@note might not work well for years < 1970 or > 2020
|
132
146
|
#@note experimental
|
133
147
|
def no_separator_order string
|
134
|
-
if string[0..1].to_i <= 31 and string[2..3].to_i <= 12 and string[4..7].to_i >= 1970
|
148
|
+
if string[0..1].to_i <= 31 and string[2..3].to_i <= 12 and string[4..7].to_i >= 1970
|
135
149
|
return :dmy
|
136
150
|
elsif string[0..3].to_i >= 1970 and string[4..5].to_i and string[6..7].to_i <= 31
|
137
151
|
return :ymd
|
@@ -141,11 +155,11 @@ class Time
|
|
141
155
|
end
|
142
156
|
|
143
157
|
#@param params [Hash] hash of time to be added {:time => amount } (can take several at once)
|
144
|
-
#@return [Time] new time object with date set to the result
|
158
|
+
#@return [Time] new time object with date set to the result
|
145
159
|
# @example add time
|
146
160
|
# t = Time.now()
|
147
161
|
# t.add(:year => 1 ) #=> a time object set for 365 days from now
|
148
|
-
# t.add(:year => 2, :day => 1 ) #=> a time object set 731 days from now
|
162
|
+
# t.add(:year => 2, :day => 1 ) #=> a time object set 731 days from now
|
149
163
|
# accepts :year :month :day :minute :hour :second :week
|
150
164
|
def add params
|
151
165
|
seconds = get_seconds params
|
@@ -153,7 +167,7 @@ class Time
|
|
153
167
|
end
|
154
168
|
|
155
169
|
#@param params [Hash] hash of time to be added {:time => amount } (can take several at once)
|
156
|
-
#@return [Time] new time object with date set to the result
|
170
|
+
#@return [Time] new time object with date set to the result
|
157
171
|
# @example substract time
|
158
172
|
# t = Time.now()
|
159
173
|
# t.substract(:year => 1 ) returns a time object set for 365 days ago
|
@@ -163,7 +177,6 @@ class Time
|
|
163
177
|
seconds = get_seconds params
|
164
178
|
return self - seconds
|
165
179
|
end
|
166
|
-
|
167
180
|
private
|
168
181
|
#@note may not account for leap years etc
|
169
182
|
def get_seconds params
|
@@ -184,3 +197,5 @@ class Time
|
|
184
197
|
end
|
185
198
|
|
186
199
|
end
|
200
|
+
class InvalidTimeStringFormat < StandardError
|
201
|
+
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: "1.
|
4
|
+
version: "1.3"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Silva
|
@@ -9,11 +9,22 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-30 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
|
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"
|
17
28
|
email: awls99@gmail.com
|
18
29
|
executables: []
|
19
30
|
|