time-helper 1.0

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.
Files changed (2) hide show
  1. data/lib/time-helper.rb +161 -0
  2. metadata +60 -0
@@ -0,0 +1,161 @@
1
+ #@version 1.0
2
+ #@author Arthur
3
+ class Time
4
+ #class methods
5
+ class << self
6
+ def strtotime string
7
+ abort 'invalid date' unless valid_datetime? string
8
+ h,m,s = 0,0,0
9
+
10
+ if date_or_dt( string ) == :datetime
11
+ separator = dt_get_separator string
12
+ date, time = 0, 0
13
+ if separator
14
+ date, time = string.split( separator )
15
+ else
16
+ date, time = string[0..7], string[8..13]
17
+ end
18
+ h,m,s = get_time_array time
19
+ string = date
20
+ end
21
+ separator = get_separator string
22
+ order = get_order string
23
+ if separator
24
+ dy, mo, yd = string.split( separator )
25
+ if order == :dmy
26
+ return Time.local( yd, mo, dy,h,m,s )
27
+ elsif order == :ymd
28
+ return Time.local( dy, mo, yd,h,m,s)
29
+ end
30
+ end
31
+ if order == :dmy
32
+ return Time.local( string[4..7].to_i, string[2..3].to_i, string[0..1].to_i,h,m,s )
33
+ elsif order == :ymd
34
+ return Time.local( string[0..3].to_i, string[4..5].to_i, string[6..7].to_i,h,m,s )
35
+ end
36
+
37
+ end
38
+
39
+ def valid_datetime? string
40
+ return false unless string.match /\d{2,4}.?\d{2}.?\d{2,4}(?:.?\d{2}:?\d{2}:?\d{2})?/
41
+ return true
42
+ end
43
+
44
+ private
45
+ def get_time_array time
46
+ h,m,s = 0,0,0
47
+ if time.match /\d{6}/
48
+ h,m,s = time[0..1], time[2..3], time[4..5]
49
+ else
50
+ h,m,s = time.split(':')
51
+ end
52
+ return h,m,s
53
+ end
54
+ #@return [Symbol] if string is date or datetime
55
+ def date_or_dt string
56
+ #case there's no separators are the easiest to handle
57
+ return :datetime if string.match /\d{14}/
58
+ return :date if string.match /\d{8}/
59
+ #now those are out of the way it's easier...
60
+ return :datetime if string.match /^\d{2,4}(?:.\d{2,4}){5}$/
61
+ #we assume the string is valid here...
62
+ return :date
63
+ end
64
+ #@param string [String] date string
65
+ #@return [String|Nil] separator
66
+ def get_separator string
67
+ separators = string.match( /\d{2,4}(.)?\d{2}(.)?\d{2,4}/ )
68
+ return nil unless separators[1]
69
+
70
+ abort 'separators must be equal' unless separators[2] == separators[1]
71
+ return separators[2]
72
+ end
73
+ #@param string [String] datetime string
74
+ #@return [String] separator between date and time
75
+ def dt_get_separator string
76
+ #trying most commong: space
77
+ separator = ' '
78
+ date, time = string.split separator
79
+ if date and time
80
+ return separator
81
+ end
82
+ #if there's no separator...
83
+ return nil if string.match /\d{14}/
84
+ separators = string.match(/(.)\d{2}:\d{2}:\d{2}$/)
85
+ return separators[1] if separators[1]
86
+ abort 'could not find date time separator'
87
+ end
88
+ #see Time#get_order
89
+ def get_dt_order string
90
+ separator = dt_get_separator string
91
+ if separator
92
+ return get_order( string.split( separator )[0] )
93
+ end
94
+ return get_order( string[0..7] )
95
+ end
96
+
97
+ #@param string [String] date string
98
+ #@return [symbol] order dmy or ymd
99
+ def get_order string
100
+ separator = get_separator string
101
+ day = Regexp.new('^([012]\d)|(3[01])$')
102
+ year = Regexp.new('^\d{4}$')
103
+ if separator
104
+ dy, m, yd = string.split( separator )
105
+ if day.match( dy ) and year.match( yd )
106
+ return :dmy
107
+ elsif year.match( dy ) and day.match( yd )
108
+ return :ymd
109
+ else
110
+ abort 'can\'t find order'
111
+ end
112
+ else
113
+ return no_separator_order string
114
+ end
115
+ end
116
+
117
+ #@param string [String] date string
118
+ #@return [symbol] order dmy or ymd
119
+ #@note called when no separator is found on string
120
+ #@note might not work well for years < 1970 or > 2020
121
+ #@note experimental
122
+ def no_separator_order string
123
+ if string[0..1].to_i <= 31 and string[2..3].to_i <= 12 and string[4..7].to_i >= 1970
124
+ return :dmy
125
+ elsif string[0..3].to_i >= 1970 and string[4..5].to_i and string[6..7].to_i <= 31
126
+ return :ymd
127
+ end
128
+ abort 'can\'t find order'
129
+ end
130
+ end
131
+
132
+ #instance methods
133
+ def add params
134
+ seconds = get_seconds params
135
+ return self + seconds
136
+ end
137
+ def substract params
138
+ seconds = get_seconds params
139
+ return self - seconds
140
+ end
141
+
142
+ private
143
+ #@note may not account for leap years etc
144
+ def get_seconds params
145
+ seconds = 0
146
+ times = {
147
+ :year => 60*60*24*365,
148
+ :month => 60*60*24*30,
149
+ :day => 60*60*24,
150
+ :minute => 60,
151
+ :hour => 60*60,
152
+ :second => 1,
153
+ :week => 60*60*24*7,
154
+ }
155
+ params.each do |unit, quantity|
156
+ seconds += quantity * times[ unit ]
157
+ end
158
+ return seconds
159
+ end
160
+
161
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2012-05-02 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |-
17
+ Adds a few methods to the Time class, more significant:
18
+
19
+ strtotime which returns a Time object based on a string.
20
+
21
+ also methods add and substract that allow you to do some easy date math (doesn't take leap months/years into account)
22
+ email: awls99@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/time-helper.rb
31
+ has_rdoc: true
32
+ homepage: https://github.com/awls99/time-helper
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A few helper functions for the Time class, useful on ruby 1.8
59
+ test_files: []
60
+