stick 1.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.
Files changed (56) hide show
  1. data/CHANGES +7 -0
  2. data/COPYING +344 -0
  3. data/README +110 -0
  4. data/lib/stick/constants.rb +3 -0
  5. data/lib/stick/constants/cgs.rb +151 -0
  6. data/lib/stick/constants/mks.rb +158 -0
  7. data/lib/stick/constants/number.rb +33 -0
  8. data/lib/stick/constants/typeless_cgs.rb +141 -0
  9. data/lib/stick/constants/typeless_mks.rb +142 -0
  10. data/lib/stick/currency.rb +8 -0
  11. data/lib/stick/mapcar.rb +61 -0
  12. data/lib/stick/matrix.rb +1022 -0
  13. data/lib/stick/quaternion.rb +562 -0
  14. data/lib/stick/times.rb +441 -0
  15. data/lib/stick/units.rb +112 -0
  16. data/lib/stick/units/base.rb +980 -0
  17. data/lib/stick/units/currency.rb +159 -0
  18. data/lib/stick/units/data/binary/base.rb +4 -0
  19. data/lib/stick/units/data/cex.rb +5 -0
  20. data/lib/stick/units/data/currency-default.rb +5 -0
  21. data/lib/stick/units/data/currency-standard.rb +2 -0
  22. data/lib/stick/units/data/currency/base.rb +89 -0
  23. data/lib/stick/units/data/iec.rb +5 -0
  24. data/lib/stick/units/data/iec_binary/base.rb +6 -0
  25. data/lib/stick/units/data/si.rb +7 -0
  26. data/lib/stick/units/data/si/base.rb +9 -0
  27. data/lib/stick/units/data/si/derived.rb +26 -0
  28. data/lib/stick/units/data/si/extra.rb +22 -0
  29. data/lib/stick/units/data/uk.rb +10 -0
  30. data/lib/stick/units/data/uk/base.rb +22 -0
  31. data/lib/stick/units/data/units-default.rb +11 -0
  32. data/lib/stick/units/data/units-standard.rb +5 -0
  33. data/lib/stick/units/data/us.rb +10 -0
  34. data/lib/stick/units/data/us/base.rb +23 -0
  35. data/lib/stick/units/data/xmethods.rb +5 -0
  36. data/lib/stick/units/data/xmethods/cached.rb +84 -0
  37. data/lib/stick/units/data/xmethods/mapping.rb +87 -0
  38. data/lib/stick/units/loaders.rb +98 -0
  39. data/lib/stick/units/units.rb +109 -0
  40. data/meta/MANIFEST +76 -0
  41. data/meta/ROLLRC +2 -0
  42. data/meta/icli.yaml +16 -0
  43. data/meta/project.yaml +18 -0
  44. data/task/clobber/package +10 -0
  45. data/task/publish +57 -0
  46. data/task/release +10 -0
  47. data/task/setup +1616 -0
  48. data/task/test +25 -0
  49. data/test/spec_matrix.rb +342 -0
  50. data/test/test_currency.rb +26 -0
  51. data/test/test_matrix.rb +359 -0
  52. data/test/test_units.rb +205 -0
  53. data/work/TODO +20 -0
  54. data/work/bytes.rb +231 -0
  55. data/work/multipliers.rb +195 -0
  56. metadata +138 -0
@@ -0,0 +1,441 @@
1
+ # Title:
2
+ #
3
+ # Times
4
+ #
5
+ # Summary:
6
+ #
7
+ # Convenience methods for time.
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
+ # - TODO Extra Add in_* methods, like in_days, in_hours, etc.
42
+ #
43
+ # - TODO Some Numeric method could still be supported.
44
+
45
+ require 'facets/time/change'
46
+
47
+ =begin
48
+ # = Time In English
49
+ #
50
+ # Plain-English convenience methods for dealing with dates and times.
51
+
52
+ class Numeric
53
+
54
+ # Enables the use of time calculations and declarations,
55
+ # like 45.minutes + 2.hours + 4.years. The base unit for
56
+ # all of these Numeric time methods is seconds.
57
+ def seconds ; self ; end
58
+ alias_method :second, :seconds
59
+ #def as_seconds ; self ; end
60
+
61
+ # Converts minutes into seconds.
62
+ def minutes ; self * 60 ; end
63
+ alias_method :minute, :minutes
64
+ #def as_minutes ; self.to_f / 60 ; end
65
+
66
+ # Converts hours into seconds.
67
+ def hours ; self * 60.minutes ; end
68
+ alias_method :hour, :hours
69
+ #def as_hours ; self / 60.minutes ; end
70
+
71
+ # Converts days into seconds.
72
+ def days ; self * 24.hours ; end
73
+ alias_method :day, :days
74
+ #def as_days ; self / 24.hours ; end
75
+
76
+ # Converts weeks into seconds.
77
+ def weeks ; self * 7.days ; end
78
+ alias_method :week, :weeks
79
+ #def as_weeks ; self / 7.days ; end
80
+
81
+ # Converts fortnights into seconds.
82
+ # (A fortnight is 2 weeks)
83
+ def fortnights ; self * 2.weeks ; end
84
+ alias_method :fortnight, :fortnights
85
+ #def as_fortnights ; self / 2.weeks ; end
86
+
87
+ # Converts months into seconds.
88
+ # WARNING: This is not exact as it assumes 30 days to a month.
89
+ def months ; self * 30.days ; end
90
+ alias_method :month, :months
91
+ #def as_months ; self / 30.days ; end
92
+
93
+ # Converts years into seconds.
94
+ def years ; self * 365.days ; end
95
+ alias_method :year, :years
96
+ #def as_years ; self / 365.days ; end
97
+
98
+ # Calculates time _before_ a given time. Default time is now.
99
+ # Reads best with arguments: 10.days.before( Time.now - 1.day )
100
+ def before(time = ::Time.now)
101
+ time - self
102
+ end
103
+ alias_method :until, :before # Reads best with argument: 10.minutes.until(time)
104
+ alias_method :ago, :before # Reads best without arguments: 10.minutes.ago
105
+
106
+ # Calculates time _after_ a given time. Default time is now.
107
+ # Reads best with argument: 10.minutes.after(time)
108
+ def after(time = ::Time.now)
109
+ time + self
110
+ end
111
+ alias_method :since, :after # Reads best with argument: 10.minutes.since(time)
112
+ alias_method :from_now, :after # Reads best without arguments: 10.minutes.from_now
113
+ alias_method :later, :after # Reads best without arguments: 10.minutes.later
114
+
115
+ # Works with day in terms of weekdays.
116
+ def weekdays
117
+ Weekdays.new(self)
118
+ end
119
+ alias :weekday :weekdays
120
+
121
+ end
122
+ =end
123
+
124
+
125
+ # The Weekdays class provides useful weekday terminology to Numeric.
126
+
127
+ class Weekdays
128
+ WEEKDAYS = 1..5 # Monday is wday 1
129
+ ONE_DAY = 60 * 60 * 24
130
+
131
+ def initialize(n)
132
+ @n = n
133
+ end
134
+
135
+ def ago(time = ::Time.now)
136
+ step :down, time
137
+ end
138
+ alias :until :ago
139
+ alias :before :ago
140
+
141
+ def since(time = ::Time.now)
142
+ step :up, time
143
+ end
144
+ alias :from_now :since
145
+ alias :after :since
146
+
147
+ private
148
+
149
+ def step(direction, original_time)
150
+ result = original_time
151
+ time = ONE_DAY
152
+
153
+ compare = direction == :up ? ">" : "<"
154
+ time *= -1 if direction == :down
155
+
156
+ @n.times do
157
+ result += time until result.send(compare, original_time) && WEEKDAYS.member?(result.wday)
158
+ original_time = result
159
+ end
160
+ result
161
+ end
162
+ end
163
+
164
+
165
+ class Time
166
+
167
+ NEVER = Time.mktime(2038)
168
+ ZERO = Time.mktime(1972)
169
+
170
+ # CREDIT George Moschovitis
171
+
172
+ # This method calculates the days extrema given two time objects.
173
+ # start time is the given time1 at 00:00:00
174
+ # end time is the given time2 at 23:59:59:999
175
+ #
176
+ # Input:
177
+ # - the two times (if only time1 is provided then you get an extrema
178
+ # of exactly one day extrema.
179
+ #
180
+ # Output:
181
+ # - the time range. you can get the start/end times using
182
+ # range methods.
183
+ #
184
+ def self.days_extrema(time1, time2=nil)
185
+ time2 = time1 if (not time2.valid? Time)
186
+ time2 = NEVER if (time2 <= time1)
187
+ start_time = Time.self.start_of_day(time1)
188
+ end_time = self.end_of_day(time2)
189
+ return (start_time..end_time)
190
+ end
191
+
192
+ # Seconds since midnight: Time.now.seconds_since_midnight
193
+ def seconds_since_midnight
194
+ self.hour.hours + self.min.minutes + self.sec + (self.usec/1.0e+6)
195
+ end
196
+
197
+ # Returns a new Time representing the time a number of seconds ago.
198
+ # Do not use this method in combination with x.months, use months_ago instead!
199
+ def ago(seconds)
200
+ # This is basically a wrapper around the Numeric extension.
201
+ #seconds.until(self)
202
+ self - seconds
203
+ end
204
+
205
+ # Returns a new Time representing the time a number of seconds
206
+ # since the instance time. Do not use this method in combination
207
+ # with x.months, use months_since instead!
208
+ def since(seconds)
209
+ # This is basically a wrapper around the Numeric extension.
210
+ #seconds.since(self)
211
+ self + seconds
212
+ end
213
+ alias_method :in, :since
214
+
215
+ # Returns a new Time representing the time a number of specified
216
+ # months ago.
217
+ def months_ago(months)
218
+ if months >= self.month
219
+ change(:year => self.year - 1, :month => 12).months_ago(months - self.month)
220
+ else
221
+ change(:year => self.year, :month => self.month - months)
222
+ end
223
+ end
224
+
225
+ def months_since(months)
226
+ if months + self.month > 12
227
+ change(:year => self.year + 1, :month => 1).months_since(months - (self.month == 1 ? 12 : (self.month + 1)))
228
+ else
229
+ change(:year => self.year, :month => self.month + months)
230
+ end
231
+ end
232
+
233
+ # Returns a new Time representing the time a number of specified
234
+ # years ago.
235
+ def years_ago(years)
236
+ change(:year => self.year - years)
237
+ end
238
+
239
+ def years_since(years)
240
+ change(:year => self.year + years)
241
+ end
242
+
243
+ # Short-hand for months_ago(1)
244
+ def last_year
245
+ years_since(1)
246
+ end
247
+
248
+ # Short-hand for months_since(1)
249
+ def next_year
250
+ years_since(1)
251
+ end
252
+
253
+ # Short-hand for months_ago(1)
254
+ def last_month
255
+ months_ago(1)
256
+ end
257
+
258
+ # Short-hand for months_since(1)
259
+ def next_month
260
+ months_since(1)
261
+ end
262
+
263
+ # Returns a new Time representing the "start" of this week (Monday, 0:00)
264
+ def beginning_of_week
265
+ (self - self.wday.days).midnight + 1.day
266
+ end
267
+ alias :monday :beginning_of_week
268
+ alias :at_beginning_of_week :beginning_of_week
269
+
270
+ # Returns a new Time representing the start of the given
271
+ # day in next week (default is Monday).
272
+ def next_week(day = :monday)
273
+ days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2,
274
+ :thursday => 3, :friday => 4, :saturday => 5,
275
+ :sunday => 6 }
276
+ since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
277
+ end
278
+
279
+ # Set time to end of day
280
+ #def end_of_day
281
+ # return Time.mktime(year, month, day, 23, 59, 59, 999)
282
+ #end
283
+
284
+ # Returns a new Time representing the start of the day (0:00)
285
+ def beginning_of_day
286
+ self - self.seconds_since_midnight
287
+ end
288
+ alias :midnight :beginning_of_day
289
+ alias :at_midnight :beginning_of_day
290
+ alias :at_beginning_of_day :beginning_of_day
291
+ alias :start_of_day :beginning_of_day
292
+
293
+ # Returns a new Time representing the start of the month
294
+ # (1st of the month, 0:00)
295
+ def beginning_of_month
296
+ #self - ((self.mday-1).days + self.seconds_since_midnight)
297
+ change(:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
298
+ end
299
+ alias_method :at_beginning_of_month, :beginning_of_month
300
+
301
+ # Returns a new Time representing the start of the year (1st of january, 0:00)
302
+ def beginning_of_year
303
+ change(:month => 1,:day => 1,:hour => 0, :min => 0, :sec => 0, :usec => 0)
304
+ end
305
+ alias :at_beginning_of_year :beginning_of_year
306
+
307
+ # Convenience method which returns a new Time representing
308
+ # the time 1 day ago
309
+ def yesterday
310
+ self.ago(1.day)
311
+ end
312
+
313
+ # Convenience method which returns a new Time representing
314
+ # the time 1 day since the instance time
315
+ def tomorrow
316
+ self.since(1.day)
317
+ end
318
+
319
+ # Returns a new time at start of day
320
+ def to_start_of_day
321
+ return Time.mktime(year, month, day, 0, 0, 0, 0)
322
+ end
323
+
324
+ # Rrturns a new time at end of day
325
+ def to_end_of_day
326
+ return Time.mktime(year, month, day, 23, 59, 59, 999)
327
+ end
328
+
329
+ # Returns true only if day of time is included in the
330
+ # range (stime..etime). Only year days are checked.
331
+ def in_day_range?(stime=ZERO, etime=NEVER)
332
+ if (etime <= stime)
333
+ warn "invalid end time (#{etime} < #{stime})" if $DEBUG
334
+ etime = NEVER
335
+ end
336
+
337
+ stime = stime.to_start_of_day
338
+ etime = etime.to_end_of_day
339
+
340
+ return (stime..etime).include?(time)
341
+ end
342
+ end
343
+
344
+
345
+ # _____ _
346
+ # |_ _|__ ___| |_
347
+ # | |/ _ \/ __| __|
348
+ # | | __/\__ \ |_
349
+ # |_|\___||___/\__|
350
+ #
351
+
352
+ =begin testing
353
+
354
+ require 'test/unit'
355
+ #require 'mega/multiplier'
356
+
357
+ class NumericTest < Test::Unit::TestCase
358
+
359
+ #def test_micro_seconds
360
+ # assert_equal( 0.000001, 1.microsecond )
361
+ #end
362
+
363
+ #def test_milli_seconds
364
+ # assert_equal( 0.001, 1.millisecond )
365
+ #end
366
+
367
+ def test_seconds
368
+ assert_equal( 60**0, 1.seconds )
369
+ end
370
+
371
+ def test_minutes
372
+ assert_equal( 60**1, 1.minutes )
373
+ end
374
+
375
+ def test_hours
376
+ assert_equal( 60**2, 1.hours )
377
+ end
378
+
379
+ def test_days
380
+ assert_equal( 24*(60**2), 1.days )
381
+ end
382
+
383
+ def test_weeks
384
+ assert_equal( 7*24*(60**2), 1.weeks )
385
+ end
386
+
387
+ def test_fortnights
388
+ assert_equal( 14*24*(60**2), 1.fortnights )
389
+ end
390
+
391
+ def test_months
392
+ assert_equal( 30*24*(60**2), 1.months )
393
+ end
394
+
395
+ def test_years
396
+ assert_equal( 365*24*(60**2), 1.years )
397
+ end
398
+
399
+ def test_before
400
+ t = Time.now
401
+ assert_equal( t - 1.day, 1.day.before(t) )
402
+ end
403
+
404
+ def test_after
405
+ t = Time.now
406
+ assert_equal( t + 1.day, 1.day.after(t) )
407
+ end
408
+
409
+ end
410
+
411
+ class WeekdaysTest < Test::Unit::TestCase
412
+
413
+ MONDAY = Time.at(1165250000)
414
+ THURSDAY = Time.at(1165500000)
415
+ FRIDAY = Time.at(1165606025)
416
+
417
+ def test_weekday_after_monday
418
+ assert_equal 2, 1.weekday.since(MONDAY).wday
419
+ end
420
+
421
+ def test_weekday_after_friday
422
+ assert_equal 1, 1.weekday.after(FRIDAY).wday
423
+ end
424
+
425
+ def test_weekdays_before_friday
426
+ assert_equal 2, 3.weekdays.before(FRIDAY).wday
427
+ end
428
+
429
+ #def test_weekday_before_today
430
+ # Time.expects(:now).returns(THURSDAY)
431
+ # assert_equal 3, 1.weekday.ago.wday
432
+ #end
433
+
434
+ #def test_weekdays_after_today
435
+ # Time.expects(:now).returns(MONDAY)
436
+ # assert_equal 3, 2.weekday.from_now.wday
437
+ #end
438
+
439
+ end
440
+
441
+ =end
@@ -0,0 +1,112 @@
1
+ require 'stick/units/units'
2
+
3
+ module Stick
4
+ # Load conversion units.
5
+ class Converter
6
+ require("units-standard")
7
+ end
8
+ end
9
+
10
+
11
+ # Checkrun
12
+
13
+ =begin check
14
+
15
+ class A
16
+
17
+ include ::Units
18
+
19
+ def test
20
+ puts 1.bit/s + 8.bytes/s
21
+
22
+ puts((1.bit/s).to(byte/s))
23
+
24
+ puts 1.mile.to(feet)
25
+
26
+ puts 1.acre.to(yd**2)
27
+
28
+ puts 1.acre.to(sq_yd)
29
+
30
+ puts 1.gallon.to(L)
31
+
32
+ puts 1.lb.to(kg)
33
+
34
+ puts 1.m.s.to(m.s)
35
+
36
+ puts 1.sq_mi.to(km**2)
37
+
38
+ puts 1.mile.to(km)
39
+
40
+ #puts 1.usd.to(twd)
41
+
42
+ with_unit_converter(:uk) {
43
+ puts 1.cwt.to(lb)
44
+ }
45
+
46
+ with_unit_converter(:us) {
47
+ puts 1.cwt.to(lb)
48
+ }
49
+
50
+ puts 1.cwt(:uk).to(lb(:uk))
51
+ puts 1.cwt(:us).to(lb(:us))
52
+
53
+ puts Converter.current.lb
54
+
55
+ p Converter.registered_converters
56
+
57
+ #begin
58
+ # puts 1.try.to(usd)
59
+ #rescue TypeError
60
+ # p $!
61
+ #end
62
+
63
+ #puts 1.usd(:cex).to(twd(:cex))
64
+
65
+ puts 1.cwt(:uk).to(cwt(:us))
66
+ puts 1.cwt(:us).to(cwt(:uk))
67
+
68
+ with_unit_converter(:uk) {
69
+ puts 1.cwt(:uk).to(cwt(:us))
70
+ puts 1.cwt(:us).to(cwt(:uk))
71
+ }
72
+
73
+ p (1.m <=> 1.L)
74
+ p (1.m <=> 1.cm)
75
+
76
+ p((1.MB / s).to(kB / s))
77
+
78
+ with_unit_converter(:binary_iec_base) {
79
+ p((1.MB / s).to(kB / s))
80
+ }
81
+
82
+ p "m / s".to_unit
83
+ p "1 m / s".to_value
84
+
85
+ p "1 m / cm L".to_value.simplify
86
+
87
+ p "1 m / cm".to_value.to_f
88
+
89
+ p 1.m.to("cm")
90
+
91
+ p 1.m + "5cm"
92
+
93
+ p 1.m + 5.cm
94
+
95
+ p 5.cm + 1.m
96
+
97
+ p cm * m
98
+
99
+ p cm * "m"
100
+
101
+ p "-5mm".to_value
102
+
103
+ p "-5mm".to_value.abs
104
+
105
+ p ("5.0mm".to_value / 1).infinite?
106
+ end
107
+ end
108
+
109
+ A.new.test
110
+
111
+ =end
112
+