birthday_integer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/birthday_integer.gemspec +23 -0
- data/lib/birthday_integer.rb +28 -0
- data/lib/birthday_integer/version.rb +3 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc3a0551bb125a0dc8ceaa97bc75c1d8b52f1b29
|
4
|
+
data.tar.gz: b44da202adb048458da9833882799dd20769ad18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eff4b693e9d340838f5b4542eed82dbb3bdd6aff84a7c634b28170f10735b341eeb8d27da0913e245b4d986cbc582f60739ffc1954e2a3b3440c213c821f9eb4
|
7
|
+
data.tar.gz: 8b10774d6e72a9d92f62d8fbe6488adf8146e869004e1b2e867d328c001fb8bb75b33f3cec9f689c305189302d0e3c29138dddbc90038d537c063f97f6c22599
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Isaac Betesh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# BirthdayInteger
|
2
|
+
|
3
|
+
Sometimes you need to handle dates in a year-agnostic date. For example, a birthday.
|
4
|
+
|
5
|
+
At its most basic level, we are not interested in storing a date, but rather an integer from 1 to 366.
|
6
|
+
|
7
|
+
In order to easily convert between this integer and a date, this gem provides a core extension to the Date class, that extracts the complexity of converting between dates and birthday integers.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'birthday_integer'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install birthday_integer
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Converting a Ruby Date to your birthday integer
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
your_birthday = Date.new(any_year, your_birthday_month, your_birthday_day)
|
31
|
+
your_birthday.leap_yday # Returns an integer value representing your birthday
|
32
|
+
```
|
33
|
+
|
34
|
+
Why is it called leap_yday? Because in a leap year, it's the same as your_birthday.yday.
|
35
|
+
|
36
|
+
What is it in a non-leap year? Until February 28th, it's the same as your_birthday.yday. Beginning March 1st, it's your_birthday.yday + 1.
|
37
|
+
|
38
|
+
### Converting your birthday integer to a Ruby Date
|
39
|
+
```ruby
|
40
|
+
require "birthday_integer"
|
41
|
+
```ruby
|
42
|
+
|
43
|
+
Suppose your birthday is February 1st. Your birthday integer is 32.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Date.from_leap_yday(32) # returns the calendar date of your birthday this year
|
47
|
+
```ruby
|
48
|
+
|
49
|
+
You could also pass an optional year.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
Date.from_leap_yday(32, 2011) # returns the calendar date of your birthday in 2011
|
53
|
+
```
|
54
|
+
|
55
|
+
Now, suppose your birthday is March 1st. Your birthday integer is 61.
|
56
|
+
|
57
|
+
"Why not 60?", you ask.
|
58
|
+
|
59
|
+
Because 60 means February 29th.
|
60
|
+
|
61
|
+
"But there is no February 29th in the year I was born!", you declare.
|
62
|
+
|
63
|
+
That may be true, but **SOME** years do have a February 29th, and you **DO** celebrate your birthday that year. (Don't you?)
|
64
|
+
|
65
|
+
Feb 29 obviously makes things a little more complicated.
|
66
|
+
```ruby
|
67
|
+
Date.from_leap_yday(60, 2012) # returns February 29th, 2012
|
68
|
+
Date.from_leap_yday(60, 2014) # raises an ArgumentError
|
69
|
+
```
|
70
|
+
|
71
|
+
"OK, I get it," you say after some thought. "If 61 means March 1st, and 59 means February 28th, then 60 isn't a valid date in 2014. But what if your birthday is really February 29th? When do you celebrate your birthday in 2014?"
|
72
|
+
|
73
|
+
Well, you have 2 choices:
|
74
|
+
|
75
|
+
#### Choice 1:
|
76
|
+
```ruby
|
77
|
+
Date.from_leap_yday_with_feb_29_birthdays_on_mar_1_in_common_years(60, 2014)
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Choice 2:
|
81
|
+
```ruby
|
82
|
+
ArgumentError # There is no February 29th in 2014 since it is not a leap year
|
83
|
+
```
|
84
|
+
|
85
|
+
It's your choice. Celebrate in March, or celebrate with an ArgumentError!
|
86
|
+
|
87
|
+
## Development
|
88
|
+
|
89
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
90
|
+
|
91
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it ( https://github.com/[my-github-username]/birthday_integer/fork )
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "birthday_integer"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'birthday_integer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "birthday_integer"
|
8
|
+
spec.version = BirthdayInteger::VERSION
|
9
|
+
spec.authors = ["Isaac Betesh"]
|
10
|
+
spec.email = ["iybetesh@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{I must tell you the truth: Your birthday is not a date. It is an integer.}
|
13
|
+
spec.homepage = "https://www.github.com/betesh/birthday_integer"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "birthday_integer/version"
|
2
|
+
require "date"
|
3
|
+
|
4
|
+
class Date
|
5
|
+
def leap_yday
|
6
|
+
yday + ((!leap? && yday >= 60) ? 1 : 0)
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def from_leap_yday(n,year=Date.today.year)
|
11
|
+
n = n.to_i
|
12
|
+
raise ArgumentError, "Cannot convert yday #{n} since it is not between 1 and 366" unless n >= 1 && n <= 366
|
13
|
+
|
14
|
+
if leap?(year) || n > 60
|
15
|
+
new(year,3,1) - 61 + n
|
16
|
+
elsif 60 == n
|
17
|
+
raise ArgumentError, "There is no February 29th in #{year} since it is not a leap year"
|
18
|
+
else
|
19
|
+
new(year,1,1) - 1 + n
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def from_leap_yday_with_feb_29_birthdays_on_mar_1_in_common_years(n, year=Date.today.year)
|
24
|
+
n = n + 1 if 60 == n && !leap?(year)
|
25
|
+
from_leap_yday(n,year)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: birthday_integer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Isaac Betesh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-19 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- iybetesh@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- bin/console
|
56
|
+
- bin/setup
|
57
|
+
- birthday_integer.gemspec
|
58
|
+
- lib/birthday_integer.rb
|
59
|
+
- lib/birthday_integer/version.rb
|
60
|
+
homepage: https://www.github.com/betesh/birthday_integer
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: 'I must tell you the truth: Your birthday is not a date. It is an integer.'
|
84
|
+
test_files: []
|