time_expr 0.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.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +11 -0
- data/Rakefile +10 -0
- data/lib/time_expr/version.rb +5 -0
- data/lib/time_expr.rb +267 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a1a7467af44f2cb7ea799d53f5f02859db1a3022314e550f3fe5555f43e21cf2
|
4
|
+
data.tar.gz: 70edf2faabce07b75e514e19368afa4fb1176b59ed057074c607eb7571e47393
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c05493e6dfeacf16a44f7c8b854b87647aa5900f6014d9502acc5475ac334900acd92d323e8c52443c29cf177afded7c4c90d135b120bc84b0ea93c20a52270
|
7
|
+
data.tar.gz: 113ae086a9e628b2055f259769f89f3cb6faea0da4cbcdcc0a59e8464888e805a4a8719ec386b4bd338b3a97d70116569ffe4fd946aed70756d9c1eb48cc24de
|
data/.standard.yml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Adam Daniels
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# TimeExpr
|
2
|
+
|
3
|
+
This is an experiment on how to manipulate `Time` in a nice manner, without
|
4
|
+
resorting to monkey-patching Fixnum.
|
5
|
+
|
6
|
+
It's incomplete, might have bugs, and only exists to document thoughts at the time.
|
7
|
+
|
8
|
+
## License
|
9
|
+
|
10
|
+
The gem is available as open source under the terms of the
|
11
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/time_expr.rb
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
module TimeExpr
|
2
|
+
class DSL
|
3
|
+
def seconds(value)
|
4
|
+
Duration::Seconds[value]
|
5
|
+
end
|
6
|
+
|
7
|
+
def minutes(value)
|
8
|
+
Duration::Minutes[value]
|
9
|
+
end
|
10
|
+
|
11
|
+
def hours(value)
|
12
|
+
Duration::Hours[value]
|
13
|
+
end
|
14
|
+
|
15
|
+
def days(value)
|
16
|
+
Duration::Days[value]
|
17
|
+
end
|
18
|
+
|
19
|
+
def weeks(value)
|
20
|
+
Duration::Weeks[value]
|
21
|
+
end
|
22
|
+
|
23
|
+
def years(value)
|
24
|
+
Duration::Years[value]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Duration
|
29
|
+
module TimeTravel
|
30
|
+
def ago
|
31
|
+
Time.now - to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def from_now
|
35
|
+
Time.now + to_i
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Coercible
|
40
|
+
def to_int
|
41
|
+
to_i
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module Adjustable
|
46
|
+
def +(other)
|
47
|
+
case other
|
48
|
+
when Integer
|
49
|
+
self.class[value + other]
|
50
|
+
when self
|
51
|
+
self.class[value + other.value]
|
52
|
+
else
|
53
|
+
Seconds[to_i + other.to_i]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def -(other)
|
58
|
+
case other
|
59
|
+
when Integer
|
60
|
+
self.class[value - other]
|
61
|
+
when self
|
62
|
+
self.class[value - other.value]
|
63
|
+
else
|
64
|
+
Seconds[to_i - other.to_i]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def *(other)
|
69
|
+
case other
|
70
|
+
when Integer
|
71
|
+
self.class[other * value]
|
72
|
+
when self
|
73
|
+
self.class[other.value * value]
|
74
|
+
else
|
75
|
+
Seconds[to_i * other.to_i]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
module Inspectable
|
81
|
+
def inspect
|
82
|
+
"#{self.class}[#{value}]"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
module Equalable
|
87
|
+
def ==(other)
|
88
|
+
self.class == other.class && value == other.value
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Seconds
|
93
|
+
include TimeTravel
|
94
|
+
include Coercible
|
95
|
+
include Adjustable
|
96
|
+
include Inspectable
|
97
|
+
include Equalable
|
98
|
+
|
99
|
+
def initialize(value)
|
100
|
+
@value = value
|
101
|
+
end
|
102
|
+
|
103
|
+
attr_reader :value
|
104
|
+
|
105
|
+
def to_i
|
106
|
+
@value
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.[](value)
|
110
|
+
new(value)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Minutes
|
115
|
+
include TimeTravel
|
116
|
+
include Coercible
|
117
|
+
include Adjustable
|
118
|
+
include Inspectable
|
119
|
+
include Equalable
|
120
|
+
|
121
|
+
def initialize(value)
|
122
|
+
@value = value
|
123
|
+
end
|
124
|
+
|
125
|
+
attr_reader :value
|
126
|
+
|
127
|
+
def to_i
|
128
|
+
@value * 60
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.[](value)
|
132
|
+
new(value)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
class Hours
|
137
|
+
include TimeTravel
|
138
|
+
include Coercible
|
139
|
+
include Adjustable
|
140
|
+
include Inspectable
|
141
|
+
include Equalable
|
142
|
+
|
143
|
+
def initialize(value)
|
144
|
+
@value = value
|
145
|
+
end
|
146
|
+
|
147
|
+
attr_reader :value
|
148
|
+
|
149
|
+
def to_i
|
150
|
+
@value * 60 * 60
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.[](value)
|
154
|
+
new(value)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
class Days
|
159
|
+
include TimeTravel
|
160
|
+
include Coercible
|
161
|
+
include Adjustable
|
162
|
+
include Inspectable
|
163
|
+
include Equalable
|
164
|
+
|
165
|
+
def initialize(value)
|
166
|
+
@value = value
|
167
|
+
end
|
168
|
+
|
169
|
+
attr_reader :value
|
170
|
+
|
171
|
+
def to_i
|
172
|
+
@value * 86400
|
173
|
+
end
|
174
|
+
|
175
|
+
def inspect
|
176
|
+
"#{self.class}[#{value}]"
|
177
|
+
end
|
178
|
+
|
179
|
+
def self.[](value)
|
180
|
+
new(value)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class Weeks
|
185
|
+
include TimeTravel
|
186
|
+
include Coercible
|
187
|
+
include Adjustable
|
188
|
+
include Inspectable
|
189
|
+
include Equalable
|
190
|
+
|
191
|
+
def initialize(value)
|
192
|
+
@value = value
|
193
|
+
end
|
194
|
+
|
195
|
+
attr_reader :value
|
196
|
+
|
197
|
+
def to_i
|
198
|
+
@value * 86400 * 7
|
199
|
+
end
|
200
|
+
|
201
|
+
def self.[](value)
|
202
|
+
new(value)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
class Years
|
207
|
+
include TimeTravel
|
208
|
+
include Coercible
|
209
|
+
include Adjustable
|
210
|
+
include Inspectable
|
211
|
+
include Equalable
|
212
|
+
|
213
|
+
def initialize(value)
|
214
|
+
@value = value
|
215
|
+
end
|
216
|
+
|
217
|
+
attr_reader :value
|
218
|
+
|
219
|
+
def to_i
|
220
|
+
@value * 86400 * 365
|
221
|
+
end
|
222
|
+
|
223
|
+
def self.[](value)
|
224
|
+
new(value)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def self.build(&blk)
|
230
|
+
if blk.arity == 1
|
231
|
+
yield DSL.new
|
232
|
+
else
|
233
|
+
dsl = DSL.new
|
234
|
+
dsl.instance_exec(&blk)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
__END__
|
240
|
+
p TimeExpr::Duration::Days[4].ago
|
241
|
+
p TimeExpr::Duration::Days[4].from_now
|
242
|
+
p TimeExpr::Duration::Weeks[1] + TimeExpr::Duration::Days[4]
|
243
|
+
p (TimeExpr::Duration::Weeks[1] + TimeExpr::Duration::Days[4]).from_now
|
244
|
+
p Time.now + TimeExpr::Duration::Days[4]
|
245
|
+
|
246
|
+
p TimeExpr() { |t| t.weeks(4) - t.days(2) }.from_now
|
247
|
+
p TimeExpr(){ weeks(4) - days(2) }.from_now
|
248
|
+
|
249
|
+
current_time = Time.now + 87400
|
250
|
+
p TimeExpr() { current_time + weeks(4) }
|
251
|
+
p TimeExpr() { Time.now + days(4) }
|
252
|
+
p TimeExpr::Duration::Days[4] * 4
|
253
|
+
|
254
|
+
p TimeExpr::Duration::Hours[4] - 2
|
255
|
+
|
256
|
+
Time::Duration::Days[4].ago
|
257
|
+
Time::Duration::Days[4].from_now
|
258
|
+
Time::Duration::Weeks[1] + Time::Duration::Days[4]
|
259
|
+
|
260
|
+
Time::Duration { |t| t.weeks(4) - t.days(2) }
|
261
|
+
|
262
|
+
Time::Duration { Time.now + minutes(15) }
|
263
|
+
Time::Duration { Time.now + years(10) }
|
264
|
+
Time::Duration { Time.now - days(30) }
|
265
|
+
Time::Duration { }
|
266
|
+
|
267
|
+
Time::Duration { |t| t.now - t.years(years) }
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_expr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Daniels
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-04-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- adam@mediadrive.ca
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".standard.yml"
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/time_expr.rb
|
25
|
+
- lib/time_expr/version.rb
|
26
|
+
homepage: https://github.com/adam12/time_expr
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
homepage_uri: https://github.com/adam12/time_expr
|
31
|
+
source_code_uri: https://github.com/adam12/time_expr
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.5.8
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Modify Time without monkeypatching Fixnum
|
51
|
+
test_files: []
|