dates_toolbox 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +33 -0
- data/.travis.yml +59 -0
- data/CHANGELOG.md +5 -0
- data/CODEOWNERS +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +169 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dates_toolbox.gemspec +29 -0
- data/lib/dates_toolbox/overloads.rb +74 -0
- data/lib/dates_toolbox/version.rb +3 -0
- data/lib/dates_toolbox.rb +100 -0
- data/spec/dates_toolbox_spec.rb +167 -0
- data/spec/spec_helper.rb +30 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af81faf21e2b88623f3532a4e9f69ed3e21e2425a2dc359fd9e409b236e162ca
|
4
|
+
data.tar.gz: 4f1c9cde3c2432d62a2d8a48394563018a65630d038f7d23db2bbcc94e694e3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 880202aa483f59cd2384c01efc81d0789ed52412de62ddc295c854329b472b827c6ede861e13f92ea21526032311bf14eea57192786d7eca811d0afa9927e6fa
|
7
|
+
data.tar.gz: aeabf9b1a5f0b1c8c5c4fdabe2fb8dca7398dbf7200cb587cb53824e6b3bb29595ecbcef4c898d988f80ff839f31afe61e9de7b0d5836bc5839254e7e88e6e3c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Layout/IndentationWidth:
|
2
|
+
Width: 4
|
3
|
+
|
4
|
+
Metrics/AbcSize:
|
5
|
+
Max: 50
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/BlockLength:
|
11
|
+
Max: 40
|
12
|
+
|
13
|
+
Metrics/CyclomaticComplexity:
|
14
|
+
Max: 20
|
15
|
+
|
16
|
+
Metrics/MethodLength:
|
17
|
+
Max: 60
|
18
|
+
|
19
|
+
Metrics/PerceivedComplexity:
|
20
|
+
Max: 20
|
21
|
+
|
22
|
+
Style/GlobalVars:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/RaiseArgs:
|
26
|
+
EnforcedStyle: compact
|
27
|
+
|
28
|
+
Style/RedundantBegin:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/WordArray:
|
32
|
+
EnforcedStyle: brackets
|
33
|
+
|
data/.travis.yml
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
sudo: required
|
2
|
+
matrix:
|
3
|
+
include:
|
4
|
+
- language: ruby
|
5
|
+
rvm: 2.4.4
|
6
|
+
before_install:
|
7
|
+
- gem install bundler -v 1.17.3
|
8
|
+
- language: ruby
|
9
|
+
rvm: 2.4.4
|
10
|
+
before_install:
|
11
|
+
- gem install bundler -v 2.0.1
|
12
|
+
- language: ruby
|
13
|
+
rvm: 2.5.3
|
14
|
+
before_install:
|
15
|
+
- gem install bundler -v 2.0.1
|
16
|
+
- language: ruby
|
17
|
+
rvm: 2.6.1
|
18
|
+
before_install:
|
19
|
+
- gem install bundler -v 2.0.1
|
20
|
+
- language: ruby
|
21
|
+
env: SKIP_INTERPRETER=true
|
22
|
+
rvm: 2.4.4
|
23
|
+
before_install:
|
24
|
+
- git clone https://github.com/TravisToolbox/rubocop-travis.git
|
25
|
+
install:
|
26
|
+
- ./rubocop-travis/install.sh
|
27
|
+
script:
|
28
|
+
- ./rubocop-travis/scan.sh
|
29
|
+
- language: ruby
|
30
|
+
env: SKIP_INTERPRETER=true
|
31
|
+
rvm: 2.5.3
|
32
|
+
before_install:
|
33
|
+
- git clone https://github.com/TravisToolbox/rubocop-travis.git
|
34
|
+
install:
|
35
|
+
- ./rubocop-travis/install.sh
|
36
|
+
script:
|
37
|
+
- ./rubocop-travis/scan.sh
|
38
|
+
- language: ruby
|
39
|
+
env: SKIP_INTERPRETER=true
|
40
|
+
rvm: 2.6.1
|
41
|
+
before_install:
|
42
|
+
- git clone https://github.com/TravisToolbox/rubocop-travis.git
|
43
|
+
install:
|
44
|
+
- ./rubocop-travis/install.sh
|
45
|
+
script:
|
46
|
+
- ./rubocop-travis/scan.sh
|
47
|
+
- language: ruby
|
48
|
+
rvm: 2.6.1
|
49
|
+
before_install:
|
50
|
+
- mkdir travis
|
51
|
+
- git clone https://github.com/TravisToolbox/awesomebot-travis.git travis/awesomebot
|
52
|
+
install:
|
53
|
+
- ./travis/awesomebot/install.sh
|
54
|
+
script:
|
55
|
+
- ./travis/awesomebot/scan.sh
|
56
|
+
notifications:
|
57
|
+
email: false
|
58
|
+
slack:
|
59
|
+
secure: CyFyhrnfUORiEE9AvBbnCCbeSLbvXgGLt2yylWqHoDuB3314podeU833WUUILJqlpFrUBan2SmP8eYFbifRfi9xnIRhVeChqssKlOM6aP1N3Xwvw7r8o7i10C02cWgnXvRyhdWnr0Mgk22V32k9teUl2s9M1Ga8gHM9rtOmBxuqwJ0lXdV0Mnb95v1xyY1PoUQGttn9eXodphkM514xiASQ0Ud8Wh+Eppdh/vMQ4TmkHH2oe/Oipw5mVN+8+j2TQD/hejXhAD9dmqRbjvdE8dYljXQFRABlyAWjNT1oMhvL12TFEhwlEXYeE2XxFMuk7f1G1ANyw0+Tn9Lh5LGSTUM8tbMpl8MX3cDA++J+rwo62P+rF4mZjd0sre3oftuGAaB2FXp1QFnKl5iuPCfEojLkq03alSLaUZLe32G0BbVQlEf4nqefF/7eQcEscGG7ipO1X4Jlo8aKSB8nW/pPIVDYxxU0muOchS+ZiX76x49urYvGNbDOyvLNcaso7IBKMDKt69f2efFktVOeGNFd9c1HrZKIs9hf8GUda51mCY4VDm/y03UZN6jd7G6wfzWwxdNwmqWLxUCEBxL3NKf378VogqGUjvRryshYRcVm/E++vUviTHsPAPWjUsN81W3gG7m9qQog0rPoaRGkWNil/40M8+QhBfJY9CTqlRy0zLCg=
|
data/CHANGELOG.md
ADDED
data/CODEOWNERS
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at wolf@tgwolf.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Tim Gurney aka Wolf
|
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,169 @@
|
|
1
|
+
[![Build Status](https://img.shields.io/travis/WolfSoftware/dates_toolbox/master.svg)](https://travis-ci.org/WolfSoftware/dates_toolbox)
|
2
|
+
[![Software License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
|
3
|
+
[![Release](https://img.shields.io/github/release/wolfsoftware/dates_toolbox.svg)](https://github.com/wolfsoftware/dates_toolbox/releases/latest)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/dates_toolbox.svg)](https://badge.fury.io/rb/dates_toolbox)
|
5
|
+
[![Github commits (since latest release)](https://img.shields.io/github/commits-since/wolfsoftware/dates_toolbox/latest.svg)](https://github.com/wolfsoftware/dates_toolbox/commits)
|
6
|
+
[![GitHub repo size in bytes](https://img.shields.io/github/repo-size/wolfsoftware/dates_toolbox.svg)](https://github.com/wolfsoftware/dates_toolbox)
|
7
|
+
[![GitHub contributors](https://img.shields.io/github/contributors/wolfsoftware/dates_toolbox.svg)](https://github.com/wolfsoftware/dates_toolbox)
|
8
|
+
|
9
|
+
# Dates Toolbox
|
10
|
+
|
11
|
+
dates_toolbox is a fully feature gem full of utilities for manipulating dates.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'dates_toolbox'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install dates_toolbox
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Basic
|
32
|
+
|
33
|
+
The following methods are available.
|
34
|
+
|
35
|
+
1. the_day = The numeric day part of a given date
|
36
|
+
2. the_month = The numeric month part of a given date
|
37
|
+
3. the_year = The numeric year part of a given date
|
38
|
+
4. day_of_the_week (dotw) = The day of the week for a given date (Sunday = 0, Saturday = 6)
|
39
|
+
4. day_of_the_week_name (dotwn) = The name of the day of the week for a given date
|
40
|
+
5. day_of_the_year (doty) = The day of the year for a given date
|
41
|
+
6. days_between = The number of days between to days (excluding both given days)
|
42
|
+
7. weekends = Arrays of Date objects for weekends between given dates (including both given dates)
|
43
|
+
8. weekends_string = Array of date strings for weekends between given dates (including both given dates)
|
44
|
+
7. weekdays = Arrays of Date objects for weekdays between given dates (including both given dates)
|
45
|
+
8. weekdays_string = Array of date strings for weekdays between given dates (including both given dates)
|
46
|
+
|
47
|
+
|
48
|
+
### Advanced
|
49
|
+
|
50
|
+
All of the methods will default to a UK date format (%d/%m/%Y), however it is also possible to set a custom date format in one of 2 different ways.
|
51
|
+
|
52
|
+
```
|
53
|
+
d = Date.new
|
54
|
+
|
55
|
+
# Override/reset the class level default for all methods.
|
56
|
+
d.format = '%m-%d-%Y'
|
57
|
+
|
58
|
+
# or
|
59
|
+
|
60
|
+
# Override the default on a method by method basis
|
61
|
+
d.the_day('6-23-1912', '%m-%d-%Y')
|
62
|
+
```
|
63
|
+
|
64
|
+
### Examples
|
65
|
+
|
66
|
+
```
|
67
|
+
d = Date.new
|
68
|
+
|
69
|
+
puts d.the_day('23/6/1912')
|
70
|
+
# or
|
71
|
+
puts '23/6/1912'.the_day
|
72
|
+
# 1
|
73
|
+
|
74
|
+
puts d.the_month('23/6/1912')
|
75
|
+
# or
|
76
|
+
puts '23/6/1912'.the_month
|
77
|
+
# 6
|
78
|
+
|
79
|
+
puts d.the_year('23/6/1912')
|
80
|
+
# or
|
81
|
+
puts '23/6/1912'.the_year
|
82
|
+
# 1912
|
83
|
+
|
84
|
+
puts d.day_of_the_week('23/6/1912')
|
85
|
+
# or
|
86
|
+
puts '23/6/1912'.day_of_the_week
|
87
|
+
# 0
|
88
|
+
|
89
|
+
puts d.day_of_the_week_name('23/6/1912')
|
90
|
+
# or
|
91
|
+
puts '23/6/1912'.day_of_the_week_name
|
92
|
+
# Sunday
|
93
|
+
|
94
|
+
puts d.day_of_the_year('23/6/1912')
|
95
|
+
# or
|
96
|
+
puts '23/6/1912'.day_of_the_year
|
97
|
+
# 175
|
98
|
+
|
99
|
+
puts d.days_between('23/6/1912', '7/6/1954')
|
100
|
+
# or
|
101
|
+
puts '23/6/1912'.days_between('7/6/1954')
|
102
|
+
# 15324
|
103
|
+
|
104
|
+
puts d.weekends('1/1/2000', '31/1/2000')
|
105
|
+
# or
|
106
|
+
puts '1/1/2000'.weekends('31/1/2000')
|
107
|
+
# Array of date objects
|
108
|
+
|
109
|
+
puts d.weekends_string('1/1/2000', '31/1/2000')
|
110
|
+
# or
|
111
|
+
puts '1/1/2000'.weekends_string('31/1/2000')
|
112
|
+
# ["01/01/2000", "02/01/2000", "08/01/2000", "09/01/2000", "15/01/2000", "16/01/2000", "22/01/2000", "23/01/2000", "29/01/2000", "30/01/2000"]
|
113
|
+
|
114
|
+
puts d.weekdays('1/1/2000', '15/1/2000')
|
115
|
+
# or
|
116
|
+
puts '1/1/2000'.weekdays('15/1/2000')
|
117
|
+
# Array of date objects
|
118
|
+
|
119
|
+
puts d.weekdays_string('1/1/2000', '15/1/2000')
|
120
|
+
#
|
121
|
+
puts '1/1/2000'.weekdays_string('15/1/2000')
|
122
|
+
# ["03/01/2000", "04/01/2000", "05/01/2000", "06/01/2000", "07/01/2000", "10/01/2000", "11/01/2000", "12/01/2000", "13/01/2000", "14/01/2000"]
|
123
|
+
```
|
124
|
+
|
125
|
+
## Method Aliases
|
126
|
+
|
127
|
+
1. day_of_the_week is aliased to dotw
|
128
|
+
2. day_of_the_week_name is aliased to dotwn
|
129
|
+
3. day_of_the_year is aliased to doty
|
130
|
+
|
131
|
+
## Development
|
132
|
+
|
133
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
134
|
+
|
135
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
136
|
+
|
137
|
+
## Testing
|
138
|
+
|
139
|
+
For local testing make sure that you run `bundle exec rspec spec` and then `rake install` to install the gem locally.
|
140
|
+
|
141
|
+
## Contributing
|
142
|
+
|
143
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WolfSoftware/dates_toolbox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
144
|
+
|
145
|
+
## License
|
146
|
+
|
147
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
148
|
+
|
149
|
+
## Code of Conduct
|
150
|
+
|
151
|
+
Everyone interacting in the dates_toolbox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/WolfSoftware/dates_toolbox/blob/master/CODE_OF_CONDUCT.md).
|
152
|
+
|
153
|
+
## To-Do List
|
154
|
+
|
155
|
+
- [ ] current_date/tody
|
156
|
+
- [ ] Short/Long day and month name options
|
157
|
+
- [ ] get_ordinal
|
158
|
+
- [ ] is_valid_date
|
159
|
+
- [ ] is_leap_year
|
160
|
+
- [ ] days_in_month / days_in_year
|
161
|
+
- [ ] week_of_the_year
|
162
|
+
- [ ] julian_dte
|
163
|
+
- [ ] is_future_date/is_past_date
|
164
|
+
- [ ] quarter_of_the_year
|
165
|
+
- [ ] end/start of this/last/next week
|
166
|
+
- [ ] end/start of this/last/next month
|
167
|
+
- [ ] next/prev_weekday
|
168
|
+
- [ ] days/weeks ahead/behind
|
169
|
+
- [ ] weeks in a month (working weeks)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dates_toolbox"
|
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
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "dates_toolbox/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "dates_toolbox"
|
7
|
+
spec.version = DatesToolbox::VERSION
|
8
|
+
spec.authors = ["Tim Gurney aka Wolf"]
|
9
|
+
spec.email = ["wolf@tgwolf.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{A full featured rubygem for manipulating dates.}
|
12
|
+
spec.description = %q{A full featured rubygem for manipulating dates.}
|
13
|
+
spec.homepage = "https://github.com/WolfSoftware/dates_toolbox"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
|
18
|
+
#spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
# `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
#end
|
21
|
+
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", ">= 1.17", "< 3.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
#
|
2
|
+
# Overload the string class
|
3
|
+
#
|
4
|
+
class String
|
5
|
+
def the_day(format = '%d/%m/%Y')
|
6
|
+
d = Date.new
|
7
|
+
d.the_day(self, format)
|
8
|
+
end
|
9
|
+
|
10
|
+
def the_month(format = '%d/%m/%Y')
|
11
|
+
d = Date.new
|
12
|
+
d.the_month(self, format)
|
13
|
+
end
|
14
|
+
|
15
|
+
def the_year(format = '%d/%m/%Y')
|
16
|
+
d = Date.new
|
17
|
+
d.the_year(self, format)
|
18
|
+
end
|
19
|
+
|
20
|
+
def dotw(format = '%d/%m/%Y')
|
21
|
+
d = Date.new
|
22
|
+
d.day_of_the_week(self, format)
|
23
|
+
end
|
24
|
+
|
25
|
+
def day_of_the_week(format = '%d/%m/%Y')
|
26
|
+
d = Date.new
|
27
|
+
d.day_of_the_week(self, format)
|
28
|
+
end
|
29
|
+
|
30
|
+
def dotwn(format = '%d/%m/%Y')
|
31
|
+
d = Date.new
|
32
|
+
d.day_of_the_week_name(self, format)
|
33
|
+
end
|
34
|
+
|
35
|
+
def day_of_the_week_name(format = '%d/%m/%Y')
|
36
|
+
d = Date.new
|
37
|
+
d.day_of_the_week_name(self, format)
|
38
|
+
end
|
39
|
+
|
40
|
+
def doty(format = '%d/%m/%Y')
|
41
|
+
d = Date.new
|
42
|
+
d.day_of_the_year(self, format)
|
43
|
+
end
|
44
|
+
|
45
|
+
def day_of_the_year(format = '%d/%m/%Y')
|
46
|
+
d = Date.new
|
47
|
+
d.day_of_the_year(self, format)
|
48
|
+
end
|
49
|
+
|
50
|
+
def days_between(end_date, format = '%d/%m/%Y')
|
51
|
+
d = Date.new
|
52
|
+
d.days_between(self, end_date, format)
|
53
|
+
end
|
54
|
+
|
55
|
+
def weekends(end_date, format = '%d/%m/%Y')
|
56
|
+
d = Date.new
|
57
|
+
d.weekends(self, end_date, format)
|
58
|
+
end
|
59
|
+
|
60
|
+
def weekends_string(end_date, format = '%d/%m/%Y')
|
61
|
+
d = Date.new
|
62
|
+
d.weekends_string(self, end_date, format)
|
63
|
+
end
|
64
|
+
|
65
|
+
def weekdays(end_date, format = '%d/%m/%Y')
|
66
|
+
d = Date.new
|
67
|
+
d.weekdays(self, end_date, format)
|
68
|
+
end
|
69
|
+
|
70
|
+
def weekdays_string(end_date, format = '%d/%m/%Y')
|
71
|
+
d = Date.new
|
72
|
+
d.weekdays_string(self, end_date, format)
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'dates_toolbox/version'
|
2
|
+
require 'dates_toolbox/overloads'
|
3
|
+
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
#
|
7
|
+
# Docs to follow
|
8
|
+
#
|
9
|
+
class Date
|
10
|
+
attr_accessor :format
|
11
|
+
|
12
|
+
# rubocop:disable Lint/DuplicateMethods
|
13
|
+
def format
|
14
|
+
@format || '%d/%m/%Y'
|
15
|
+
end
|
16
|
+
# rubocop:enable Lint/DuplicateMethods
|
17
|
+
|
18
|
+
def test_format
|
19
|
+
(parse_date('1/1/1970', format).wday == 6)
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_date(date1, format_string)
|
23
|
+
begin
|
24
|
+
return Date.strptime(date1, format_string)
|
25
|
+
rescue ArgumentError => e
|
26
|
+
puts "Invalid date #{date1} with format #{format}"
|
27
|
+
throw e
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def the_day(date1, format_string = format)
|
32
|
+
parse_date(date1, format_string).day
|
33
|
+
end
|
34
|
+
|
35
|
+
def the_month(date1, format_string = format)
|
36
|
+
parse_date(date1, format_string).month
|
37
|
+
end
|
38
|
+
|
39
|
+
def the_year(date1, format_string = format)
|
40
|
+
parse_date(date1, format_string).year
|
41
|
+
end
|
42
|
+
|
43
|
+
def dotw(date1, format_string = format)
|
44
|
+
day_of_the_week(date1, format_string)
|
45
|
+
end
|
46
|
+
|
47
|
+
def day_of_the_week(date1, format_string = format)
|
48
|
+
parse_date(date1, format_string).wday
|
49
|
+
end
|
50
|
+
|
51
|
+
def dotwn(date1, format_string = format)
|
52
|
+
day_of_the_week_name(date1, format_string)
|
53
|
+
end
|
54
|
+
|
55
|
+
def day_of_the_week_name(date1, format_string = format)
|
56
|
+
Date::DAYNAMES[day_of_the_week(date1, format_string)]
|
57
|
+
end
|
58
|
+
|
59
|
+
def doty(date1, format_string = format)
|
60
|
+
day_of_the_year(date1, format_string)
|
61
|
+
end
|
62
|
+
|
63
|
+
def day_of_the_year(date1, format_string = format)
|
64
|
+
parse_date(date1, format_string).yday
|
65
|
+
end
|
66
|
+
|
67
|
+
def days_between(date1, date2, format_string = format)
|
68
|
+
d1 = parse_date(date1, format_string)
|
69
|
+
d2 = parse_date(date2, format_string)
|
70
|
+
|
71
|
+
d2.mjd - d1.mjd
|
72
|
+
end
|
73
|
+
|
74
|
+
def weekends(date1, date2, format_string = format)
|
75
|
+
start_date = parse_date(date1, format_string)
|
76
|
+
end_date = parse_date(date2, format_string)
|
77
|
+
|
78
|
+
# day of the week in 0-6. Sunday is day-of-week 0; Saturday is day-of-week 6.
|
79
|
+
my_days = [0, 6]
|
80
|
+
|
81
|
+
(start_date..end_date).to_a.select { |k| my_days.include?(k.wday) }
|
82
|
+
end
|
83
|
+
|
84
|
+
def weekends_string(date1, date2, format_string = format)
|
85
|
+
weekends(date1, date2, format_string).map { |n| n.strftime(format) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def weekdays(date1, date2, format_string = format)
|
89
|
+
start_date = parse_date(date1, format_string)
|
90
|
+
end_date = parse_date(date2, format_string)
|
91
|
+
|
92
|
+
# day of the week in 0-6. Sunday is day-of-week 0; Saturday is day-of-week 6.
|
93
|
+
my_days = (1..5).to_a
|
94
|
+
(start_date..end_date).to_a.select { |k| my_days.include?(k.wday) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def weekdays_string(date1, date2, format_string = format)
|
98
|
+
weekdays(date1, date2, format_string).map { |n| n.strftime(format) }
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# rubocop:disable Metrics/BlockLength
|
2
|
+
|
3
|
+
require 'dates_toolbox'
|
4
|
+
|
5
|
+
RSpec.describe DatesToolbox do
|
6
|
+
it 'has a version number' do
|
7
|
+
expect(DatesToolbox::VERSION).not_to be nil
|
8
|
+
end
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
@d = Date.new
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'Testing extending date class' do
|
15
|
+
context 'Test format setting' do
|
16
|
+
it 'says true for testing format after setting to %Y' do
|
17
|
+
@d.format = '%Y'
|
18
|
+
expect(@d.test_format).to eql(true)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'says false for testing format after setting to %Q' do
|
22
|
+
@d.format = '%Q'
|
23
|
+
expect(@d.test_format).to eql(false)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'Test basic functionality' do
|
28
|
+
context 'Full/Default method names' do
|
29
|
+
it 'says 23 June 1912 is a day 1' do
|
30
|
+
expect(@d.the_day('23/6/1912')).to eql(23)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'says 23 June 1912 is a month 6' do
|
34
|
+
expect(@d.the_month('23/6/1912')).to eql(6)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'says 23 June 1912 is a year 1912' do
|
38
|
+
expect(@d.the_year('23/6/1912')).to eql(1912)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'says 23 June 1912 is day 0' do
|
42
|
+
expect(@d.day_of_the_week('23/6/1912')).to eql(0)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'says 23 June 1912 is a Sunday' do
|
46
|
+
expect(@d.day_of_the_week_name('23/6/1912')).to eql('Sunday')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'says 23 June 1912 is day of year 175' do
|
50
|
+
expect(@d.day_of_the_year('23/6/1912')).to eql(175)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'says 15324 for days between 23 June 1912 and 7 June 1954' do
|
54
|
+
expect(@d.days_between('23/6/1912', '7/6/1954')).to eql(15_324)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
context 'Aliased method names' do
|
58
|
+
it 'says 23 June 1912 is day 0' do
|
59
|
+
expect(@d.dotw('23/6/1912')).to eql(0)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'says 23 June 1912 is a Sunday' do
|
63
|
+
expect(@d.dotwn('23/6/1912')).to eql('Sunday')
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'says 23 June 1912 is day of year 175' do
|
67
|
+
expect(@d.doty('23/6/1912')).to eql(175)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'Test complex functionality' do
|
73
|
+
it 'returns an array for weekends in the year 2000' do
|
74
|
+
expect(@d.weekends('1/1/2000', '31/12/2000')).to be_an_instance_of(Array)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'says ["01/01/2000", "02/01/2000", "08/01/2000", "09/01/2000", "15/01/2000", "16/01/2000", "22/01/2000", "23/01/2000", "29/01/2000", "30/01/2000"] for weekends in the year Jan 2000' do
|
78
|
+
expect(@d.weekends_string('1/1/2000', '31/1/2000')).to match_array(['01/01/2000', '02/01/2000', '08/01/2000', '09/01/2000', '15/01/2000', '16/01/2000', '22/01/2000', '23/01/2000', '29/01/2000', '30/01/2000'])
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'returns an array for weekdays in the year 2000' do
|
82
|
+
expect(@d.weekdays('1/1/2000', '31/12/2000')).to be_an_instance_of(Array)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'says ["03/01/2000", "04/01/2000", "05/01/2000", "06/01/2000", "07/01/2000", "10/01/2000", "11/01/2000", "12/01/2000", "13/01/2000", "14/01/2000"] for weekdays in the first 2 weeks Jan 2000' do
|
86
|
+
expect(@d.weekdays_string('1/1/2000', '14/1/2000')).to match_array(['03/01/2000', '04/01/2000', '05/01/2000', '06/01/2000', '07/01/2000', '10/01/2000', '11/01/2000', '12/01/2000', '13/01/2000', '14/01/2000'])
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'Test error handling' do
|
91
|
+
it 'says Invalid date 25/12/2000 with format %m/%d/%Y' do
|
92
|
+
begin
|
93
|
+
expect(@d.day_of_the_year('25/12/2000', '%m/%d/%Y')).to eql('Invalid date 25/12/2000 with format %m/%d/%Y')
|
94
|
+
rescue ArgumentError => e
|
95
|
+
_actual = e
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'Test overloaded string class' do
|
102
|
+
context 'Test basic functionality' do
|
103
|
+
context 'Full/Default method names' do
|
104
|
+
it 'says 23 June 1912 is day 23' do
|
105
|
+
expect('23/6/1912'.the_day).to eql(23)
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'says 23 June 1912 is month 6' do
|
109
|
+
expect('23/6/1912'.the_month).to eql(6)
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'says 23 June 1912 is a year 1912' do
|
113
|
+
expect('23/6/1912'.the_year).to eql(1912)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'says 23 June 1912 is day 0' do
|
117
|
+
expect('23/6/1912'.day_of_the_week).to eql(0)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'says 23 June 1912 is a Sunday' do
|
121
|
+
expect('23/6/1912'.day_of_the_week_name).to eql('Sunday')
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'says 23 June 1912 is day of year 175' do
|
125
|
+
expect('23/6/1912'.day_of_the_year).to eql(175)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'says 15324 for days between 23 June 1912 and 7 June 1954' do
|
129
|
+
expect('23/6/1912'.days_between('7/6/1954')).to eql(15_324)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
context 'Alias method names' do
|
133
|
+
it 'says 23 June 1912 is day 0' do
|
134
|
+
expect('23/6/1912'.dotw).to eql(0)
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'says 23 June 1912 is a Sunday' do
|
138
|
+
expect('23/6/1912'.dotwn).to eql('Sunday')
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'says 23 June 1912 is day of year 175' do
|
142
|
+
expect('23/6/1912'.doty).to eql(175)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'Test complex functionality' do
|
148
|
+
it 'returns an array for weekends in the year 2000' do
|
149
|
+
expect('1/1/2000'.weekends('31/12/2000')).to be_an_instance_of(Array)
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'says ["01/01/2000", "02/01/2000", "08/01/2000", "09/01/2000", "15/01/2000", "16/01/2000", "22/01/2000", "23/01/2000", "29/01/2000", "30/01/2000"] for weekends in the year Jan 2000' do
|
153
|
+
expect('1/1/2000'.weekends_string('31/1/2000')).to match_array(['01/01/2000', '02/01/2000', '08/01/2000', '09/01/2000', '15/01/2000', '16/01/2000', '22/01/2000', '23/01/2000', '29/01/2000', '30/01/2000'])
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'returns an array for weekdays in the year 2000' do
|
157
|
+
expect('1/1/2000'.weekdays('31/12/2000')).to be_an_instance_of(Array)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'says ["03/01/2000", "04/01/2000", "05/01/2000", "06/01/2000", "07/01/2000", "10/01/2000", "11/01/2000", "12/01/2000", "13/01/2000", "14/01/2000"] for weekdays in the first 2 weeks Jan 2000' do
|
161
|
+
expect('1/1/2000'.weekdays_string('14/1/2000')).to match_array(['03/01/2000', '04/01/2000', '05/01/2000', '06/01/2000', '07/01/2000', '10/01/2000', '11/01/2000', '12/01/2000', '13/01/2000', '14/01/2000'])
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# rubocop:enable Metrics/BlockLength
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'dates_toolbox'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# Enable flags like --only-failures and --next-failure
|
6
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
7
|
+
|
8
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
9
|
+
config.disable_monkey_patching!
|
10
|
+
|
11
|
+
config.expect_with :rspec do |c|
|
12
|
+
c.syntax = :expect
|
13
|
+
end
|
14
|
+
|
15
|
+
original_stderr = $stderr
|
16
|
+
original_stdout = $stdout
|
17
|
+
|
18
|
+
# Surpress console output
|
19
|
+
config.before(:all) do
|
20
|
+
# Redirect stderr and stdout
|
21
|
+
$stderr = File.open(File::NULL, 'w')
|
22
|
+
$stdout = File.open(File::NULL, 'w')
|
23
|
+
end
|
24
|
+
|
25
|
+
# Allow console output
|
26
|
+
config.after(:all) do
|
27
|
+
$stderr = original_stderr
|
28
|
+
$stdout = original_stdout
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dates_toolbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Gurney aka Wolf
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-13 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.17'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.17'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.0'
|
61
|
+
description: A full featured rubygem for manipulating dates.
|
62
|
+
email:
|
63
|
+
- wolf@tgwolf.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- ".rspec"
|
70
|
+
- ".rubocop.yml"
|
71
|
+
- ".travis.yml"
|
72
|
+
- CHANGELOG.md
|
73
|
+
- CODEOWNERS
|
74
|
+
- CODE_OF_CONDUCT.md
|
75
|
+
- Gemfile
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- bin/console
|
80
|
+
- bin/setup
|
81
|
+
- dates_toolbox.gemspec
|
82
|
+
- lib/dates_toolbox.rb
|
83
|
+
- lib/dates_toolbox/overloads.rb
|
84
|
+
- lib/dates_toolbox/version.rb
|
85
|
+
- spec/dates_toolbox_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: https://github.com/WolfSoftware/dates_toolbox
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubygems_version: 3.0.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: A full featured rubygem for manipulating dates.
|
110
|
+
test_files: []
|