dohutil 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/doh/app/init_from_pwd.rb +4 -0
- data/lib/doh/app/util.rb +15 -0
- data/lib/doh/core_ext/bigdecimal.rb +4 -11
- data/lib/doh/core_ext/date.rb +2 -91
- data/lib/doh/core_ext/datewithtime.rb +52 -0
- data/lib/doh/current_date.rb +3 -3
- data/lib/doh/to_display.rb +6 -0
- data/test/test_date.rb +14 -0
- data/test/test_datewithtime.rb +13 -0
- metadata +11 -6
- data/lib/doh/app/common.rb +0 -10
- data/lib/doh/app/pwd.rb +0 -4
- data/lib/doh/core_ext/kernel.rb +0 -8
data/lib/doh/app/util.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'doh/root'
|
2
|
+
|
3
|
+
module DohApp
|
4
|
+
|
5
|
+
def self.require_custom_config
|
6
|
+
path = File.expand_path(File::join(Doh::root, 'config/dohapp.rb'))
|
7
|
+
require(path) if File.exist?(path)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.init_from_file(filepath)
|
11
|
+
Doh::find_root_from_file(filepath)
|
12
|
+
DohApp::require_custom_config
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -1,22 +1,15 @@
|
|
1
1
|
require 'bigdecimal'
|
2
|
+
require 'bigdecimal/util'
|
2
3
|
|
3
4
|
class Integer
|
5
|
+
# this is being added in 1.9.3, so remove it once that is the required version
|
4
6
|
def to_d
|
5
7
|
to_s.to_d
|
6
8
|
end
|
7
9
|
end
|
8
10
|
|
9
11
|
class BigDecimal
|
10
|
-
|
11
|
-
def to_s(format = 'F')
|
12
|
-
_original_to_s(format)
|
13
|
-
end
|
14
|
-
|
15
|
-
undef inspect
|
16
|
-
def inspect
|
17
|
-
to_s
|
18
|
-
end
|
19
|
-
|
12
|
+
# this is being added in 1.9.3, so remove it once that is the required version
|
20
13
|
def to_d
|
21
14
|
self
|
22
15
|
end
|
@@ -25,7 +18,7 @@ class BigDecimal
|
|
25
18
|
raise ArgumentError.new("digits_after_decimal must be > 0") unless digits_after_decimal > 0
|
26
19
|
return '0.' + ('0' * digits_after_decimal) if nan? || infinite?
|
27
20
|
|
28
|
-
retval = truncate(digits_after_decimal).to_s
|
21
|
+
retval = truncate(digits_after_decimal).to_s('F')
|
29
22
|
digits_needed = retval.index('.') + digits_after_decimal + 1 - retval.size
|
30
23
|
retval += ('0' * digits_needed) if digits_needed > 0
|
31
24
|
|
data/lib/doh/core_ext/date.rb
CHANGED
@@ -1,29 +1,10 @@
|
|
1
1
|
require 'date'
|
2
|
-
require 'time'
|
3
2
|
|
4
3
|
class Date
|
5
4
|
def self.days_in_month(year, month)
|
6
5
|
civil(year, month, -1).mday
|
7
6
|
end
|
8
7
|
|
9
|
-
def self.smart_parse(datestr)
|
10
|
-
if datestr =~ /\A(\d\d)\/(\d\d)\/(\d\d)\z/
|
11
|
-
month = $1.to_i
|
12
|
-
mday = $2.to_i
|
13
|
-
year = $3.to_i
|
14
|
-
result = Date.new(year, month, mday)
|
15
|
-
else
|
16
|
-
result = Date.parse(datestr)
|
17
|
-
end
|
18
|
-
|
19
|
-
if result.year < 50
|
20
|
-
result = Date.new(result.year + 2000, result.month, result.day)
|
21
|
-
elsif result.year < 100
|
22
|
-
result = Date.new(result.year + 1900, result.month, result.day)
|
23
|
-
end
|
24
|
-
result
|
25
|
-
end
|
26
|
-
|
27
8
|
undef inspect
|
28
9
|
def inspect
|
29
10
|
strftime('%F')
|
@@ -33,77 +14,7 @@ class Date
|
|
33
14
|
(wday > 0) && (wday < 6)
|
34
15
|
end
|
35
16
|
|
36
|
-
def
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.short_weekday_to_num(weekday)
|
41
|
-
@@short_days_of_week ||= Time::RFC2822_DAY_NAME.collect {|day| day.downcase}
|
42
|
-
@@short_days_of_week.index(weekday.downcase)
|
43
|
-
end
|
44
|
-
|
45
|
-
def years_since(date)
|
46
|
-
years_diff = year - date.year
|
47
|
-
if month < date.month || (month == date.month && day < date.day)
|
48
|
-
years_diff -= 1
|
49
|
-
end
|
50
|
-
years_diff
|
51
|
-
end
|
52
|
-
|
53
|
-
def make_datetime(hour = 0, min = 0, sec = 0)
|
54
|
-
DateTime.new(year, month, mday, hour, min, sec)
|
55
|
-
end
|
56
|
-
|
57
|
-
def add_months(months, new_day = nil)
|
58
|
-
total_month = month + months
|
59
|
-
new_month = total_month % 12
|
60
|
-
new_month = 12 if new_month == 0
|
61
|
-
new_year = year + ((total_month - 1) / 12)
|
62
|
-
calc_day = [mday, Date.days_in_month(new_year, new_month)].min
|
63
|
-
Date.new(new_year, new_month, new_day || calc_day)
|
64
|
-
end
|
65
|
-
|
66
|
-
def add_years(years)
|
67
|
-
new_year = year + years
|
68
|
-
calc_day = [mday, Date.days_in_month(new_year, month)].min
|
69
|
-
Date.new(new_year, month, calc_day)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class DateTime
|
74
|
-
DOHRUBY_SECONDS_IN_DAY = (24 * 60 * 60).freeze
|
75
|
-
|
76
|
-
def self.seconds_to_days(seconds)
|
77
|
-
seconds.to_f / DOHRUBY_SECONDS_IN_DAY.to_f
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.zow
|
81
|
-
obj = now
|
82
|
-
new(obj.year, obj.month, obj.mday, obj.hour, obj.min, obj.sec)
|
83
|
-
end
|
84
|
-
|
85
|
-
def inspect
|
86
|
-
strftime('%F %X')
|
87
|
-
end
|
88
|
-
|
89
|
-
def date_only
|
90
|
-
Date.new(year, month, mday)
|
91
|
-
end
|
92
|
-
|
93
|
-
def add_seconds(seconds)
|
94
|
-
self + Rational(seconds, DOHRUBY_SECONDS_IN_DAY)
|
95
|
-
end
|
96
|
-
|
97
|
-
def sub_seconds(seconds)
|
98
|
-
add_seconds(-seconds)
|
99
|
-
end
|
100
|
-
|
101
|
-
# subtract another DateTime object, return difference in seconds
|
102
|
-
def sub_dt(other)
|
103
|
-
((self - other) * DOHRUBY_SECONDS_IN_DAY).to_i
|
104
|
-
end
|
105
|
-
|
106
|
-
def make_datetime
|
107
|
-
self
|
17
|
+
def make_datetime(hour = 0, minute = 0, second = 0, offset = 0)
|
18
|
+
DateTime.new(year, month, mday, hour, minute, second, offset)
|
108
19
|
end
|
109
20
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
# these are sets of methods to make DateTime & Time objects more interchangeable
|
5
|
+
# they are not comprehensive, nor exact - for example, this DateTime utc method does not modify the receiver, whereas the Time utc method does
|
6
|
+
# they simply provide good enough compatibility for what I've encountered so far
|
7
|
+
module MakeDateTimeLikeTime
|
8
|
+
def utc
|
9
|
+
new_offset
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module MakeTimeLikeDateTime
|
14
|
+
def next_second(n = 1)
|
15
|
+
self + n
|
16
|
+
end
|
17
|
+
|
18
|
+
def prev_second(n = 1)
|
19
|
+
self + n
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class DateTime
|
24
|
+
include MakeDateTimeLikeTime
|
25
|
+
|
26
|
+
DOHRUBY_SECONDS_IN_DAY = (24 * 60 * 60).freeze
|
27
|
+
|
28
|
+
def self.seconds_to_days(seconds)
|
29
|
+
seconds.to_f / DOHRUBY_SECONDS_IN_DAY.to_f
|
30
|
+
end
|
31
|
+
|
32
|
+
def inspect
|
33
|
+
strftime('%F %X')
|
34
|
+
end
|
35
|
+
|
36
|
+
def next_second(n = 1)
|
37
|
+
self + (SECONDS_IN_DAY * n)
|
38
|
+
end
|
39
|
+
|
40
|
+
def prev_second(n = 1)
|
41
|
+
next_second(-n)
|
42
|
+
end
|
43
|
+
|
44
|
+
# subtract another DateTime object, return difference in seconds
|
45
|
+
def sub_dt(other)
|
46
|
+
((self - other) * DOHRUBY_SECONDS_IN_DAY).to_i
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Time
|
51
|
+
include MakeTimeLikeDateTime
|
52
|
+
end
|
data/lib/doh/current_date.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'doh/core_ext/
|
1
|
+
require 'doh/core_ext/datewithtime'
|
2
2
|
|
3
3
|
module Doh
|
4
4
|
|
@@ -6,10 +6,10 @@ module Doh
|
|
6
6
|
|
7
7
|
def self.current_date(dflt = Date.today)
|
8
8
|
return dflt if @@current_date_objs.empty?
|
9
|
-
@@current_date_objs.last.
|
9
|
+
@@current_date_objs.last.to_date
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.current_datetime(dflt = DateTime.
|
12
|
+
def self.current_datetime(dflt = DateTime.now.utc)
|
13
13
|
return dflt if @@current_date_objs.empty?
|
14
14
|
cdo = @@current_date_objs.last
|
15
15
|
return cdo if cdo.respond_to?(:hour)
|
data/lib/doh/to_display.rb
CHANGED
data/test/test_date.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/doh/core_ext/date'
|
3
|
+
|
4
|
+
module Doh
|
5
|
+
|
6
|
+
class Test_core_ext_date < MiniTest::Unit::TestCase
|
7
|
+
def test_weekday
|
8
|
+
Date.new(2007, 1, 15).upto(Date.new(2007, 1, 19)) { |date| assert(date.weekday?) }
|
9
|
+
assert(!Date.new(2007, 1, 13).weekday?)
|
10
|
+
assert(!Date.new(2007, 1, 14).weekday?)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/doh/core_ext/datewithtime'
|
3
|
+
|
4
|
+
module Doh
|
5
|
+
|
6
|
+
class Test_core_ext_datewithtime < MiniTest::Unit::TestCase
|
7
|
+
def test_next_second
|
8
|
+
assert_equal(DateTime.new(2009, 2, 10, 15, 30, 3), DateTime.new(2009, 2, 10, 15, 30, 2).next_second(1))
|
9
|
+
assert_equal(Time.new(2009, 2, 10, 15, 30, 3), Time.new(2009, 2, 10, 15, 30, 2).next_second(1))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dohutil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-02-
|
13
|
+
date: 2012-02-23 00:00:00.000000000Z
|
14
14
|
dependencies: []
|
15
15
|
description: Tiny utilities, packaged together so they don't each have their own gem.
|
16
16
|
email:
|
@@ -20,17 +20,17 @@ extensions: []
|
|
20
20
|
extra_rdoc_files:
|
21
21
|
- MIT-LICENSE
|
22
22
|
files:
|
23
|
-
- lib/doh/app/
|
24
|
-
- lib/doh/app/
|
23
|
+
- lib/doh/app/init_from_pwd.rb
|
24
|
+
- lib/doh/app/util.rb
|
25
25
|
- lib/doh/array_to_hash.rb
|
26
26
|
- lib/doh/class_basename.rb
|
27
27
|
- lib/doh/config.rb
|
28
28
|
- lib/doh/core_ext/bigdecimal.rb
|
29
29
|
- lib/doh/core_ext/class/force_deep_copy.rb
|
30
30
|
- lib/doh/core_ext/date.rb
|
31
|
+
- lib/doh/core_ext/datewithtime.rb
|
31
32
|
- lib/doh/core_ext/dir.rb
|
32
33
|
- lib/doh/core_ext/hash.rb
|
33
|
-
- lib/doh/core_ext/kernel.rb
|
34
34
|
- lib/doh/core_ext/string.rb
|
35
35
|
- lib/doh/current_date.rb
|
36
36
|
- lib/doh/env.rb
|
@@ -40,6 +40,8 @@ files:
|
|
40
40
|
- lib/doh/options.rb
|
41
41
|
- lib/doh/root.rb
|
42
42
|
- lib/doh/to_display.rb
|
43
|
+
- test/test_date.rb
|
44
|
+
- test/test_datewithtime.rb
|
43
45
|
- MIT-LICENSE
|
44
46
|
homepage: https://github.com/atpsoft/dohutil
|
45
47
|
licenses:
|
@@ -66,4 +68,7 @@ rubygems_version: 1.8.15
|
|
66
68
|
signing_key:
|
67
69
|
specification_version: 3
|
68
70
|
summary: assorted tiny utilities
|
69
|
-
test_files:
|
71
|
+
test_files:
|
72
|
+
- test/test_date.rb
|
73
|
+
- test/test_datewithtime.rb
|
74
|
+
has_rdoc:
|
data/lib/doh/app/common.rb
DELETED
data/lib/doh/app/pwd.rb
DELETED