just_time 0.1.1
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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.md +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/just_time.gemspec +25 -0
- data/lib/just_time.rb +134 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6aec9eb51451cdafe294c8581bed1e22afbe1cf4
|
4
|
+
data.tar.gz: 474f870b572860db415c67fbfe02d290e563877c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a98dc5f5fa4e586335e2ad8b659137ac1ecacc77748dd6540188e8c98b8d32f57b2e8b3654698823914d1f061eadf08ac83513703a3d2dfe012af513c4879e2c
|
7
|
+
data.tar.gz: 2b11c2ba8034d0fa0d196365ae6bcd4793a21fce0208f4ad1cf36e93b24bcc485d0080f0f84e0d862be3d77641970d363dfa6e3dd03334c9bce8d3a3d4ede1c2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Andrew Jones
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
10
|
+
so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# JustTime
|
2
|
+
|
3
|
+
A class to represent a time object without a date. Ruby doesn't have one, and if you don't want to
|
4
|
+
rely on, eg, Sequel.SQLTime, then you have to roll your own.
|
5
|
+
|
6
|
+
Or you can use mine. ;)
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'just_time'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install just_time
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
The code is very short and hopefully the whole thing is pretty obvious. Some example code:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
|
32
|
+
jt1 = JustTime.new("12:34:56")
|
33
|
+
jt2 = JustTime.from_time(Time.now - 120)
|
34
|
+
jt3 = JustTime.now
|
35
|
+
|
36
|
+
jt4 = jt1 - jt2
|
37
|
+
puts jt2.to_s(:hhmm)
|
38
|
+
date = jt2.to_time(Date.today)
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
## Thanks
|
44
|
+
|
45
|
+
This code was developed, by me, during working hours at [James Hall & Co.
|
46
|
+
Ltd](https://www.jameshall.co.uk/). I'm incredibly greatful that they have permitted me to
|
47
|
+
open-source it.
|
48
|
+
|
49
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "just_time"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/just_time.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "just_time"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "just_time"
|
8
|
+
spec.version = JustTime::VERSION
|
9
|
+
spec.authors = ["Andy Jones"]
|
10
|
+
spec.email = ["andy@twosticksconsulting.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = %q|a little class to handle time-without-date|
|
13
|
+
spec.homepage = "https://bitbucket.org/andy-twosticks/just_time"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.8"
|
25
|
+
end
|
data/lib/just_time.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
|
5
|
+
##
|
6
|
+
# A class to represent a time object without a date
|
7
|
+
# Ruby doesn't have one, and I don't want to rely on, eg, Sequel.SQLTime.
|
8
|
+
#
|
9
|
+
class JustTime
|
10
|
+
include Comparable
|
11
|
+
|
12
|
+
VERSION = "0.1.1"
|
13
|
+
|
14
|
+
attr_reader :ssm # Seconds Since Midnight
|
15
|
+
|
16
|
+
class << self
|
17
|
+
|
18
|
+
def from_time(time)
|
19
|
+
return nil if time.nil?
|
20
|
+
secs = ( time - midnight(time) ).to_i
|
21
|
+
self.new(secs)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse(string)
|
25
|
+
return nil if (string.nil? || string =~ /\A\s*\Z/)
|
26
|
+
|
27
|
+
begin
|
28
|
+
parts = string.split(":").map{|s| Integer(s, 10) }
|
29
|
+
raise if parts.size < 2
|
30
|
+
rescue
|
31
|
+
raise ArgumentError,
|
32
|
+
"Bad parameters passed to JustTime.parse - '#{string}'"
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
self.new(*parts)
|
37
|
+
end
|
38
|
+
|
39
|
+
def now
|
40
|
+
secs = (Time.now - midnight).to_i
|
41
|
+
self.new(secs)
|
42
|
+
end
|
43
|
+
|
44
|
+
def midnight(time=Time.now)
|
45
|
+
Time.new(time.year, time.month, time.day)
|
46
|
+
end
|
47
|
+
|
48
|
+
end # of class << self
|
49
|
+
|
50
|
+
def initialize(*arg)
|
51
|
+
fail_bad_params("new", arg) unless arg.all?{|a| a.kind_of?(Fixnum) }
|
52
|
+
fail_bad_params("new", arg) if arg.size > 1 && arg.any?{|a| a < 0}
|
53
|
+
|
54
|
+
@ssm =
|
55
|
+
case arg.size
|
56
|
+
when 1
|
57
|
+
@ssm = arg.first
|
58
|
+
when 2
|
59
|
+
fail_bad_params("new", arg) if arg.first > 24 || arg.last > 59
|
60
|
+
@ssm = arg.last * 60 + arg.first * 60 * 60
|
61
|
+
when 3
|
62
|
+
fail_bad_params("new", arg) if arg[0] > 24 || arg[1] > 59 || arg[2] > 59
|
63
|
+
@ssm = arg[2] + arg[1] * 60 + arg[0] * 60 * 60
|
64
|
+
else
|
65
|
+
fail_bad_params("new", arg)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
def minutes
|
71
|
+
ssm / 60 % 60
|
72
|
+
end
|
73
|
+
|
74
|
+
alias mins minutes
|
75
|
+
alias minute minutes
|
76
|
+
|
77
|
+
def hours
|
78
|
+
ssm / (60 * 60)
|
79
|
+
end
|
80
|
+
|
81
|
+
alias hrs hours
|
82
|
+
alias hour hours
|
83
|
+
|
84
|
+
def seconds
|
85
|
+
ssm % 60
|
86
|
+
end
|
87
|
+
|
88
|
+
alias secs seconds
|
89
|
+
alias second seconds
|
90
|
+
|
91
|
+
##
|
92
|
+
# If you want to turn a JustTime into a Time, you might want to supply a date.
|
93
|
+
#
|
94
|
+
def to_time(date=Date.today)
|
95
|
+
Time.new(date.year, date.month, date.day, hours, minutes, seconds)
|
96
|
+
end
|
97
|
+
|
98
|
+
def to_s(mode=:hhmmss)
|
99
|
+
case mode
|
100
|
+
when :hhmm then "%02d:%02d" % [ hours, minutes ]
|
101
|
+
else "%02d:%02d:%02d" % [ hours, minutes, seconds ]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def inspect
|
106
|
+
"#<JustTime #{to_s}>"
|
107
|
+
end
|
108
|
+
|
109
|
+
def -(other)
|
110
|
+
o = other.kind_of?(JustTime) ? other.ssm : other
|
111
|
+
JustTime.new(ssm - o)
|
112
|
+
end
|
113
|
+
|
114
|
+
def +(other)
|
115
|
+
o = other.kind_of?(JustTime) ? other.ssm : other
|
116
|
+
JustTime.new(ssm + o)
|
117
|
+
end
|
118
|
+
|
119
|
+
def <=>(other)
|
120
|
+
return 0 unless other.kind_of? JustTime
|
121
|
+
ssm <=> other.ssm
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def fail_bad_params(method, para)
|
127
|
+
raise ArgumentError,
|
128
|
+
"Bad parameters passed to JustTime.#{method} - '#{para.inspect}'",
|
129
|
+
caller
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
end # of JustTime
|
134
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: just_time
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Jones
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.8'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- andy@twosticksconsulting.co.uk
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.md
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- just_time.gemspec
|
72
|
+
- lib/just_time.rb
|
73
|
+
homepage: https://bitbucket.org/andy-twosticks/just_time
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: a little class to handle time-without-date
|
97
|
+
test_files: []
|