ruby-nuggets 0.2.9.266 → 0.3.0.275
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/README +1 -1
- data/lib/nuggets/numeric/duration.rb +109 -0
- data/lib/nuggets/version.rb +2 -2
- metadata +5 -4
data/README
CHANGED
@@ -0,0 +1,109 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of ruby-nuggets, some extensions to the Ruby programming #
|
5
|
+
# language. #
|
6
|
+
# #
|
7
|
+
# Copyright (C) 2007-2008 Jens Wille #
|
8
|
+
# #
|
9
|
+
# Authors: #
|
10
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
11
|
+
# #
|
12
|
+
# ruby-nuggets is free software; you can redistribute it and/or modify it #
|
13
|
+
# under the terms of the GNU General Public License as published by the Free #
|
14
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
15
|
+
# any later version. #
|
16
|
+
# #
|
17
|
+
# ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
|
18
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
20
|
+
# more details. #
|
21
|
+
# #
|
22
|
+
# You should have received a copy of the GNU General Public License along #
|
23
|
+
# with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
24
|
+
# #
|
25
|
+
###############################################################################
|
26
|
+
#++
|
27
|
+
|
28
|
+
class Numeric
|
29
|
+
|
30
|
+
# call-seq:
|
31
|
+
# num.hms => anArray
|
32
|
+
#
|
33
|
+
# Converts _num_ into hour, minute, and second portions.
|
34
|
+
def hms
|
35
|
+
raise ArgumentError, "negative duration #{self}" if self < 0
|
36
|
+
|
37
|
+
one_minute = 60
|
38
|
+
one_hour = 60 * one_minute
|
39
|
+
|
40
|
+
[((h,) = divmod(one_hour)).last.divmod(one_minute), h].reverse.flatten # *SCNR* ;-)
|
41
|
+
end
|
42
|
+
|
43
|
+
# call-seq:
|
44
|
+
# num.ymd => anArray
|
45
|
+
#
|
46
|
+
# Converts _num_ into year, month, and day portions.
|
47
|
+
def ymd
|
48
|
+
raise ArgumentError, "negative duration #{self}" if self < 0
|
49
|
+
|
50
|
+
one_day = 24 * 60 * 60
|
51
|
+
one_month = 30 * one_day
|
52
|
+
one_year = 365.25 * one_day
|
53
|
+
|
54
|
+
y, m = divmod(one_year)
|
55
|
+
m, d = m.divmod(one_month)
|
56
|
+
|
57
|
+
[y, m, d / one_day]
|
58
|
+
end
|
59
|
+
|
60
|
+
# call-seq:
|
61
|
+
# num.to_hms(precision = 0, labels = %w[h m s]) => aString
|
62
|
+
#
|
63
|
+
# Produces a stringified version of _num_'s time portions (cf. #hms),
|
64
|
+
# with the specified +precision+ for the seconds (treated as floating
|
65
|
+
# point). The individual parts are labelled as specified in the +labels+
|
66
|
+
# parameter (hours, minutes, seconds in that order). Leading parts with
|
67
|
+
# a value of zero are omitted.
|
68
|
+
#
|
69
|
+
# Examples:
|
70
|
+
# 180.to_hms #=> "3m0s"
|
71
|
+
# 180.75.to_hms #=> "3m1s"
|
72
|
+
# 180.75.to_hms(2) #=> "3m0.75s"
|
73
|
+
# 8180.to_hms #=> "2h16m20s"
|
74
|
+
# 8180.to_hms(0, %w[: :]) #=> "2:16:20"
|
75
|
+
def to_hms(precision = 0, labels = %w[h m s], time = hms)
|
76
|
+
h, m, s = time
|
77
|
+
|
78
|
+
h.zero? ? m.zero? ?
|
79
|
+
"%0.#{precision}f#{labels[2]}" % [s] :
|
80
|
+
"%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [m, s] :
|
81
|
+
"%d#{labels[0]}%d#{labels[1]}%0.#{precision}f#{labels[2]}" % [h, m, s]
|
82
|
+
end
|
83
|
+
|
84
|
+
# call-seq:
|
85
|
+
# num.to_ymd(include_hms = false, labels = %w[y m d]) => aString
|
86
|
+
#
|
87
|
+
# Produces a stringified version of _num_'s date portions (cf. #ymd),
|
88
|
+
# analogous to #to_hms. Includes time portions (cf. #hms) if +include_hms+
|
89
|
+
# is true.
|
90
|
+
def to_ymd(include_hms = false, labels = %w[y m d])
|
91
|
+
unless include_hms
|
92
|
+
to_hms(0, labels, ymd)
|
93
|
+
else
|
94
|
+
y, m, d = ymd
|
95
|
+
e = d.truncate
|
96
|
+
|
97
|
+
"#{to_hms(0, labels, [y, m, e])} #{((d - e) * 24 * 60 * 60).to_hms}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
if $0 == __FILE__
|
104
|
+
[123, 123456789, 0, 0.001, 1.23, 1234.56789].each { |n|
|
105
|
+
p n
|
106
|
+
p [n.hms, n.to_hms, n.to_hms(2)]
|
107
|
+
p [n.ymd, n.to_ymd, n.to_ymd(true)]
|
108
|
+
}
|
109
|
+
end
|
data/lib/nuggets/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0.275
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-08-15 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/nuggets/array/format.rb
|
60
60
|
- lib/nuggets/array/combination.rb
|
61
61
|
- lib/nuggets/numeric/between.rb
|
62
|
+
- lib/nuggets/numeric/duration.rb
|
62
63
|
- lib/nuggets/numeric/signum.rb
|
63
64
|
- lib/nuggets/numeric/limit.rb
|
64
65
|
- lib/nuggets/numeric/to_multiple.rb
|
@@ -82,10 +83,10 @@ has_rdoc: true
|
|
82
83
|
homepage: http://prometheus.rubyforge.org/ruby-nuggets
|
83
84
|
post_install_message:
|
84
85
|
rdoc_options:
|
85
|
-
- --title
|
86
|
-
- ruby-nuggets Application documentation
|
87
86
|
- --charset
|
88
87
|
- UTF-8
|
88
|
+
- --title
|
89
|
+
- ruby-nuggets Application documentation
|
89
90
|
- --main
|
90
91
|
- README
|
91
92
|
- --all
|