richunits 0.2.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.
- data/CHANGES +4 -0
- data/MANIFEST +24 -0
- data/NEWS +3 -0
- data/README +5 -0
- data/VERSION +1 -0
- data/lib/rich_units.rb +4 -0
- data/lib/rich_units/bytes.rb +164 -0
- data/lib/rich_units/multipliers.rb +99 -0
- data/lib/rich_units/times.rb +388 -0
- data/lib/rich_units/weekdays.rb +53 -0
- data/meta/abstract +5 -0
- data/meta/contact +1 -0
- data/meta/created +1 -0
- data/meta/homepage +2 -0
- data/meta/license +1 -0
- data/meta/repository +0 -0
- data/meta/title +1 -0
- data/test/test_bytes.rb +69 -0
- data/test/test_multipliers.rb +99 -0
- data/test/test_times.rb +87 -0
- metadata +91 -0
data/CHANGES
ADDED
data/MANIFEST
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
doc
|
2
|
+
test
|
3
|
+
test/test_bytes.rb
|
4
|
+
test/test_multipliers.rb
|
5
|
+
test/test_times.rb
|
6
|
+
CHANGES
|
7
|
+
README
|
8
|
+
meta
|
9
|
+
meta/created
|
10
|
+
meta/repository
|
11
|
+
meta/homepage
|
12
|
+
meta/abstract
|
13
|
+
meta/title
|
14
|
+
meta/license
|
15
|
+
meta/contact
|
16
|
+
lib
|
17
|
+
lib/rich_units.rb
|
18
|
+
lib/rich_units
|
19
|
+
lib/rich_units/times.rb
|
20
|
+
lib/rich_units/multipliers.rb
|
21
|
+
lib/rich_units/weekdays.rb
|
22
|
+
lib/rich_units/bytes.rb
|
23
|
+
VERSION
|
24
|
+
NEWS
|
data/NEWS
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
richunits 0.2.0 beta (2008-09-06)
|
data/lib/rich_units.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# TITLE:
|
2
|
+
#
|
3
|
+
# Bytes
|
4
|
+
#
|
5
|
+
# DESCRIPTION:
|
6
|
+
#
|
7
|
+
# Additional methods for Numeric class to make working with
|
8
|
+
# bits and bytes easier.
|
9
|
+
#
|
10
|
+
# COPYRIGHT:
|
11
|
+
#
|
12
|
+
# Copyright (c) 2005 Rich Kilmer
|
13
|
+
#
|
14
|
+
# LICENSE:
|
15
|
+
#
|
16
|
+
# Ruby License
|
17
|
+
#
|
18
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
19
|
+
# software under the same terms as Ruby.
|
20
|
+
#
|
21
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
22
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
23
|
+
# FOR A PARTICULAR PURPOSE.
|
24
|
+
#
|
25
|
+
# HISTORY:
|
26
|
+
#
|
27
|
+
# Special thanks to Richard Kilmer for the orignal work.
|
28
|
+
# This library is based on the original library bytes.rb
|
29
|
+
# Copyright (c) 2004 by Rich Kilmer.
|
30
|
+
#
|
31
|
+
# Also thanks to Alexander Kellett for suggesting it be
|
32
|
+
# included in Facets.
|
33
|
+
#
|
34
|
+
# AUTHORS:
|
35
|
+
#
|
36
|
+
# - Rich Kilmer
|
37
|
+
# - Thomas Sawyer
|
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
|
+
# - Currently kilo, mega, etc. are all powers of two and not ten,
|
47
|
+
# which technically isn't corrent even though it is common usage.
|
48
|
+
#
|
49
|
+
# - The in_* notation is weak. If a better nomentclature is thought
|
50
|
+
# of then consider changing this.
|
51
|
+
|
52
|
+
|
53
|
+
# = Binary Multipliers
|
54
|
+
#
|
55
|
+
# Additional methods for Numeric class to make working with
|
56
|
+
# bits and bytes easier. Bits are used as the base value and
|
57
|
+
# these methods can be used to convert between different
|
58
|
+
# magnitudes.
|
59
|
+
#
|
60
|
+
# == Synopisis
|
61
|
+
#
|
62
|
+
# 1.byte #=> 8
|
63
|
+
# 2.bytes #=> 16
|
64
|
+
# 1.kilobit #=> 1024
|
65
|
+
# 1.kilobyte #=> 8192
|
66
|
+
#
|
67
|
+
# Use the in_* methods to perform the inverse operations.
|
68
|
+
#
|
69
|
+
# 8192.in_kilobytes #=> 1
|
70
|
+
# 1024.in_kilobits #=> 1
|
71
|
+
#
|
72
|
+
|
73
|
+
class Numeric
|
74
|
+
|
75
|
+
def bit ; self ; end
|
76
|
+
def bits ; self ; end
|
77
|
+
def byte ; self * 8 ; end
|
78
|
+
def bytes ; self * 8 ; end
|
79
|
+
|
80
|
+
[ 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa' ].each_with_index do |m, i|
|
81
|
+
j = i + 1
|
82
|
+
class_eval %{
|
83
|
+
def #{m}bit ; self * #{1024**j} ; end
|
84
|
+
def #{m}byte ; self * #{1024**j*8} ; end
|
85
|
+
def in_#{m}bits ; self / #{1024**j} ; end
|
86
|
+
def in_#{m}bytes ; self / #{1024**j*8} ; end
|
87
|
+
alias_method :#{m}bits, :#{m}bit
|
88
|
+
alias_method :#{m}bytes, :#{m}byte
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
[ 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi' ].each_with_index do |m, i|
|
93
|
+
j = i + 1
|
94
|
+
class_eval %{
|
95
|
+
def #{m}bit ; self * #{1024**j} ; end
|
96
|
+
def #{m}byte ; self * #{1024**j*8} ; end
|
97
|
+
def in_#{m}bits ; self / #{1024**j} ; end
|
98
|
+
def in_#{m}bytes ; self / #{1024**j*8} ; end
|
99
|
+
alias_method :#{m}bits, :#{m}bit
|
100
|
+
alias_method :#{m}bytes, :#{m}byte
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
# Formated string of bits proportial to size.
|
105
|
+
#
|
106
|
+
# 1024.bits_to_s #=> "1.00 kb"
|
107
|
+
# 1048576.bits_to_s #=> "1.00 mb"
|
108
|
+
# 1073741824.bits_to_s #=> "1.00 gb"
|
109
|
+
# 1099511627776.bits_to_s #=> "1.00 tb"
|
110
|
+
#
|
111
|
+
# Takes a format string to adjust output.
|
112
|
+
#
|
113
|
+
# 1024.bits_to_s('%.0f') #=> "1 kb"
|
114
|
+
#
|
115
|
+
def strfbits(fmt='%.2f')
|
116
|
+
case
|
117
|
+
when self < 1024
|
118
|
+
"#{self} bits"
|
119
|
+
when self < 1024**2
|
120
|
+
"#{fmt % (self.to_f / 1024)} kb"
|
121
|
+
when self < 1024**3
|
122
|
+
"#{fmt % (self.to_f / 1024**2)} mb"
|
123
|
+
when self < 1024**4
|
124
|
+
"#{fmt % (self.to_f / 1024**3)} gb"
|
125
|
+
when self < 1024**5
|
126
|
+
"#{fmt % (self.to_f / 1024**4)} tb"
|
127
|
+
else
|
128
|
+
"#{self} bits"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Formated string of bytes proportial to size.
|
133
|
+
#
|
134
|
+
# 1024.bytes_to_s #=> "1.00 KB"
|
135
|
+
# 1048576.bytes_to_s #=> "1.00 MB"
|
136
|
+
# 1073741824.bytes_to_s #=> "1.00 GB"
|
137
|
+
# 1099511627776.bytes_to_s #=> "1.00 TB"
|
138
|
+
#
|
139
|
+
# Takes a format string to adjust output.
|
140
|
+
#
|
141
|
+
# 1024.bytes_to_s('%.0f') #=> "1 KB"
|
142
|
+
#
|
143
|
+
def strfbytes(fmt='%.2f')
|
144
|
+
case
|
145
|
+
when self < 1024
|
146
|
+
"#{self} bytes"
|
147
|
+
when self < 1024**2
|
148
|
+
"#{fmt % (self.to_f / 1024)} KB"
|
149
|
+
when self < 1024**3
|
150
|
+
"#{fmt % (self.to_f / 1024**2)} MB"
|
151
|
+
when self < 1024**4
|
152
|
+
"#{fmt % (self.to_f / 1024**3)} GB"
|
153
|
+
when self < 1024**5
|
154
|
+
"#{fmt % (self.to_f / 1024**4)} TB"
|
155
|
+
else
|
156
|
+
"#{self} bytes"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# deprecated
|
161
|
+
alias_method :octet_units, :strfbytes
|
162
|
+
|
163
|
+
end
|
164
|
+
|
@@ -0,0 +1,99 @@
|
|
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
|
+
|
@@ -0,0 +1,388 @@
|
|
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
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# = Weekdays
|
2
|
+
#
|
3
|
+
# The Weekdays class provides useful weekday terminology.
|
4
|
+
|
5
|
+
class Weekdays
|
6
|
+
|
7
|
+
WEEKDAYS = 1..5 # Monday is wday 1
|
8
|
+
ONE_DAY = 60 * 60 * 24
|
9
|
+
|
10
|
+
def initialize(n)
|
11
|
+
@n = n
|
12
|
+
end
|
13
|
+
|
14
|
+
def ago(time = ::Time.now)
|
15
|
+
step :down, time
|
16
|
+
end
|
17
|
+
alias_method :until, :ago
|
18
|
+
alias_method :before, :ago
|
19
|
+
|
20
|
+
def since(time = ::Time.now)
|
21
|
+
step :up, time
|
22
|
+
end
|
23
|
+
alias_method :from_now, :since
|
24
|
+
alias_method :after, :since
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def step(direction, original_time)
|
29
|
+
result = original_time
|
30
|
+
time = ONE_DAY
|
31
|
+
|
32
|
+
compare = direction == :up ? ">" : "<"
|
33
|
+
time *= -1 if direction == :down
|
34
|
+
|
35
|
+
@n.times do
|
36
|
+
result += time until result.send(compare, original_time) && WEEKDAYS.member?(result.wday)
|
37
|
+
original_time = result
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Numeric
|
44
|
+
|
45
|
+
# Works with day in terms of weekdays.
|
46
|
+
def weekdays
|
47
|
+
Weekdays.new(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :weekday, :weekdays
|
51
|
+
|
52
|
+
end
|
53
|
+
|
data/meta/abstract
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
Rich Kilmer's Unit system provides english-esque methods
|
2
|
+
for working with common units, such as days and bytes and
|
3
|
+
multiplers like kilo, or mega. It does so by reducing basic
|
4
|
+
measures to a lower common denominator, such as seconds for
|
5
|
+
time measures and bits for byte measures.
|
data/meta/contact
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
7ransUnit <transfire@gmail.com>
|
data/meta/created
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2008-02-21
|
data/meta/homepage
ADDED
data/meta/license
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
MIT
|
data/meta/repository
ADDED
File without changes
|
data/meta/title
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Rich Kilmer's Units System
|
data/test/test_bytes.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'rich_units/bytes.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TC_Numeric < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# bits
|
7
|
+
|
8
|
+
def test_bits
|
9
|
+
assert_equal( 8, 8.bits )
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_kilobits
|
13
|
+
assert_equal( 1024**1, 1.kilobit )
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_megabits
|
17
|
+
assert_equal( 1024**2, 1.megabit )
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_gigabits
|
21
|
+
assert_equal( 1024**3, 1.gigabit )
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_terabits
|
25
|
+
assert_equal( 1024**4, 1.terabit )
|
26
|
+
end
|
27
|
+
|
28
|
+
# bytes
|
29
|
+
|
30
|
+
def test_bytes
|
31
|
+
assert_equal( 8192, 1024.bytes )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_kilobytes
|
35
|
+
assert_equal( 1024**1*8, 1.kilobyte )
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_megabytes
|
39
|
+
assert_equal( 1024**2*8, 1.megabyte )
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_gigabytes
|
43
|
+
assert_equal( 1024**3*8, 1.gigabyte )
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_terabytes
|
47
|
+
assert_equal( 1024**4*8, 1.terabyte )
|
48
|
+
end
|
49
|
+
|
50
|
+
# bits_to_s
|
51
|
+
|
52
|
+
def test_strfbits
|
53
|
+
assert_equal( "1.00 kb", 1024.strfbits )
|
54
|
+
assert_equal( "1.00 mb", 1048576.strfbits )
|
55
|
+
assert_equal( "1.00 gb", 1073741824.strfbits )
|
56
|
+
assert_equal( "1.00 tb", 1099511627776.strfbits )
|
57
|
+
end
|
58
|
+
|
59
|
+
# bytes_to_s
|
60
|
+
|
61
|
+
def test_strfbytes
|
62
|
+
assert_equal( "1.00 KB", 1024.strfbytes )
|
63
|
+
assert_equal( "1.00 MB", 1048576.strfbytes )
|
64
|
+
assert_equal( "1.00 GB", 1073741824.strfbytes )
|
65
|
+
assert_equal( "1.00 TB", 1099511627776.strfbytes )
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'rich_units/multipliers.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TC_Multipliers < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_deka
|
7
|
+
assert_equal( 10, 1.deka )
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_hecto
|
11
|
+
assert_equal( 100, 1.hecto )
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_kilo
|
15
|
+
assert_equal( 1000, 1.kilo )
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_mega
|
19
|
+
assert_equal( 1000000, 1.mega )
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_giga
|
23
|
+
assert_equal( 1000000000, 1.giga )
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_tera
|
27
|
+
assert_equal( 1000000000000, 1.tera )
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_peta
|
31
|
+
assert_equal( 1000000000000000, 1.peta )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_exa
|
35
|
+
assert_equal( 1000000000000000000, 1.exa )
|
36
|
+
end
|
37
|
+
|
38
|
+
# Fractional
|
39
|
+
|
40
|
+
def test_deci
|
41
|
+
assert_equal( 0.1, 1.deci )
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_centi
|
45
|
+
assert_equal( 0.01, 1.centi )
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_milli
|
49
|
+
assert_equal( 0.001, 1.milli )
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_milli
|
53
|
+
assert_equal( 0.000001, 1.micro )
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_nano
|
57
|
+
assert_equal( 0.000000001, 1.nano )
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_pico
|
61
|
+
assert_equal( 0.000000000001, 1.pico )
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_femto
|
65
|
+
assert_equal( 0.000000000000001, 1.femto )
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_atto
|
69
|
+
assert_equal( 0.000000000000000001, 1.atto )
|
70
|
+
end
|
71
|
+
|
72
|
+
# SI Binary
|
73
|
+
|
74
|
+
def test_kibi
|
75
|
+
assert_equal( 1024, 1.kibi )
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_mebi
|
79
|
+
assert_equal( 1024**2, 1.mebi )
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_gibi
|
83
|
+
assert_equal( 1024**3, 1.gibi )
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_tebi
|
87
|
+
assert_equal( 1024**4, 1.tebi )
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_pebi
|
91
|
+
assert_equal( 1024**5, 1.pebi )
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_exbi
|
95
|
+
assert_equal( 1024**6, 1.exbi )
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
data/test/test_times.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rich_units/times.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TC_Times < Test::Unit::TestCase
|
5
|
+
|
6
|
+
#def test_micro_seconds
|
7
|
+
# assert_equal( 0.000001, 1.microsecond )
|
8
|
+
#end
|
9
|
+
|
10
|
+
#def test_milli_seconds
|
11
|
+
# assert_equal( 0.001, 1.millisecond )
|
12
|
+
#end
|
13
|
+
|
14
|
+
def test_seconds
|
15
|
+
assert_equal( 60**0, 1.seconds )
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_minutes
|
19
|
+
assert_equal( 60**1, 1.minutes )
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_hours
|
23
|
+
assert_equal( 60**2, 1.hours )
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_days
|
27
|
+
assert_equal( 24*(60**2), 1.days )
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_weeks
|
31
|
+
assert_equal( 7*24*(60**2), 1.weeks )
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_fortnights
|
35
|
+
assert_equal( 14*24*(60**2), 1.fortnights )
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_months
|
39
|
+
assert_equal( 30*24*(60**2), 1.months )
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_years
|
43
|
+
assert_equal( 365*24*(60**2), 1.years )
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_before
|
47
|
+
t = Time.now
|
48
|
+
assert_equal( t - 1.day, 1.day.before(t) )
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_after
|
52
|
+
t = Time.now
|
53
|
+
assert_equal( t + 1.day, 1.day.after(t) )
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class WeekdaysTest < Test::Unit::TestCase
|
59
|
+
|
60
|
+
MONDAY = Time.at(1165250000)
|
61
|
+
THURSDAY = Time.at(1165500000)
|
62
|
+
FRIDAY = Time.at(1165606025)
|
63
|
+
|
64
|
+
def test_weekday_after_monday
|
65
|
+
assert_equal 2, 1.weekday.since(MONDAY).wday
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_weekday_after_friday
|
69
|
+
assert_equal 1, 1.weekday.after(FRIDAY).wday
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_weekdays_before_friday
|
73
|
+
assert_equal 2, 3.weekdays.before(FRIDAY).wday
|
74
|
+
end
|
75
|
+
|
76
|
+
#def test_weekday_before_today
|
77
|
+
# Time.expects(:now).returns(THURSDAY)
|
78
|
+
# assert_equal 3, 1.weekday.ago.wday
|
79
|
+
#end
|
80
|
+
|
81
|
+
#def test_weekdays_after_today
|
82
|
+
# Time.expects(:now).returns(MONDAY)
|
83
|
+
# assert_equal 3, 2.weekday.from_now.wday
|
84
|
+
#end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: richunits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- |
|
8
|
+
7ransUnit <transfire@gmail.com>
|
9
|
+
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2008-09-06 00:00:00 -04:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
description: Rich Kilmer's Unit system provides english-esque methods for working with common units, such as days and bytes and multiplers like kilo, or mega. It does so by reducing basic measures to a lower common denominator, such as seconds for time measures and bits for byte measures.
|
19
|
+
email: transfire@gmail.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files:
|
25
|
+
- README
|
26
|
+
- MANIFEST
|
27
|
+
- CHANGES
|
28
|
+
- VERSION
|
29
|
+
- NEWS
|
30
|
+
files:
|
31
|
+
- doc
|
32
|
+
- MANIFEST
|
33
|
+
- test
|
34
|
+
- test/test_bytes.rb
|
35
|
+
- test/test_multipliers.rb
|
36
|
+
- test/test_times.rb
|
37
|
+
- CHANGES
|
38
|
+
- README
|
39
|
+
- meta
|
40
|
+
- meta/created
|
41
|
+
- meta/repository
|
42
|
+
- meta/homepage
|
43
|
+
- meta/abstract
|
44
|
+
- meta/title
|
45
|
+
- meta/license
|
46
|
+
- meta/contact
|
47
|
+
- lib
|
48
|
+
- lib/rich_units.rb
|
49
|
+
- lib/rich_units
|
50
|
+
- lib/rich_units/times.rb
|
51
|
+
- lib/rich_units/multipliers.rb
|
52
|
+
- lib/rich_units/weekdays.rb
|
53
|
+
- lib/rich_units/bytes.rb
|
54
|
+
- VERSION
|
55
|
+
- NEWS
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: |+
|
58
|
+
http://tigerops.rubyforge.org/richunits
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --inline-source
|
63
|
+
- --title
|
64
|
+
- richunits api
|
65
|
+
- --main
|
66
|
+
- README
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: tigerops
|
84
|
+
rubygems_version: 1.2.0
|
85
|
+
signing_key:
|
86
|
+
specification_version: 2
|
87
|
+
summary: Rich Kilmer's Unit system provides english-esque methods
|
88
|
+
test_files:
|
89
|
+
- test/test_bytes.rb
|
90
|
+
- test/test_multipliers.rb
|
91
|
+
- test/test_times.rb
|