date_time-duration 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/README +32 -0
- data/Rakefile +134 -0
- data/lib/date_time/duration.rb +69 -0
- data/spec/date_time-duration_spec.rb +51 -0
- metadata +62 -0
data/ChangeLog
ADDED
data/README
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
= date_time-duration
|
3
|
+
|
4
|
+
== Synopsis
|
5
|
+
|
6
|
+
require 'date_time'
|
7
|
+
dd = DateTime::Duration(DateTime.new(1984,12,26), DateTime.new(2007,12,27))
|
8
|
+
dd.years # => 23
|
9
|
+
|
10
|
+
== Description
|
11
|
+
You can handle datetime duration easy. If you use date_time-smart, it's more easy to do it.
|
12
|
+
|
13
|
+
== Installation
|
14
|
+
|
15
|
+
=== Archive Installation
|
16
|
+
|
17
|
+
rake install
|
18
|
+
|
19
|
+
=== Gem Installation
|
20
|
+
|
21
|
+
gem install date_time-duration
|
22
|
+
|
23
|
+
|
24
|
+
== Features/Problems
|
25
|
+
|
26
|
+
This library does not care about leap seconds.
|
27
|
+
|
28
|
+
== Copyright
|
29
|
+
|
30
|
+
Author:: Keiji, Yoshimi <walf443 at gmail.com>
|
31
|
+
Copyright:: Copyright (c) 2007 Keiji, Yoshimi
|
32
|
+
License:: you can redistribute it and/or modify it under the same terms as Ruby itself.
|
data/Rakefile
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'rake/contrib/sshpublisher'
|
10
|
+
require 'fileutils'
|
11
|
+
include FileUtils
|
12
|
+
|
13
|
+
NAME = "date_time-duration"
|
14
|
+
AUTHOR = "Keiji, Yoshimi"
|
15
|
+
EMAIL = "walf443 at gmail.com"
|
16
|
+
DESCRIPTION = "Handle datetime duration easy"
|
17
|
+
RUBYFORGE_PROJECT = "akasakarb"
|
18
|
+
RUBYFORGE_PROJECT_ID = 4314
|
19
|
+
RUBYFORGE_PACKAGE_ID = 6501
|
20
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
21
|
+
BIN_FILES = %w( )
|
22
|
+
VERS = "0.0.1"
|
23
|
+
|
24
|
+
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
25
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
26
|
+
RDOC_OPTS = [
|
27
|
+
'--title', "#{NAME} documentation",
|
28
|
+
"--charset", "utf-8",
|
29
|
+
"--opname", "index.html",
|
30
|
+
"--line-numbers",
|
31
|
+
"--main", "README",
|
32
|
+
"--inline-source",
|
33
|
+
]
|
34
|
+
|
35
|
+
task :default => [:test]
|
36
|
+
task :package => [:clean]
|
37
|
+
|
38
|
+
Rake::TestTask.new("test") do |t|
|
39
|
+
t.libs << "spec"
|
40
|
+
t.pattern = "spec/**/*_spec.rb"
|
41
|
+
t.verbose = true
|
42
|
+
end
|
43
|
+
|
44
|
+
spec = Gem::Specification.new do |s|
|
45
|
+
s.name = NAME
|
46
|
+
s.version = VERS
|
47
|
+
s.platform = Gem::Platform::RUBY
|
48
|
+
s.has_rdoc = true
|
49
|
+
s.extra_rdoc_files = ["README", "ChangeLog"]
|
50
|
+
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
|
51
|
+
s.summary = DESCRIPTION
|
52
|
+
s.description = DESCRIPTION
|
53
|
+
s.author = AUTHOR
|
54
|
+
s.email = EMAIL
|
55
|
+
s.homepage = HOMEPATH
|
56
|
+
s.executables = BIN_FILES
|
57
|
+
s.rubyforge_project = RUBYFORGE_PROJECT
|
58
|
+
s.bindir = "bin"
|
59
|
+
s.require_path = "lib"
|
60
|
+
s.autorequire = ""
|
61
|
+
s.test_files = Dir["spec/*_spec.rb"]
|
62
|
+
|
63
|
+
#s.add_dependency('activesupport', '>=1.3.1')
|
64
|
+
#s.required_ruby_version = '>= 1.8.2'
|
65
|
+
|
66
|
+
s.files = %w(README ChangeLog Rakefile) +
|
67
|
+
Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
|
68
|
+
Dir.glob("ext/**/*.{h,c,rb}") +
|
69
|
+
Dir.glob("examples/**/*.rb") +
|
70
|
+
Dir.glob("tools/*.rb")
|
71
|
+
|
72
|
+
s.extensions = FileList["ext/**/extconf.rb"].to_a
|
73
|
+
end
|
74
|
+
|
75
|
+
Rake::GemPackageTask.new(spec) do |p|
|
76
|
+
p.need_tar = true
|
77
|
+
p.gem_spec = spec
|
78
|
+
end
|
79
|
+
|
80
|
+
task :install do
|
81
|
+
name = "#{NAME}-#{VERS}.gem"
|
82
|
+
sh %{rake package}
|
83
|
+
sh %{sudo gem install pkg/#{name}}
|
84
|
+
end
|
85
|
+
|
86
|
+
task :uninstall => [:clean] do
|
87
|
+
sh %{sudo gem uninstall #{NAME}}
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
Rake::RDocTask.new do |rdoc|
|
92
|
+
rdoc.rdoc_dir = 'html'
|
93
|
+
rdoc.options += RDOC_OPTS
|
94
|
+
rdoc.template = "resh"
|
95
|
+
#rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
96
|
+
if ENV['DOC_FILES']
|
97
|
+
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
|
98
|
+
else
|
99
|
+
rdoc.rdoc_files.include('README', 'ChangeLog')
|
100
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
101
|
+
rdoc.rdoc_files.include('ext/**/*.c')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "Publish to RubyForge"
|
106
|
+
task :rubyforge => [:rdoc, :package] do
|
107
|
+
require 'rubyforge'
|
108
|
+
Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'walf443').upload
|
109
|
+
end
|
110
|
+
|
111
|
+
desc 'Package and upload the release to rubyforge.'
|
112
|
+
task :release => [:clean, :package] do |t|
|
113
|
+
require 'rubyforge'
|
114
|
+
v = VERS or abort "Must supply VERSION=x.y.z"
|
115
|
+
abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
|
116
|
+
pkg = "pkg/#{NAME}-#{VERS}"
|
117
|
+
|
118
|
+
rf = RubyForge.new
|
119
|
+
puts "Logging in"
|
120
|
+
rf.login
|
121
|
+
|
122
|
+
c = rf.userconfig
|
123
|
+
# c["release_notes"] = description if description
|
124
|
+
# c["release_changes"] = changes if changes
|
125
|
+
c["preformatted"] = true
|
126
|
+
|
127
|
+
files = [
|
128
|
+
"#{pkg}.tgz",
|
129
|
+
"#{pkg}.gem"
|
130
|
+
].compact
|
131
|
+
|
132
|
+
puts "Releasing #{NAME} v. #{VERS}"
|
133
|
+
rf.add_release RUBYFORGE_PROJECT_ID, RUBYFORGE_PACKAGE_ID, VERS, *files
|
134
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'date'
|
2
|
+
# SYNOPSYS
|
3
|
+
# dt = DateTime.new(2007, 12, 25)
|
4
|
+
# birth = DateTime.new(1984, 12, 26)
|
5
|
+
# dur = DaTeTime::Duration.new(birth, dt)
|
6
|
+
# dur.years.to_i # => 22
|
7
|
+
class DateTime::Duration
|
8
|
+
def initialize from, to
|
9
|
+
@flag = ( from <= to ) ? 1 : -1
|
10
|
+
@from, @to = ( from <= to ) ? [from, to] : [ to, from ]
|
11
|
+
@duration = to.ajd - from.ajd
|
12
|
+
end
|
13
|
+
|
14
|
+
def == anothor
|
15
|
+
( ( self.days == anothor.days ) && ( self.months == anothor.months ) )
|
16
|
+
end
|
17
|
+
|
18
|
+
# It doesn't care about leap seconds.
|
19
|
+
def seconds
|
20
|
+
@duration * 24 * 60 * 60
|
21
|
+
end
|
22
|
+
|
23
|
+
def minutes
|
24
|
+
@duration * 24 * 60
|
25
|
+
end
|
26
|
+
|
27
|
+
def hours
|
28
|
+
@duration * 24
|
29
|
+
end
|
30
|
+
|
31
|
+
def days
|
32
|
+
@duration
|
33
|
+
end
|
34
|
+
|
35
|
+
def weeks
|
36
|
+
@duration / 7
|
37
|
+
end
|
38
|
+
|
39
|
+
# This method return not Rational but Fixnum.
|
40
|
+
def months
|
41
|
+
if @months.nil?
|
42
|
+
i = 0
|
43
|
+
while ( ( @from >> i ) <= @to ) do
|
44
|
+
i += 1
|
45
|
+
end
|
46
|
+
|
47
|
+
@months = ( @flag * ( i - 1 ) )
|
48
|
+
end
|
49
|
+
|
50
|
+
return @months
|
51
|
+
end
|
52
|
+
|
53
|
+
def years
|
54
|
+
months / 12
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.new2 from, hash
|
58
|
+
duration = Hash.new(0)
|
59
|
+
duration[:months] = ( hash[:years] || 0 ) * 12 + ( hash[:months] || 0 )
|
60
|
+
duration[:days] = Rational(( hash[:weeks] || 0 ) * 7 + ( hash[:days] || 0), 1)
|
61
|
+
duration[:seconds] = Rational(
|
62
|
+
( hash[:hours] || 0 ) * 60 * 60 + ( hash[:minutes] || 0 ) * 60 + ( hash[:seconds] || 0 ), 24 * 60 * 60
|
63
|
+
)
|
64
|
+
|
65
|
+
to = ( ( from + ( duration[:days] + duration[:seconds] ) ) >> duration[:months] )
|
66
|
+
|
67
|
+
new(from, to)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'date_time/duration'
|
3
|
+
|
4
|
+
# FIXME: oops... it's not easy to read. I want RSpec::Base.
|
5
|
+
describe DateTime::Duration do
|
6
|
+
before do
|
7
|
+
@birth = DateTime.new(1984, 12, 26)
|
8
|
+
@birth_after_a_year_before_one_day = DateTime.new(1985,12,25)
|
9
|
+
@birth_after_a_year = DateTime.new(1985, 12,26)
|
10
|
+
@dt = DateTime.new(2007, 12, 25)
|
11
|
+
@dt2 = DateTime.new(2007, 12, 26)
|
12
|
+
@dd = DateTime::Duration.new(@birth, @dt)
|
13
|
+
@dd2 = DateTime::Duration.new(@birth, @dt2)
|
14
|
+
@dd3 = DateTime::Duration.new(@birth, @birth_after_a_year_before_one_day)
|
15
|
+
@dd4 = DateTime::Duration.new(@birth, @birth_after_a_year )
|
16
|
+
@dd5 = DateTime::Duration.new(@birth_after_a_year, @birth)
|
17
|
+
end
|
18
|
+
|
19
|
+
it %{ should convert duration in days properly} do
|
20
|
+
@dd.days.to_i.should == 8399
|
21
|
+
@dd2.days.to_i.should == 8400
|
22
|
+
@dd3.days.to_i.should == 364
|
23
|
+
@dd4.days.to_i.should == 365
|
24
|
+
@dd5.days.to_i.should == -365
|
25
|
+
end
|
26
|
+
|
27
|
+
it %{ should convert duration in months properly} do
|
28
|
+
@dd.months.should == 275
|
29
|
+
@dd2.months.should == 276
|
30
|
+
@dd3.months.should == 11
|
31
|
+
@dd4.months.should == 12
|
32
|
+
@dd5.months.should == -12
|
33
|
+
end
|
34
|
+
|
35
|
+
it %{ should convert duration in years properly} do
|
36
|
+
@dd.years.should == 22
|
37
|
+
@dd2.years.should == 23
|
38
|
+
@dd3.years.should == 0
|
39
|
+
@dd4.years.should == 1
|
40
|
+
@dd5.years.should == -1
|
41
|
+
end
|
42
|
+
|
43
|
+
it %{ should make from data} do
|
44
|
+
DateTime::Duration.new2(@birth, :years => 22, :months => 11, :days => 30).should == @dd
|
45
|
+
DateTime::Duration.new2(@birth, :years => 23, :days => -1).should == @dd
|
46
|
+
DateTime::Duration.new2(@birth, :years => 23).should == @dd2
|
47
|
+
DateTime::Duration.new2(@birth, :years => 1, :days => -1).should == @dd3
|
48
|
+
DateTime::Duration.new2(@birth, :years => 1).should == @dd4
|
49
|
+
DateTime::Duration.new2(@birth_after_a_year, :years => -1)
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: date_time-duration
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-12-29 00:00:00 +09:00
|
8
|
+
summary: Handle datetime duration easy
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: walf443 at gmail.com
|
12
|
+
homepage: http://akasakarb.rubyforge.org
|
13
|
+
rubyforge_project: akasakarb
|
14
|
+
description: Handle datetime duration easy
|
15
|
+
autorequire: ""
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Keiji, Yoshimi
|
31
|
+
files:
|
32
|
+
- README
|
33
|
+
- ChangeLog
|
34
|
+
- Rakefile
|
35
|
+
- lib/date_time
|
36
|
+
- lib/date_time/duration.rb
|
37
|
+
test_files:
|
38
|
+
- spec/date_time-duration_spec.rb
|
39
|
+
rdoc_options:
|
40
|
+
- --title
|
41
|
+
- date_time-duration documentation
|
42
|
+
- --charset
|
43
|
+
- utf-8
|
44
|
+
- --opname
|
45
|
+
- index.html
|
46
|
+
- --line-numbers
|
47
|
+
- --main
|
48
|
+
- README
|
49
|
+
- --inline-source
|
50
|
+
- --exclude
|
51
|
+
- ^(examples|extras)/
|
52
|
+
extra_rdoc_files:
|
53
|
+
- README
|
54
|
+
- ChangeLog
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
dependencies: []
|
62
|
+
|