richunits 0.2.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +17 -1
- data/COPYING +22 -0
- data/MANIFEST +29 -13
- data/README +50 -1
- data/RELEASE +21 -0
- data/TODO +5 -0
- data/VERSION +1 -1
- data/doc/ads/rdoc.html +12 -0
- data/doc/images/ruby-md.png +0 -0
- data/doc/images/ruby-sm.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tiger-sm.png +0 -0
- data/doc/images/tiger_logo.png +0 -0
- data/doc/index.css +179 -0
- data/doc/index.html +163 -0
- data/lib/rich_units.rb +1 -4
- data/lib/richunits.rb +5 -0
- data/lib/richunits/bytes.rb +178 -0
- data/lib/richunits/duration.rb +290 -0
- data/lib/richunits/multipliers.rb +113 -0
- data/lib/richunits/times.rb +491 -0
- data/lib/richunits/weekdays.rb +65 -0
- data/meta/version +1 -0
- data/test/test_bytes.rb +1 -1
- data/test/test_duration.rb +54 -0
- data/test/test_multipliers.rb +1 -1
- data/test/test_times.rb +10 -9
- metadata +38 -23
- data/NEWS +0 -3
- data/lib/rich_units/bytes.rb +0 -164
- data/lib/rich_units/multipliers.rb +0 -99
- data/lib/rich_units/times.rb +0 -388
- data/lib/rich_units/weekdays.rb +0 -53
@@ -1,99 +0,0 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Multipliers
|
4
|
-
#
|
5
|
-
# DESCRIPTION:
|
6
|
-
#
|
7
|
-
# Adds methods to Numeric to make working with
|
8
|
-
# magnitudes (kilo, mega, giga, milli, micro, etc.)
|
9
|
-
# as well as bits and bytes easier.
|
10
|
-
#
|
11
|
-
# COPYRIGHT:
|
12
|
-
#
|
13
|
-
# Copyright (c) 2005 Thomas Sawyer
|
14
|
-
#
|
15
|
-
# LICENSE:
|
16
|
-
#
|
17
|
-
# Ruby License
|
18
|
-
#
|
19
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
20
|
-
# software under the same terms as Ruby.
|
21
|
-
#
|
22
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
23
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
24
|
-
# FOR A PARTICULAR PURPOSE.
|
25
|
-
#
|
26
|
-
# HISTORY:
|
27
|
-
#
|
28
|
-
# Thanks to Rich Kilmer and bytes.rb which inspired this library.
|
29
|
-
#
|
30
|
-
# AUTHORS:
|
31
|
-
#
|
32
|
-
# - Thomas Sawyer
|
33
|
-
#
|
34
|
-
# NOTES:
|
35
|
-
#
|
36
|
-
# - This library is not compatible with STICK's units.rb (an spin-off
|
37
|
-
# of Facets old units.rb library). Do not attempt to use both at the same time.
|
38
|
-
|
39
|
-
|
40
|
-
# = Multipliers
|
41
|
-
#
|
42
|
-
# Adds methods to Numeric to make working with
|
43
|
-
# magnitudes (kilo, mega, giga, milli, micro, etc.)
|
44
|
-
# as well as bits and bytes easier.
|
45
|
-
#
|
46
|
-
# 1.kilo #=> 1000
|
47
|
-
# 1.milli #=> 0.001
|
48
|
-
# 1.kibi #=> 1024
|
49
|
-
#
|
50
|
-
# To display a value in a certain denomination, simply
|
51
|
-
# perform the inverse operation by placing the
|
52
|
-
# multiplier called on unit (1) in the denominator.
|
53
|
-
#
|
54
|
-
# 1000 / 1.kilo #=> 1
|
55
|
-
# 1024 / 1.kibi #=> 1
|
56
|
-
#
|
57
|
-
|
58
|
-
class Numeric
|
59
|
-
|
60
|
-
# SI Multipliers
|
61
|
-
|
62
|
-
def deka ; self * 10 ; end
|
63
|
-
def hecto ; self * 100 ; end
|
64
|
-
def kilo ; self * 1000 ; end
|
65
|
-
def mega ; self * 1000000 ; end
|
66
|
-
def giga ; self * 1000000000 ; end
|
67
|
-
def tera ; self * 1000000000000 ; end
|
68
|
-
def peta ; self * 1000000000000000 ; end
|
69
|
-
def exa ; self * 1000000000000000000 ; end
|
70
|
-
|
71
|
-
# SI Fractional
|
72
|
-
|
73
|
-
def deci ; self.to_f / 10 ; end
|
74
|
-
def centi ; self.to_f / 100 ; end
|
75
|
-
def milli ; self.to_f / 1000 ; end
|
76
|
-
def micro ; self.to_f / 1000000 ; end
|
77
|
-
def nano ; self.to_f / 1000000000 ; end
|
78
|
-
def pico ; self.to_f / 1000000000000 ; end
|
79
|
-
def femto ; self.to_f / 1000000000000000 ; end
|
80
|
-
def atto ; self.to_f / 1000000000000000000 ; end
|
81
|
-
|
82
|
-
# SI Binary
|
83
|
-
|
84
|
-
def kibi ; self * 1024 ; end
|
85
|
-
def mebi ; self * 1024**2 ; end
|
86
|
-
def gibi ; self * 1024**3 ; end
|
87
|
-
def tebi ; self * 1024**4 ; end
|
88
|
-
def pebi ; self * 1024**5 ; end
|
89
|
-
def exbi ; self * 1024**6 ; end
|
90
|
-
|
91
|
-
# Bits and Bytes
|
92
|
-
|
93
|
-
def bit ; self ; end
|
94
|
-
def bits ; self ; end
|
95
|
-
def byte ; self * 8 ; end
|
96
|
-
def bytes ; self * 8 ; end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
data/lib/rich_units/times.rb
DELETED
@@ -1,388 +0,0 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Times
|
4
|
-
#
|
5
|
-
# DESCRIPTION:
|
6
|
-
#
|
7
|
-
# Plain-English convenience methods for dealing with dates and times.
|
8
|
-
#
|
9
|
-
# COPYRIGHT:
|
10
|
-
#
|
11
|
-
# Copyright (c) 2005 Rich Kilmer, Thomas Sawyer
|
12
|
-
#
|
13
|
-
# LICENSE:
|
14
|
-
#
|
15
|
-
# Ruby License
|
16
|
-
#
|
17
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
18
|
-
# software under the same terms as Ruby.
|
19
|
-
#
|
20
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
21
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22
|
-
# FITNESS FOR A PARTICULAR PURPOSE.
|
23
|
-
#
|
24
|
-
# HISTORY:
|
25
|
-
#
|
26
|
-
# Thanks to Richard Kilmer for the orignal work and Alexander Kellett
|
27
|
-
# for suggesting it for Facets.
|
28
|
-
#
|
29
|
-
# Thanks to Dave Hoover and Ryan Platte for the Weekdays implementation.
|
30
|
-
#
|
31
|
-
# AUTHORS:
|
32
|
-
#
|
33
|
-
# - Rich Kilmer
|
34
|
-
# - Thomas Sawyer
|
35
|
-
# - Dave Hoover
|
36
|
-
# - Ryan Platte
|
37
|
-
# - George Moschovitis
|
38
|
-
#
|
39
|
-
# NOTES:
|
40
|
-
#
|
41
|
-
# - This library is not compatible with STICK's units.rb (an spin-off
|
42
|
-
# of Facets old units.rb library). Do not attempt to use both at the same time.
|
43
|
-
#
|
44
|
-
# TODOs:
|
45
|
-
#
|
46
|
-
# TODO Extra Add in_* methods, like in_days, in_hours, etc.
|
47
|
-
|
48
|
-
require 'facets/time/change'
|
49
|
-
require 'facets/time/set'
|
50
|
-
require 'facets/time/ago'
|
51
|
-
require 'facets/time/hence'
|
52
|
-
|
53
|
-
require 'rich_units/weekdays'
|
54
|
-
|
55
|
-
class Time
|
56
|
-
|
57
|
-
NEVER = Time.mktime(2038)
|
58
|
-
ZERO = Time.mktime(1972)
|
59
|
-
|
60
|
-
# This method calculates the days extrema given two time objects.
|
61
|
-
# start time is the given time1 at 00:00:00
|
62
|
-
# end time is the given time2 at 23:59:59:999
|
63
|
-
#
|
64
|
-
# Input:
|
65
|
-
# - the two times (if only time1 is provided then you get an extrema
|
66
|
-
# of exactly one day extrema.
|
67
|
-
#
|
68
|
-
# Output:
|
69
|
-
# - the time range. you can get the start/end times using
|
70
|
-
# range methods.
|
71
|
-
#
|
72
|
-
# CREDIT George Moschovitis
|
73
|
-
|
74
|
-
def self.days_extrema(time1, time2=nil)
|
75
|
-
time2 = time1 if (not time2.valid? Time)
|
76
|
-
time2 = NEVER if (time2 <= time1)
|
77
|
-
start_time = Time.self.start_of_day(time1)
|
78
|
-
end_time = self.end_of_day(time2)
|
79
|
-
return (start_time..end_time)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Seconds since midnight: Time.now.seconds_since_midnight
|
83
|
-
|
84
|
-
def seconds_since_midnight
|
85
|
-
self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
|
86
|
-
end
|
87
|
-
|
88
|
-
# Returns a new Time representing the time a number of seconds ago.
|
89
|
-
# Do not use this method in combination with x.months, use months_ago instead!
|
90
|
-
#def ago(seconds)
|
91
|
-
# # This is basically a wrapper around the Numeric extension.
|
92
|
-
# #seconds.until(self)
|
93
|
-
# self - seconds
|
94
|
-
#end
|
95
|
-
|
96
|
-
# Returns a new Time representing the time
|
97
|
-
# a number of minutes ago.
|
98
|
-
|
99
|
-
def minutes_ago(minutes)
|
100
|
-
self - (minutes * 60)
|
101
|
-
end
|
102
|
-
|
103
|
-
# Returns a new Time representing the time
|
104
|
-
# a number of hours ago.
|
105
|
-
|
106
|
-
def hours_ago(hours)
|
107
|
-
self - (hours * 3600)
|
108
|
-
end
|
109
|
-
|
110
|
-
# Returns a new Time representing the time
|
111
|
-
# a number of days ago.
|
112
|
-
|
113
|
-
def days_ago(days)
|
114
|
-
self - (days * 86400)
|
115
|
-
end
|
116
|
-
|
117
|
-
# Returns a new Time representing the time
|
118
|
-
# a number of weeks ago.
|
119
|
-
|
120
|
-
def weeks_ago(weeks)
|
121
|
-
self - (weeks * 604800)
|
122
|
-
end
|
123
|
-
|
124
|
-
# Returns a new Time representing the time
|
125
|
-
# a number of months ago.
|
126
|
-
|
127
|
-
def months_ago(months)
|
128
|
-
years = (month - months / 12).to_i
|
129
|
-
set(:year=>(year - years), :month=>(month - months) % 12)
|
130
|
-
end
|
131
|
-
|
132
|
-
# Returns a new Time representing the time a number of specified
|
133
|
-
# months ago.
|
134
|
-
#def months_ago(months)
|
135
|
-
# if months >= self.month
|
136
|
-
# change(:year => self.year - 1, :month => 12).months_ago(months - self.month)
|
137
|
-
# else
|
138
|
-
# change(:year => self.year, :month => self.month - months)
|
139
|
-
# end
|
140
|
-
#end
|
141
|
-
|
142
|
-
# Returns a new Time representing the time
|
143
|
-
# a number of years ago.
|
144
|
-
|
145
|
-
def years_ago(years)
|
146
|
-
set(:year=>(year - years))
|
147
|
-
end
|
148
|
-
|
149
|
-
# Returns a new Time representing the time a number of specified
|
150
|
-
# years ago.
|
151
|
-
#def years_ago(years)
|
152
|
-
# change(:year => self.year - years)
|
153
|
-
#end
|
154
|
-
|
155
|
-
# Returns a new Time representing the time
|
156
|
-
# a number of minutes hence.
|
157
|
-
|
158
|
-
def minutes_hence(minutes)
|
159
|
-
self + (minutes * 60)
|
160
|
-
end
|
161
|
-
|
162
|
-
# Returns a new Time representing the time
|
163
|
-
# a number of hours hence.
|
164
|
-
|
165
|
-
def hours_hence(hours)
|
166
|
-
self + (hours * 3600)
|
167
|
-
end
|
168
|
-
|
169
|
-
# Returns a new Time representing the time
|
170
|
-
# a number of days hence.
|
171
|
-
|
172
|
-
def days_hence(days)
|
173
|
-
self + (days * 86400)
|
174
|
-
end
|
175
|
-
|
176
|
-
# Returns a new Time representing the time
|
177
|
-
# a number of weeks hence.
|
178
|
-
|
179
|
-
def weeks_hence(weeks)
|
180
|
-
self + (weeks * 604800)
|
181
|
-
end
|
182
|
-
|
183
|
-
# Returns a new Time representing the time
|
184
|
-
# a number of months hence.
|
185
|
-
|
186
|
-
def months_hence(months)
|
187
|
-
years = (month + months / 12).to_i
|
188
|
-
set(:year=>(year + years), :month=>(month + months) % 12)
|
189
|
-
end
|
190
|
-
|
191
|
-
#def months_hence(months)
|
192
|
-
# if months + self.month > 12
|
193
|
-
# change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1)))
|
194
|
-
# else
|
195
|
-
# change(:year => self.year, :month => self.month + months)
|
196
|
-
# end
|
197
|
-
#end
|
198
|
-
|
199
|
-
# Returns a new Time representing the time
|
200
|
-
# a number of years hence.
|
201
|
-
|
202
|
-
def years_hence(years)
|
203
|
-
set(:year=>(year + years))
|
204
|
-
end
|
205
|
-
|
206
|
-
# Returns a new Time representing the time a number of seconds
|
207
|
-
# since the instance time. Do not use this method in combination
|
208
|
-
# with x.months, use months_since instead!
|
209
|
-
alias_method :since, :hence
|
210
|
-
|
211
|
-
alias_method :minutes_since, :minutes_hence
|
212
|
-
alias_method :days_since, :days_hence
|
213
|
-
alias_method :weeks_since, :weeks_hence
|
214
|
-
alias_method :months_since, :months_hence
|
215
|
-
alias_method :years_since, :years_hence
|
216
|
-
|
217
|
-
#def years_since(years)
|
218
|
-
# change(:year => self.year + years)
|
219
|
-
#end
|
220
|
-
|
221
|
-
# Short-hand for years_ago(1)
|
222
|
-
def last_year
|
223
|
-
years_ago(1)
|
224
|
-
end
|
225
|
-
|
226
|
-
# Short-hand for years_since(1)
|
227
|
-
def next_year
|
228
|
-
years_since(1)
|
229
|
-
end
|
230
|
-
|
231
|
-
# Short-hand for months_ago(1)
|
232
|
-
def last_month
|
233
|
-
months_ago(1)
|
234
|
-
end
|
235
|
-
|
236
|
-
# Short-hand for months_since(1)
|
237
|
-
def next_month
|
238
|
-
months_since(1)
|
239
|
-
end
|
240
|
-
|
241
|
-
# Returns a new Time representing the "start" of this week (Monday, 0:00)
|
242
|
-
def beginning_of_week
|
243
|
-
(self - self.wday.days).midnight + 1.day
|
244
|
-
end
|
245
|
-
alias :monday :beginning_of_week
|
246
|
-
alias :at_beginning_of_week :beginning_of_week
|
247
|
-
|
248
|
-
# Returns a new Time representing the start of the given
|
249
|
-
# day in next week (default is Monday).
|
250
|
-
def next_week(day = :monday)
|
251
|
-
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2,
|
252
|
-
:thursday => 3, :friday => 4, :saturday => 5,
|
253
|
-
:sunday => 6 }
|
254
|
-
since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Set time to end of day
|
258
|
-
#def end_of_day
|
259
|
-
# return Time.mktime(year, month, day, 23, 59, 59, 999)
|
260
|
-
#end
|
261
|
-
|
262
|
-
# Returns a new Time representing the start of the day (0:00)
|
263
|
-
def beginning_of_day
|
264
|
-
self - self.seconds_since_midnight
|
265
|
-
end
|
266
|
-
alias :midnight :beginning_of_day
|
267
|
-
alias :at_midnight :beginning_of_day
|
268
|
-
alias :at_beginning_of_day :beginning_of_day
|
269
|
-
alias :start_of_day :beginning_of_day
|
270
|
-
|
271
|
-
# Returns a new Time representing the start of the month
|
272
|
-
# (1st of the month, 0:00)
|
273
|
-
def beginning_of_month
|
274
|
-
#self - ((self.mday-1).days + self.seconds_since_midnight)
|
275
|
-
change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
276
|
-
end
|
277
|
-
alias_method :at_beginning_of_month, :beginning_of_month
|
278
|
-
|
279
|
-
# Returns a new Time representing the start of the year (1st of january, 0:00)
|
280
|
-
def beginning_of_year
|
281
|
-
change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
|
282
|
-
end
|
283
|
-
alias :at_beginning_of_year :beginning_of_year
|
284
|
-
|
285
|
-
# Convenience method which returns a new Time representing
|
286
|
-
# the time 1 day ago
|
287
|
-
def yesterday
|
288
|
-
self.ago(1.day)
|
289
|
-
end
|
290
|
-
|
291
|
-
# Convenience method which returns a new Time representing
|
292
|
-
# the time 1 day since the instance time
|
293
|
-
def tomorrow
|
294
|
-
self.since(1.day)
|
295
|
-
end
|
296
|
-
|
297
|
-
# Returns a new time at start of day
|
298
|
-
def to_start_of_day
|
299
|
-
return Time.mktime(year, month, day, 0, 0, 0, 0)
|
300
|
-
end
|
301
|
-
|
302
|
-
# Returns a new time at end of day
|
303
|
-
def to_end_of_day
|
304
|
-
Time.mktime(year, month, day, 23, 59, 59, 999)
|
305
|
-
end
|
306
|
-
|
307
|
-
# Returns true only if day of time is included in the
|
308
|
-
# range (stime..etime). Only year days are checked.
|
309
|
-
def in_day_range?(stime=ZERO, etime=NEVER)
|
310
|
-
if (etime <= stime)
|
311
|
-
warn "invalid end time (#{etime} < #{stime})" if $DEBUG
|
312
|
-
etime = NEVER
|
313
|
-
end
|
314
|
-
|
315
|
-
stime = stime.to_start_of_day
|
316
|
-
etime = etime.to_end_of_day
|
317
|
-
|
318
|
-
return (stime..etime).include?(time)
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
|
323
|
-
class Numeric
|
324
|
-
|
325
|
-
# Enables the use of time calculations and declarations,
|
326
|
-
# like 45.minutes + 2.hours + 4.years. The base unit for
|
327
|
-
# all of these Numeric time methods is seconds.
|
328
|
-
def seconds ; self ; end
|
329
|
-
alias_method :second, :seconds
|
330
|
-
#def as_seconds ; self ; end
|
331
|
-
|
332
|
-
# Converts minutes into seconds.
|
333
|
-
def minutes ; self * 60 ; end
|
334
|
-
alias_method :minute, :minutes
|
335
|
-
#def as_minutes ; self.to_f / 60 ; end
|
336
|
-
|
337
|
-
# Converts hours into seconds.
|
338
|
-
def hours ; self * 60.minutes ; end
|
339
|
-
alias_method :hour, :hours
|
340
|
-
#def as_hours ; self / 60.minutes ; end
|
341
|
-
|
342
|
-
# Converts days into seconds.
|
343
|
-
def days ; self * 24.hours ; end
|
344
|
-
alias_method :day, :days
|
345
|
-
#def as_days ; self / 24.hours ; end
|
346
|
-
|
347
|
-
# Converts weeks into seconds.
|
348
|
-
def weeks ; self * 7.days ; end
|
349
|
-
alias_method :week, :weeks
|
350
|
-
#def as_weeks ; self / 7.days ; end
|
351
|
-
|
352
|
-
# Converts fortnights into seconds.
|
353
|
-
# (A fortnight is 2 weeks)
|
354
|
-
def fortnights ; self * 2.weeks ; end
|
355
|
-
alias_method :fortnight, :fortnights
|
356
|
-
#def as_fortnights ; self / 2.weeks ; end
|
357
|
-
|
358
|
-
# Converts months into seconds.
|
359
|
-
# WARNING: This is not exact as it assumes 30 days to a month.
|
360
|
-
def months ; self * 30.days ; end
|
361
|
-
alias_method :month, :months
|
362
|
-
#def as_months ; self / 30.days ; end
|
363
|
-
|
364
|
-
# Converts years into seconds.
|
365
|
-
def years ; self * 365.days ; end
|
366
|
-
alias_method :year, :years
|
367
|
-
#def as_years ; self / 365.days ; end
|
368
|
-
|
369
|
-
# Calculates time _before_ a given time. Default time is now.
|
370
|
-
# Reads best with arguments: 10.days.before( Time.now - 1.day )
|
371
|
-
def before(time = ::Time.now)
|
372
|
-
time - self
|
373
|
-
end
|
374
|
-
alias_method :until, :before # Reads best with argument: 10.minutes.until(time)
|
375
|
-
alias_method :ago, :before # Reads best without arguments: 10.minutes.ago
|
376
|
-
|
377
|
-
# Calculates time _after_ a given time. Default time is now.
|
378
|
-
# Reads best with argument: 10.minutes.after(time)
|
379
|
-
def after(time = ::Time.now)
|
380
|
-
time + self
|
381
|
-
end
|
382
|
-
alias_method :since, :after # Reads best with argument: 10.minutes.since(time)
|
383
|
-
alias_method :hence, :after # Reads best with argument: 10.minutes.since(time)
|
384
|
-
alias_method :from_now, :after # Reads best without arguments: 10.minutes.from_now
|
385
|
-
alias_method :later, :after # Reads best without arguments: 10.minutes.later
|
386
|
-
|
387
|
-
end
|
388
|
-
|