timespans 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/README.md +1 -0
- data/Rakefile +1 -0
- data/lib/abstractive/timespans.rb +125 -0
- data/timespans.gemspec +20 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9361ec0c27153ba2e12efe1194f5995114e705a0
|
4
|
+
data.tar.gz: e2b93d1df70d2b62b38aa6b8b02333af93644c81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4cd612ca266ecc3b821a5874164cfef4824da0db309908cf1a5a5ccca3bf7d05184575dbf9dc8893e8cea61db644505f50f5488c47dd62f067f5a4e228320e30
|
7
|
+
data.tar.gz: 0ff24808dfecc5961233e77b1ecd118352865c02072b5cdc8365989b89703f5e2745a942b5022f69b3fa43493ef4551456c46d6e04c10a399f4d211a0aa07222
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
`Abstractive::TimeSpans`
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module Abstractive
|
5
|
+
class TimeSpans
|
6
|
+
module Methods
|
7
|
+
|
8
|
+
MINUTE = 60
|
9
|
+
HOUR = MINUTE*60
|
10
|
+
DAY = HOUR*24
|
11
|
+
|
12
|
+
def notated_time_length(span)
|
13
|
+
span = time_spans(span) unless span.is_a? Array
|
14
|
+
length = ""
|
15
|
+
length += "#{span[0]}d " if span[0] > 0
|
16
|
+
length += "#{span[1]}h " if span[1] > 0
|
17
|
+
length += "#{span[2]}m " if span[2] > 0
|
18
|
+
length += "#{span[3]}s "
|
19
|
+
length.strip
|
20
|
+
end
|
21
|
+
alias :readable_duration :notated_time_length
|
22
|
+
|
23
|
+
def day_decimal(span)
|
24
|
+
span = time_spans(span) if span.is_a? Fixnum
|
25
|
+
hms = span[1]*HOUR+span[2]*MINUTE+span[3]
|
26
|
+
span[0].to_f + (hms.to_f/DAY.to_f)
|
27
|
+
end
|
28
|
+
def time_spans(length)
|
29
|
+
days = (length / DAY).floor
|
30
|
+
length = length % DAY if days > 0
|
31
|
+
hours = (length / HOUR).floor
|
32
|
+
length = length % HOUR if hours > 0
|
33
|
+
minutes = (length / MINUTE).floor
|
34
|
+
length = length % MINUTE if minutes > 0
|
35
|
+
seconds = length
|
36
|
+
[days, hours, minutes, seconds]
|
37
|
+
end
|
38
|
+
def plus_span(base, add)
|
39
|
+
add = day_decimal(add) unless add.is_a?(Float)
|
40
|
+
_ = DateTime.jd(base.to_datetime.jd + add)
|
41
|
+
DateTime.strptime(_.strftime('%FT%T')+base.strftime('%:z'))
|
42
|
+
end
|
43
|
+
def minus_span(base, sub)
|
44
|
+
sub = day_decimal(sub) unless sub.is_a?(Float)
|
45
|
+
_ = DateTime.jd(base.to_datetime.jd - sub)
|
46
|
+
DateTime.strptime(_.strftime('%FT%T')+base.strftime('%:z'))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
include Methods
|
51
|
+
extend Methods
|
52
|
+
|
53
|
+
def initialize(i); @i = i end
|
54
|
+
def to_i; @i end
|
55
|
+
|
56
|
+
class << self
|
57
|
+
def at(text, format=STANDARD_FORMAT)
|
58
|
+
DateTime.strptime(text, format).to_time
|
59
|
+
rescue => ex
|
60
|
+
Abstractive[:logger].exception(ex,"Trouble turning string into DateTime and then Time object.")
|
61
|
+
end
|
62
|
+
def duration(start, finish)
|
63
|
+
finish.to_i - start.to_i
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Days < Abstractive::TimeSpans; def to_seconds; @i * DAY end end
|
70
|
+
class Hours < Abstractive::TimeSpans; def to_seconds; @i * HOUR end end
|
71
|
+
class Minutes < Abstractive::TimeSpans; def to_seconds; @i * MINUTE end end
|
72
|
+
class Seconds < Abstractive::TimeSpans; def to_seconds; @i * MINUTE end end
|
73
|
+
|
74
|
+
class Fixnum
|
75
|
+
def days; Days.new(self) end
|
76
|
+
def hours; Hours.new(self) end
|
77
|
+
def minutes; Minutes.new(self) end
|
78
|
+
def seconds; Seconds.new(self) end
|
79
|
+
end
|
80
|
+
|
81
|
+
class DateTime
|
82
|
+
alias old_subtract -
|
83
|
+
alias old_add +
|
84
|
+
def -(x)
|
85
|
+
case x
|
86
|
+
when Days; return Abstractive::TimeSpans.minus_span(self, x.to_seconds)
|
87
|
+
when Hours; return DateTime.new(year, month, day, hour-x.to_i, min, sec, strftime("%:z"))
|
88
|
+
when Minutes; return DateTime.new(year, month, day, hour, min-x.to_i, sec, strftime("%:z"))
|
89
|
+
when Seconds; return DateTime.new(year, month, day, hour, min, sec-x.to_i, strftime("%:z"))
|
90
|
+
else; return self.old_subtract(x)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
def +(x)
|
94
|
+
case x
|
95
|
+
when Days; return Abstractive::TimeSpans.plus_span(self, x.to_seconds)
|
96
|
+
when Hours; return DateTime.new(year, month, day, hour+x.to_i, min, sec, strftime("%:z"))
|
97
|
+
when Minutes; return DateTime.new(year, month, day, hour, min+x.to_i, sec, strftime("%:z"))
|
98
|
+
when Seconds; return DateTime.new(year, month, day, hour, min, sec+x.to_i, strftime("%:z"))
|
99
|
+
else; return self.old_add(x)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class Time
|
105
|
+
alias old_subtract -
|
106
|
+
alias old_add +
|
107
|
+
def -(x)
|
108
|
+
case x
|
109
|
+
when Days; return Abstractive::TimeSpans.minus_span(self, x.to_seconds).to_time
|
110
|
+
when Hours; return DateTime.new(year, month, day, hour-x.to_i, min, sec, strftime("%:z")).to_time
|
111
|
+
when Minutes; return DateTime.new(year, month, day, hour, min-x.to_i, sec, strftime("%:z")).to_time
|
112
|
+
when Seconds; return DateTime.new(year, month, day, hour, min, sec-x.to_i, strftime("%:z")).to_time
|
113
|
+
else; return self.old_subtract(x)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
def +(x)
|
117
|
+
case x
|
118
|
+
when Days; return Abstractive::TimeSpans.plus_span(self, x.to_seconds).to_time
|
119
|
+
when Hours; return DateTime.new(year, month, day, hour+x.to_i, min, sec, strftime("%:z")).to_time
|
120
|
+
when Minutes; return DateTime.new(year, month, day, hour, min+x.to_i, sec, strftime("%:z")).to_time
|
121
|
+
when Seconds; return DateTime.new(year, month, day, hour, min, sec+x.to_i, strftime("%:z")).to_time
|
122
|
+
else; return self.old_add(x)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/timespans.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = "timespans"
|
5
|
+
gem.version = "0.0.1"
|
6
|
+
gem.platform = Gem::Platform::RUBY
|
7
|
+
gem.summary = "Time delineation and organization utilities."
|
8
|
+
gem.description = "Time delineation and organization utilities."
|
9
|
+
gem.licenses = ["MIT"]
|
10
|
+
|
11
|
+
gem.authors = ["digitalextremist //"]
|
12
|
+
gem.email = ["code@extremist.digital"]
|
13
|
+
gem.homepage = "https://github.com/abstractive/timespans"
|
14
|
+
|
15
|
+
gem.required_ruby_version = ">= 1.9.2"
|
16
|
+
gem.required_rubygems_version = ">= 1.3.6"
|
17
|
+
|
18
|
+
gem.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|examples|spec|features)/}) }
|
19
|
+
gem.require_path = "lib"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timespans
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- digitalextremist //
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Time delineation and organization utilities.
|
14
|
+
email:
|
15
|
+
- code@extremist.digital
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/abstractive/timespans.rb
|
25
|
+
- timespans.gemspec
|
26
|
+
homepage: https://github.com/abstractive/timespans
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.9.2
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.3.6
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.4.8
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Time delineation and organization utilities.
|
50
|
+
test_files: []
|