Repository: tmlee/time_difference
Branch: master
Commit: bc617085ff22
Files: 12
Total size: 13.1 KB
Directory structure:
gitextract_s_u6vzjo/
├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── Gemfile
├── Gemfile.activesupport51
├── LICENSE
├── README.md
├── Rakefile
├── lib/
│ └── time_difference.rb
├── spec/
│ ├── spec_helper.rb
│ └── time_difference_spec.rb
└── time_difference.gemspec
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
================================================
FILE: .travis.yml
================================================
language: ruby
rvm:
- 2.2
- 2.3
- 2.4
- jruby-19mode # JRuby in 1.9 mode
# uncomment this line if your project needs to run something other than `rake`:
script: bundle exec rspec spec
gemfile:
- Gemfile.activesupport51
before_install:
- gem install bundler
================================================
FILE: CHANGELOG.md
================================================
## v0.4.2
* Support Time, DateTime, and Date class as input
## v0.3.1
* Support in_each_component as a shortcut for all time components
## v0.2.0
* Time difference will return absolute value
## v0.1.0
* First release at v0.1.0, here we go!
================================================
FILE: Gemfile
================================================
source 'https://rubygems.org'
# Specify your gem's dependencies in time_difference.gemspec
gemspec
================================================
FILE: Gemfile.activesupport51
================================================
source "https://rubygems.org"
gemspec
gem 'activesupport', '~> 5.1'
================================================
FILE: LICENSE
================================================
Copyright (c) 2013 TM Lee
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
================================================
FILE: README.md
================================================
[](https://travis-ci.org/tmlee/time_difference)
This latest version of the gem works with ActiveSupport 5.1. For prior version, check out [v0.6.x-activesupport42](https://github.com/tmlee/time_difference/tree/0.6.0-activesupport42)
# TimeDifference
TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'time_difference'
```
And then execute:
```bash
$ bundle install
```
## Usage
### Works for Time, DateTime, and Date
```ruby
# Time
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_years
=> 1.0
# DateTime
start_time = DateTime.new(2013,1)
end_time = DateTime.new(2014,1)
TimeDifference.between(start_time, end_time).in_years
=> 1.0
# Date
start_time = Date.new(2013,1)
end_time = Date.new(2014,1)
TimeDifference.between(start_time, end_time).in_years
=> 1.0
```
### Get the time difference in various units
```ruby
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_years
=> 1.0
TimeDifference.between(start_time, end_time).in_months
=> 12.0
TimeDifference.between(start_time, end_time).in_weeks
=> 52.14
TimeDifference.between(start_time, end_time).in_days
=> 365.0
TimeDifference.between(start_time, end_time).in_hours
=> 8760.0
TimeDifference.between(start_time, end_time).in_minutes
=> 525600.0
TimeDifference.between(start_time, end_time).in_seconds
=> 31536000.0
```
### Get the time difference in each component
```ruby
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_each_component
=> {:years=>1.0, :months=>12.0, :weeks=>52.14, :days=>365.0, :hours=>8760.0, :minutes=>525600.0, :seconds=>31536000.0}
```
### If you would like an overall estimated time component, use `in_general` _(not that accurate)_
```ruby
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).in_general
=> {:years=>0, :months=>12, :weeks=>0, :days=>5, :hours=>0, :minutes=>0, :seconds=>0}
```
### You can also get `in_general` as a human readable string, using `humanize`
```ruby
start_time = Time.new(2013,1)
end_time = Time.new(2014,1)
TimeDifference.between(start_time, end_time).humanize
=> "12 Months and 5 Days"
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
================================================
FILE: Rakefile
================================================
#!/usr/bin/env rake
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
task :default => :spec
RSpec::Core::RakeTask.new('spec')
================================================
FILE: lib/time_difference.rb
================================================
require 'rubygems'
require "active_support/all"
class TimeDifference
private_class_method :new
TIME_COMPONENTS = [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
def self.between(start_time, end_time)
new(start_time, end_time)
end
def in_years
in_component(:years)
end
def in_months
(@time_diff / (1.day * 30.42)).round(2)
end
def in_weeks
in_component(:weeks)
end
def in_days
in_component(:days)
end
def in_hours
in_component(:hours)
end
def in_minutes
in_component(:minutes)
end
def in_seconds
@time_diff
end
def in_each_component
Hash[TIME_COMPONENTS.map do |time_component|
[time_component, public_send("in_#{time_component}")]
end]
end
def in_general
remaining = @time_diff
Hash[TIME_COMPONENTS.map do |time_component|
if remaining > 0
rounded_time_component = (remaining / 1.send(time_component).seconds).round(2).floor
remaining -= rounded_time_component.send(time_component)
[time_component, rounded_time_component]
else
[time_component, 0]
end
end]
end
def humanize
diff_parts = []
in_general.each do |part,quantity|
next if quantity <= 0
part = part.to_s.humanize
if quantity <= 1
part = part.singularize
end
diff_parts << "#{quantity} #{part}"
end
last_part = diff_parts.pop
if diff_parts.empty?
return last_part
else
return [diff_parts.join(', '), last_part].join(' and ')
end
end
private
def initialize(start_time, end_time)
start_time = time_in_seconds(start_time)
end_time = time_in_seconds(end_time)
@time_diff = (end_time - start_time).abs
end
def time_in_seconds(time)
time.to_time.to_f
end
def in_component(component)
(@time_diff / 1.send(component)).round(2)
end
end
================================================
FILE: spec/spec_helper.rb
================================================
require 'rspec'
require 'time_difference'
RSpec.configure do |config|
config.formatter = 'documentation'
# Configure Timezone for proper tests
ENV['TZ'] = 'UTC'
end
================================================
FILE: spec/time_difference_spec.rb
================================================
require 'spec_helper'
describe TimeDifference do
def self.with_each_class(&block)
classes = [Time, Date, DateTime]
classes.each do |clazz|
context "with a #{clazz.name} class" do
instance_exec clazz, &block
end
end
end
describe ".between" do
with_each_class do |clazz|
it "returns a new TimeDifference instance in each component" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time)).to be_a(TimeDifference)
end
end
end
describe "#in_each_component" do
with_each_class do |clazz|
it "returns time difference in each component" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_each_component).to eql({years: 0.91, months: 10.98, weeks: 47.71, days: 334.0, hours: 8016.0, minutes: 480960.0, seconds: 28857600.0})
end
end
end
describe "#in_general" do
with_each_class do |clazz|
it "returns time difference in general that matches the total seconds" do
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_general).to eql({years: 1, months: 2, weeks: 0, days: 0, hours: 0, minutes: 0, seconds: 0})
end
end
end
describe "#humanize" do
with_each_class do |clazz|
it "returns a string representing the time difference from in_general" do
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year and 2 Months")
end
end
end
describe "#in_years" do
with_each_class do |clazz|
it "returns time difference in years based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
end
end
end
describe "#in_months" do
with_each_class do |clazz|
it "returns time difference in months based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
end
end
end
describe "#in_weeks" do
with_each_class do |clazz|
it "returns time difference in weeks based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
end
end
end
describe "#in_days" do
with_each_class do |clazz|
it "returns time difference in weeks based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
end
end
end
describe "#in_hours" do
with_each_class do |clazz|
it "returns time difference in hours based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
end
end
end
describe "#in_minutes" do
with_each_class do |clazz|
it "returns time difference in minutes based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_minutes).to eql(480960.0)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_minutes).to eql(480960.0)
end
end
end
describe "#in_seconds" do
with_each_class do |clazz|
it "returns time difference in seconds based on Wolfram Alpha" do
start_time = clazz.new(2011, 1)
end_time = clazz.new(2011, 12)
expect(TimeDifference.between(start_time, end_time).in_seconds).to eql(28857600.0)
end
it "returns an absolute difference" do
start_time = clazz.new(2011, 12)
end_time = clazz.new(2011, 1)
expect(TimeDifference.between(start_time, end_time).in_seconds).to eql(28857600.0)
end
end
end
end
================================================
FILE: time_difference.gemspec
================================================
# -*- encoding: utf-8 -*-
Gem::Specification.new do |gem|
gem.authors = ["TM Lee"]
gem.email = ["tmlee.ltm@gmail.com"]
gem.description = "TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds."
gem.summary = "TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds."
gem.homepage = ""
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "time_difference"
gem.require_paths = ["lib"]
gem.version = "0.7.0"
gem.license = 'MIT'
gem.add_runtime_dependency('activesupport', '~> 5.1')
gem.add_development_dependency('rspec', '~> 3.7.0')
gem.add_development_dependency('rake')
end
gitextract_s_u6vzjo/ ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── Gemfile.activesupport51 ├── LICENSE ├── README.md ├── Rakefile ├── lib/ │ └── time_difference.rb ├── spec/ │ ├── spec_helper.rb │ └── time_difference_spec.rb └── time_difference.gemspec
SYMBOL INDEX (16 symbols across 2 files)
FILE: lib/time_difference.rb
class TimeDifference (line 4) | class TimeDifference
method between (line 10) | def self.between(start_time, end_time)
method in_years (line 14) | def in_years
method in_months (line 18) | def in_months
method in_weeks (line 22) | def in_weeks
method in_days (line 26) | def in_days
method in_hours (line 30) | def in_hours
method in_minutes (line 34) | def in_minutes
method in_seconds (line 38) | def in_seconds
method in_each_component (line 42) | def in_each_component
method in_general (line 48) | def in_general
method humanize (line 61) | def humanize
method initialize (line 84) | def initialize(start_time, end_time)
method time_in_seconds (line 91) | def time_in_seconds(time)
method in_component (line 95) | def in_component(component)
FILE: spec/time_difference_spec.rb
function with_each_class (line 5) | def self.with_each_class(&block)
Condensed preview — 12 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (15K chars).
[
{
"path": ".gitignore",
"chars": 154,
"preview": "*.gem\n*.rbc\n.bundle\n.config\n.yardoc\nGemfile.lock\nInstalledFiles\n_yardoc\ncoverage\ndoc/\nlib/bundler/man\npkg\nrdoc\nspec/repo"
},
{
"path": ".travis.yml",
"chars": 269,
"preview": "language: ruby\nrvm:\n - 2.2\n - 2.3\n - 2.4\n - jruby-19mode # JRuby in 1.9 mode\n# uncomment this line if your project n"
},
{
"path": "CHANGELOG.md",
"chars": 243,
"preview": "## v0.4.2\n* Support Time, DateTime, and Date class as input\n\n## v0.3.1\n* Support in_each_component as a shortcut for all"
},
{
"path": "Gemfile",
"chars": 100,
"preview": "source 'https://rubygems.org'\n\n# Specify your gem's dependencies in time_difference.gemspec\ngemspec\n"
},
{
"path": "Gemfile.activesupport51",
"chars": 74,
"preview": "source \"https://rubygems.org\"\t\t\ngemspec\t\t\n\t\t\ngem 'activesupport', '~> 5.1'"
},
{
"path": "LICENSE",
"chars": 1062,
"preview": "Copyright (c) 2013 TM Lee\n\nMIT License\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of "
},
{
"path": "README.md",
"chars": 2764,
"preview": "[](https://travis-ci.org/tmlee/time_difference)\n\nThis la"
},
{
"path": "Rakefile",
"chars": 137,
"preview": "#!/usr/bin/env rake\nrequire \"bundler/gem_tasks\"\n\nrequire 'rspec/core/rake_task'\n\ntask :default => :spec\nRSpec::Core::Rak"
},
{
"path": "lib/time_difference.rb",
"chars": 1890,
"preview": "require 'rubygems'\nrequire \"active_support/all\"\n\nclass TimeDifference\n\n private_class_method :new\n\n TIME_COMPONENTS = "
},
{
"path": "spec/spec_helper.rb",
"chars": 176,
"preview": "require 'rspec'\nrequire 'time_difference'\n\nRSpec.configure do |config|\n config.formatter = 'documentation'\n\n # Con"
},
{
"path": "spec/time_difference_spec.rb",
"chars": 5529,
"preview": "require 'spec_helper'\n\ndescribe TimeDifference do\n\n def self.with_each_class(&block)\n classes = [Time, Date, DateTim"
},
{
"path": "time_difference.gemspec",
"chars": 1034,
"preview": "# -*- encoding: utf-8 -*-\nGem::Specification.new do |gem|\n gem.authors = [\"TM Lee\"]\n gem.email = [\"tmlee"
}
]
About this extraction
This page contains the full source code of the tmlee/time_difference GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 12 files (13.1 KB), approximately 4.0k tokens, and a symbol index with 16 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.