vidibus-timecode 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/.travis.yml +1 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +67 -0
- data/Rakefile +25 -0
- data/lib/vidibus-timecode.rb +2 -0
- data/lib/vidibus/timecode.rb +66 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/vidibus/timecode_spec.rb +93 -0
- data/vidibus-timecode.gemspec +31 -0
- metadata +165 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
script: "bundle exec rspec spec --format progress"
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Andre Pankratz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
= Vidibus::Timecode
|
2
|
+
|
3
|
+
Timecodes are a simple, yet powerful way to notate times.
|
4
|
+
|
5
|
+
This gem is part of {Vidibus}[http://vidibus.org], an open source toolset for building distributed (video) applications.
|
6
|
+
|
7
|
+
|
8
|
+
== Compatibility
|
9
|
+
|
10
|
+
http://travis-ci.org/vidibus/vidibus-timecode.png
|
11
|
+
|
12
|
+
Vidibus::Secure is tested against Ruby 1.8.7. {Build History}[http://travis-ci.org/vidibus/vidibus-timecode]
|
13
|
+
|
14
|
+
|
15
|
+
== Installation
|
16
|
+
|
17
|
+
Add the dependency to the Gemfile of your application:
|
18
|
+
|
19
|
+
gem "vidibus-timecode"
|
20
|
+
|
21
|
+
Then call bundle install on your console.
|
22
|
+
|
23
|
+
|
24
|
+
== Usage
|
25
|
+
|
26
|
+
Timecodes may be used to describe single, definite times or multiple, even random times. Each timecode is
|
27
|
+
comprised of a date and a time part, joined by "@":
|
28
|
+
#{date}@#{time}
|
29
|
+
|
30
|
+
In it's basic form, a timecode object will return a single time:
|
31
|
+
2011/12/07@21:00 # => Wed Dec 07 21:00:00 +0100 2011
|
32
|
+
|
33
|
+
But timecodes may also be used to define repeating times, like cron. Timcode will return the next future time:
|
34
|
+
7@21:00 # => next 7th of a month, e.g. Thu Jul 07 21:00:00 +0200 2011
|
35
|
+
|
36
|
+
When defining a range, Timecode will output a random time within range defined:
|
37
|
+
2011/12/07@21:00-23:55 # => e.g. Wed Dec 07 22:13:45 +0100 2011
|
38
|
+
|
39
|
+
Please note: Only the basic for has been implemented already.
|
40
|
+
|
41
|
+
|
42
|
+
== Examples
|
43
|
+
|
44
|
+
Some usage examples:
|
45
|
+
|
46
|
+
7@21 # each 7th of each month at 21:00
|
47
|
+
7@21:00 # each 7th of each month at 21:00
|
48
|
+
12/7@21:00 # each 7th of each december at 21:00
|
49
|
+
2011/12/07@21:00 # on 7th of december 2011 at 21:00
|
50
|
+
2011/12/07,23@21:00 # on 7th and 23rd of december 2011 at 21:00
|
51
|
+
2011/12/07-23@21:00 # between 7th and 23rd of december 2011 at 21:00
|
52
|
+
2011/11,12/07-23@21:00 # between 7th and 23rd of november and between 7th and 23rd of december 2011 at 21:00
|
53
|
+
2011/11-12/07-23@21:00 # between 7th and 23rd of november or december 2011 at 21:00
|
54
|
+
2011/12/07,2012/01/04@21:00,22:30 # on 7th of december 2011 and on 4th of january 2012 at 21:00 and 22:30
|
55
|
+
|
56
|
+
|
57
|
+
== TODO
|
58
|
+
|
59
|
+
* Allow range input
|
60
|
+
* Allow list input
|
61
|
+
* Allow precision option: :seconds or :miliseconds
|
62
|
+
* Add method #explain to print timecode in words
|
63
|
+
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2011 Andre Pankratz. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "bundler"
|
2
|
+
require "rdoc/task"
|
3
|
+
require "rspec"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
9
|
+
require "vidibus/timecode"
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
12
|
+
t.pattern = "spec/**/*_spec.rb"
|
13
|
+
t.rcov = true
|
14
|
+
t.rcov_opts = ["--exclude", "^spec,/gems/"]
|
15
|
+
end
|
16
|
+
|
17
|
+
Rake::RDocTask.new do |rdoc|
|
18
|
+
rdoc.rdoc_dir = "rdoc"
|
19
|
+
rdoc.title = "vidibus-sysinfo #{Vidibus::Timecode::VERSION}"
|
20
|
+
rdoc.rdoc_files.include("README*")
|
21
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
22
|
+
rdoc.options << "--charset=utf-8"
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => :rcov
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Vidibus
|
2
|
+
class Timecode
|
3
|
+
VERSION = "0.1.0"
|
4
|
+
|
5
|
+
YEAR_REGEXP = /(\d{3,4}(?:[,\-]\d{3,4})?)/
|
6
|
+
MONTH_REGEXP = /(\d{1,2}(?:[,\-]\d{1,2})?)/
|
7
|
+
DAY_REGEXP = /(\d{1,2}(?:[,\-]\d{1,2})?)/
|
8
|
+
TIME_REGEXP = /(\d{1,2}(?:[,\-]\d{1,2})?)/
|
9
|
+
REGEXP = /^(?:#{YEAR_REGEXP}\/)?(?:#{MONTH_REGEXP}\/)?#{DAY_REGEXP}@#{TIME_REGEXP}(?:\:#{TIME_REGEXP}){0,2}$/
|
10
|
+
|
11
|
+
class Error < StandardError; end
|
12
|
+
class InputError < Error; end
|
13
|
+
class FormatError < Error; end
|
14
|
+
|
15
|
+
def initialize(*args)
|
16
|
+
options = extract_options!(args)
|
17
|
+
@input = args.first or raise_input_error
|
18
|
+
process_input
|
19
|
+
end
|
20
|
+
|
21
|
+
def timecode
|
22
|
+
@timecode ||= @time.strftime("%Y/%m/%d@%H:%M")
|
23
|
+
end
|
24
|
+
|
25
|
+
def time
|
26
|
+
@time ||= parse_timecode
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def process_input
|
32
|
+
if @input.is_a?(Time)
|
33
|
+
@time = @input
|
34
|
+
elsif @input.is_a?(String)
|
35
|
+
@timecode = @input
|
36
|
+
time # trigger parsing of timecode for validation
|
37
|
+
else
|
38
|
+
raise_input_error
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def extract_options!(args)
|
43
|
+
args.last.is_a?(Hash) and args.last.instance_of?(Hash) ? args.pop : {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_timecode
|
47
|
+
if m = timecode.match(REGEXP)
|
48
|
+
year = m[1] || Time.now.strftime("%Y")
|
49
|
+
month = m[2] || Time.now.strftime("%m")
|
50
|
+
day = m[3]
|
51
|
+
hour = m[4].to_i
|
52
|
+
minute = m[5].to_i
|
53
|
+
|
54
|
+
Time.local(year, month, day, hour, minute).tap do |time|
|
55
|
+
time.in_time_zone if time.respond_to?(:in_time_zone)
|
56
|
+
end
|
57
|
+
else
|
58
|
+
raise(FormatError)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def raise_input_error
|
63
|
+
raise(InputError, "Please provide Time or Timecode input.")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "rspec"
|
6
|
+
require "rr"
|
7
|
+
require "vidibus-timecode"
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.mock_with :rr
|
11
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Vidibus::Timecode" do
|
4
|
+
let(:timecode) {"2011/12/07@21:00"}
|
5
|
+
let(:time) {Time.parse("2011-12-07 21:00")}
|
6
|
+
|
7
|
+
describe "initializing" do
|
8
|
+
it "should require arguments" do
|
9
|
+
expect {Vidibus::Timecode.new}.to raise_error(Vidibus::Timecode::InputError, "Please provide Time or Timecode input.")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should accept a Time" do
|
13
|
+
expect {Vidibus::Timecode.new(time)}.not_to raise_error(Vidibus::Timecode::InputError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should accept a Timecode string" do
|
17
|
+
expect {Vidibus::Timecode.new(timecode)}.not_to raise_error(Vidibus::Timecode::InputError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should raise an FormatError for invalid Timecode input" do
|
21
|
+
expect {Vidibus::Timecode.new("2011/12/07")}.to raise_error(Vidibus::Timecode::FormatError)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#time" do
|
26
|
+
let(:this) {Vidibus::Timecode.new(timecode)}
|
27
|
+
|
28
|
+
it "should return time input" do
|
29
|
+
Vidibus::Timecode.new(time).time.should === time
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should try to use Rails' #in_time_zone by default" do
|
33
|
+
mock.any_instance_of(Time).in_time_zone
|
34
|
+
this.time
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return a local time object by default" do
|
38
|
+
this.time.should eql(time.localtime)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#timecode" do
|
43
|
+
let(:this) {Vidibus::Timecode.new(time)}
|
44
|
+
|
45
|
+
it "should return the timecode input" do
|
46
|
+
Vidibus::Timecode.new(timecode).timecode.should === timecode
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return the timecode of a given time" do
|
50
|
+
this.timecode.should eql(timecode)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# describe "speed test" do
|
55
|
+
# let(:iterations) {100000}
|
56
|
+
# let(:year) {2011}
|
57
|
+
# let(:month) {12}
|
58
|
+
# let(:day) {7}
|
59
|
+
# let(:hour) {21}
|
60
|
+
# let(:minute) {4}
|
61
|
+
# let(:time) {Time.local(year, month, day, hour, minute)}
|
62
|
+
#
|
63
|
+
# it "of Time.local" do
|
64
|
+
# iterations.times do
|
65
|
+
# Time.local(year, month, day, hour, minute)
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# it "of Time.parse" do
|
70
|
+
# iterations.times do
|
71
|
+
# Time.parse("#{year}-#{month}-#{day} #{hour}:#{minute}")
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# it "of rescuing a failed method call" do
|
76
|
+
# iterations.times do
|
77
|
+
# begin
|
78
|
+
# time.invalid
|
79
|
+
# rescue NoMethodError
|
80
|
+
# time
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# it "of checking #respond_to?" do
|
86
|
+
# if time.respond_to?(:invalid)
|
87
|
+
# time.invalid
|
88
|
+
# else
|
89
|
+
# time
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
# end
|
93
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib/", __FILE__)
|
4
|
+
$:.unshift lib unless $:.include?(lib)
|
5
|
+
|
6
|
+
require "vidibus/timecode"
|
7
|
+
|
8
|
+
Gem::Specification.new do |s|
|
9
|
+
s.name = "vidibus-timecode"
|
10
|
+
s.version = Vidibus::Timecode::VERSION
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.authors = "Andre Pankratz"
|
13
|
+
s.email = "andre@vidibus.com"
|
14
|
+
s.homepage = "https://github.com/vidibus/vidibus-timecode"
|
15
|
+
s.summary = "Timecodes are a simple, yet powerful way to notate times."
|
16
|
+
s.description = "Timecodes may be used to describe single, definite times or multiple, even random times."
|
17
|
+
|
18
|
+
s.required_rubygems_version = ">= 1.3.6"
|
19
|
+
s.rubyforge_project = "vidibus-timecode"
|
20
|
+
|
21
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "rdoc"
|
24
|
+
s.add_development_dependency "rcov"
|
25
|
+
s.add_development_dependency "rspec", "~> 2"
|
26
|
+
s.add_development_dependency "rr"
|
27
|
+
|
28
|
+
s.files = `git ls-files`.split("\n")
|
29
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
30
|
+
s.require_path = 'lib'
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vidibus-timecode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andre Pankratz
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-09 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rdoc
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: rcov
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
type: :development
|
78
|
+
version_requirements: *id004
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: rspec
|
81
|
+
prerelease: false
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 7
|
88
|
+
segments:
|
89
|
+
- 2
|
90
|
+
version: "2"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: rr
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
107
|
+
description: Timecodes may be used to describe single, definite times or multiple, even random times.
|
108
|
+
email: andre@vidibus.com
|
109
|
+
executables: []
|
110
|
+
|
111
|
+
extensions: []
|
112
|
+
|
113
|
+
extra_rdoc_files: []
|
114
|
+
|
115
|
+
files:
|
116
|
+
- .gitignore
|
117
|
+
- .rspec
|
118
|
+
- .travis.yml
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE
|
121
|
+
- README.rdoc
|
122
|
+
- Rakefile
|
123
|
+
- lib/vidibus-timecode.rb
|
124
|
+
- lib/vidibus/timecode.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/vidibus/timecode_spec.rb
|
127
|
+
- vidibus-timecode.gemspec
|
128
|
+
has_rdoc: true
|
129
|
+
homepage: https://github.com/vidibus/vidibus-timecode
|
130
|
+
licenses: []
|
131
|
+
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
|
135
|
+
require_paths:
|
136
|
+
- lib
|
137
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
hash: 3
|
143
|
+
segments:
|
144
|
+
- 0
|
145
|
+
version: "0"
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
hash: 23
|
152
|
+
segments:
|
153
|
+
- 1
|
154
|
+
- 3
|
155
|
+
- 6
|
156
|
+
version: 1.3.6
|
157
|
+
requirements: []
|
158
|
+
|
159
|
+
rubyforge_project: vidibus-timecode
|
160
|
+
rubygems_version: 1.6.2
|
161
|
+
signing_key:
|
162
|
+
specification_version: 3
|
163
|
+
summary: Timecodes are a simple, yet powerful way to notate times.
|
164
|
+
test_files: []
|
165
|
+
|