date_by_example 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/.DS_Store +0 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +149 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +91 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/date_by_example.gemspec +37 -0
- data/lib/date_by_example.rb +22 -0
- data/lib/date_by_example/date.rb +5 -0
- data/lib/date_by_example/date_time.rb +5 -0
- data/lib/date_by_example/example_formatter.rb +48 -0
- data/lib/date_by_example/time.rb +5 -0
- data/lib/date_by_example/version.rb +3 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8467f9d24308479efdaf1fd7a0e1071f3fe37c97c90b36386153f72348ad0c92
|
4
|
+
data.tar.gz: 5013f2e7a18cdcdf3f13ab21b1bb47981850a42934704f72aa9fd91fd3e646d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1560b2ef198d33fed16adfa0efedc8477691b6eb40ebea5cb5d0205e67afdbf9b30aa7537c64c956ad3952f64b96cec5bb4b23d94ef07eb311db133c228ff021
|
7
|
+
data.tar.gz: 0f2472d0cd4db73ed9adf508bbb28e7f6c58edbd083037a828218ff00350cd681483700ba985521faa7ae464b360090b01969f1d0a32ba76e8939d2360d59195
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Exclude:
|
6
|
+
- "db/schema.rb" # You can't touch this
|
7
|
+
- ".bundle/**/*" # Auto-generated
|
8
|
+
- "bin/**/*" # Auto-generated
|
9
|
+
- "vendor/**/*" # We cannot solve the world's problems
|
10
|
+
TargetRubyVersion: 2.5
|
11
|
+
|
12
|
+
Rails:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/AbcSize:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/BlockLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/ClassLength:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/LineLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/MethodLength:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/ParameterLists:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Layout/AlignParameters:
|
34
|
+
EnforcedStyle: with_fixed_indentation
|
35
|
+
IndentationWidth: 2
|
36
|
+
|
37
|
+
Style/CaseEquality:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Layout/EmptyLinesAroundBlockBody:
|
41
|
+
Exclude:
|
42
|
+
- "spec/**/*"
|
43
|
+
- "lib/tasks/*.rake"
|
44
|
+
|
45
|
+
Layout/FirstParameterIndentation:
|
46
|
+
IndentationWidth: 2
|
47
|
+
|
48
|
+
Style/FrozenStringLiteralComment:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Layout/IndentArray:
|
52
|
+
IndentationWidth: 2
|
53
|
+
|
54
|
+
Layout/IndentAssignment:
|
55
|
+
IndentationWidth: 2
|
56
|
+
|
57
|
+
Layout/IndentHash:
|
58
|
+
IndentationWidth: 2
|
59
|
+
|
60
|
+
Layout/MultilineMethodCallBraceLayout:
|
61
|
+
EnforcedStyle: same_line
|
62
|
+
|
63
|
+
Layout/MultilineMethodCallIndentation:
|
64
|
+
EnforcedStyle: indented
|
65
|
+
IndentationWidth: 2
|
66
|
+
|
67
|
+
Layout/MultilineHashBraceLayout:
|
68
|
+
EnforcedStyle: same_line
|
69
|
+
|
70
|
+
Layout/MultilineOperationIndentation:
|
71
|
+
EnforcedStyle: indented
|
72
|
+
IndentationWidth: 2
|
73
|
+
|
74
|
+
Naming/VariableNumber:
|
75
|
+
EnforcedStyle: snake_case
|
76
|
+
|
77
|
+
Style/SignalException:
|
78
|
+
EnforcedStyle: only_raise
|
79
|
+
|
80
|
+
Style/StringLiterals:
|
81
|
+
EnforcedStyle: double_quotes
|
82
|
+
|
83
|
+
Style/TrivialAccessors:
|
84
|
+
ExactNameMatch: true
|
85
|
+
|
86
|
+
Style/TrailingCommaInHashLiteral:
|
87
|
+
EnforcedStyleForMultiline: no_comma
|
88
|
+
|
89
|
+
Style/TrailingCommaInArrayLiteral:
|
90
|
+
EnforcedStyleForMultiline: no_comma
|
91
|
+
|
92
|
+
Style/TrailingCommaInArguments:
|
93
|
+
EnforcedStyleForMultiline: no_comma
|
94
|
+
|
95
|
+
Layout/SpaceInsideBlockBraces:
|
96
|
+
EnforcedStyle: space
|
97
|
+
EnforcedStyleForEmptyBraces: no_space
|
98
|
+
SpaceBeforeBlockParameters: true
|
99
|
+
|
100
|
+
Layout/SpaceInsideHashLiteralBraces:
|
101
|
+
EnforcedStyle: no_space
|
102
|
+
EnforcedStyleForEmptyBraces: no_space
|
103
|
+
|
104
|
+
Style/Documentation:
|
105
|
+
Enabled: false
|
106
|
+
|
107
|
+
Style/BlockDelimiters:
|
108
|
+
Exclude:
|
109
|
+
- "spec/**/*" # because let statements use braces for multiline blocks
|
110
|
+
|
111
|
+
Layout/BlockEndNewline:
|
112
|
+
Exclude:
|
113
|
+
- "spec/**/*" # because let statements use braces for multiline blocks
|
114
|
+
|
115
|
+
Layout/MultilineBlockLayout:
|
116
|
+
Exclude:
|
117
|
+
- "spec/**/*" # because let statements use braces for multiline blocks
|
118
|
+
|
119
|
+
Style/Semicolon:
|
120
|
+
AllowAsExpressionSeparator: true
|
121
|
+
Exclude:
|
122
|
+
- "spec/**/*" # because sometimes we use this in expect or then blocks
|
123
|
+
|
124
|
+
Style/RaiseArgs:
|
125
|
+
Enabled: false
|
126
|
+
|
127
|
+
Style/AccessModifierDeclarations:
|
128
|
+
Enabled: false
|
129
|
+
|
130
|
+
RSpec/ContextWording:
|
131
|
+
Enabled: false
|
132
|
+
|
133
|
+
RSpec/ExampleLength:
|
134
|
+
Enabled: false
|
135
|
+
|
136
|
+
RSpec/HookArgument:
|
137
|
+
EnforcedStyle: "example"
|
138
|
+
|
139
|
+
RSpec/MultipleExpectations:
|
140
|
+
Enabled: false
|
141
|
+
|
142
|
+
RSpec/NestedGroups:
|
143
|
+
Enabled: false
|
144
|
+
|
145
|
+
RSpec/DescribedClass:
|
146
|
+
Enabled: false
|
147
|
+
|
148
|
+
Style/DateTime:
|
149
|
+
Enabled: false
|
data/.travis.yml
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 noelrappin@gmail.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/Gemfile.lock
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
date_by_example (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
jaro_winkler (1.5.1)
|
12
|
+
parallel (1.12.1)
|
13
|
+
parser (2.5.1.0)
|
14
|
+
ast (~> 2.4.0)
|
15
|
+
powerpack (0.1.1)
|
16
|
+
rainbow (3.0.0)
|
17
|
+
rake (10.5.0)
|
18
|
+
rspec (3.7.0)
|
19
|
+
rspec-core (~> 3.7.0)
|
20
|
+
rspec-expectations (~> 3.7.0)
|
21
|
+
rspec-mocks (~> 3.7.0)
|
22
|
+
rspec-core (3.7.1)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-expectations (3.7.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.7.0)
|
27
|
+
rspec-mocks (3.7.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.7.0)
|
30
|
+
rspec-support (3.7.1)
|
31
|
+
rubocop (0.57.1)
|
32
|
+
jaro_winkler (~> 1.5.1)
|
33
|
+
parallel (~> 1.10)
|
34
|
+
parser (>= 2.5)
|
35
|
+
powerpack (~> 0.1)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
ruby-progressbar (~> 1.7)
|
38
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
39
|
+
rubocop-rspec (1.26.0)
|
40
|
+
rubocop (>= 0.53.0)
|
41
|
+
ruby-progressbar (1.9.0)
|
42
|
+
unicode-display_width (1.4.0)
|
43
|
+
|
44
|
+
PLATFORMS
|
45
|
+
ruby
|
46
|
+
|
47
|
+
DEPENDENCIES
|
48
|
+
bundler (~> 1.16)
|
49
|
+
date_by_example!
|
50
|
+
rake (~> 10.0)
|
51
|
+
rspec (~> 3.0)
|
52
|
+
rubocop (~> 0.57)
|
53
|
+
rubocop-rspec (~> 1.25)
|
54
|
+
|
55
|
+
BUNDLED WITH
|
56
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Noel Rappin
|
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,91 @@
|
|
1
|
+
# DateByExample
|
2
|
+
|
3
|
+
Provides a Ruby implementation of Go-style date formatting by example
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'date_by_example'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install date_by_example
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic usage
|
24
|
+
|
25
|
+
This gem adds a `by_example` method to `Date`, `Time`, and `DateTime`.
|
26
|
+
|
27
|
+
The argument to this method is the formatted version of a reference date.
|
28
|
+
|
29
|
+
For example:
|
30
|
+
|
31
|
+
```
|
32
|
+
> d = Date.today
|
33
|
+
> d.by_example("2006-01-02") => "2018-06-11"
|
34
|
+
|
35
|
+
> t = DateTime.now
|
36
|
+
> t.by_example("Jan 02 2006 @ 3:04 pm") => "Jun 11 2018 @ 3:52 pm"
|
37
|
+
|
38
|
+
> t = Time.now
|
39
|
+
> t.by_example("Jan 02 2006 @ 3:04 pm") => "Jun 11 2018 @ 3:53 pm"
|
40
|
+
```
|
41
|
+
|
42
|
+
The reference date is Jan 2, 2006 at 3:04:05 PM MST, which can be
|
43
|
+
remembered as 1 2 3 4 5 6 -7.
|
44
|
+
|
45
|
+
I realize that remembering the reference date is not the easy part,
|
46
|
+
but I think it's easier than remembering all the strftime format
|
47
|
+
strings
|
48
|
+
|
49
|
+
### Supported formats
|
50
|
+
|
51
|
+
* Year: 2000 and 00 (but not Ruby's %c for the century)
|
52
|
+
* Month: January, JANUARY, Jan, JAN, 1, and 01, but not " 1" for space padding
|
53
|
+
* Day: 2, 02, 002 (for day of year), Monday, MONDAY, Mon, MON, but not " 2" for space padding
|
54
|
+
* Hour: 15, 3, pm, PM
|
55
|
+
* Minute: 04
|
56
|
+
* Second: 05, 05.000 for milliseconds and 05.000000 and 05.000000000
|
57
|
+
* Time Zone: -7000, -07:00, -07:00:00, MST
|
58
|
+
|
59
|
+
### Unsupported Formats
|
60
|
+
|
61
|
+
This does not support Ruby's week based formats
|
62
|
+
|
63
|
+
### Alternate usage
|
64
|
+
|
65
|
+
The slow part of using this gem is generating the format string from
|
66
|
+
the example, if you want to reuse a format multiple times, you can do so
|
67
|
+
by creating and reusing an `ExampleFormatter`
|
68
|
+
|
69
|
+
```
|
70
|
+
> ef = ExampleFormatter.new("01/02/06")
|
71
|
+
> ef.format(Date.today) => "06/11/18"
|
72
|
+
> ef.format(Time.now) => "06/11/18"
|
73
|
+
```
|
74
|
+
|
75
|
+
## Development
|
76
|
+
|
77
|
+
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.
|
78
|
+
|
79
|
+
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).
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/date_by_example. 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.
|
84
|
+
|
85
|
+
## License
|
86
|
+
|
87
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
88
|
+
|
89
|
+
## Code of Conduct
|
90
|
+
|
91
|
+
Everyone interacting in the DateByExample project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/date_by_example/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "date_by_example"
|
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,37 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "date_by_example/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "date_by_example"
|
7
|
+
spec.version = DateByExample::VERSION
|
8
|
+
spec.authors = ["Noel Rappin"]
|
9
|
+
spec.email = ["noelrappin@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Go style date formatting by example"
|
12
|
+
spec.description = "Go style date formatting by example"
|
13
|
+
spec.homepage = "http://www.github.com/noelrappin/date_by_example"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "rubocop", "~> 0.57"
|
36
|
+
spec.add_development_dependency "rubocop-rspec", "~> 1.25"
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "date"
|
2
|
+
require "date_by_example/date"
|
3
|
+
require "date_by_example/date_time"
|
4
|
+
require "date_by_example/time"
|
5
|
+
require "date_by_example/version"
|
6
|
+
require "date_by_example/example_formatter"
|
7
|
+
require "benchmark"
|
8
|
+
|
9
|
+
module DateByExample
|
10
|
+
def self.benchmark
|
11
|
+
dt = DateTime.new(2018, 6, 9, 13, 15)
|
12
|
+
Benchmark.bm do |x|
|
13
|
+
x.report do
|
14
|
+
1_000_000.times do
|
15
|
+
ExampleFormatter.new("2 Jan 06 15:04").format(dt)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# GSUB version is 16.15 seconds
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
class ExampleFormatter
|
2
|
+
attr_accessor :reference
|
3
|
+
|
4
|
+
def initialize(reference)
|
5
|
+
@reference = reference
|
6
|
+
@format_string = nil
|
7
|
+
end
|
8
|
+
|
9
|
+
FORMATS = {
|
10
|
+
".000000000" => ".9N",
|
11
|
+
"-07:00:00" => "%::z",
|
12
|
+
".000000" => ".%6N",
|
13
|
+
"January" => "%B",
|
14
|
+
"JANUARY" => "%^B",
|
15
|
+
"Monday" => "%A",
|
16
|
+
"MONDAY" => "%^A",
|
17
|
+
"-07:00" => "%:z",
|
18
|
+
"-7000" => "%z",
|
19
|
+
"2006" => "%Y",
|
20
|
+
".000" => ".%L",
|
21
|
+
"002" => "%j",
|
22
|
+
"Jan" => "%b",
|
23
|
+
"JAN" => "%^b",
|
24
|
+
"Mon" => "%a",
|
25
|
+
"MON" => "%^a",
|
26
|
+
"MST" => "%Z",
|
27
|
+
"15" => "%H",
|
28
|
+
"06" => "%y",
|
29
|
+
"01" => "%m",
|
30
|
+
"02" => "%d",
|
31
|
+
"03" => "%I",
|
32
|
+
"PM" => "%p",
|
33
|
+
"pm" => "%P",
|
34
|
+
"04" => "%M",
|
35
|
+
"05" => "%S",
|
36
|
+
"1" => "%-m",
|
37
|
+
"2" => "%-e"}.freeze
|
38
|
+
|
39
|
+
FORMAT_MATCHER = Regexp.union(FORMATS.keys)
|
40
|
+
|
41
|
+
def format_string
|
42
|
+
@format_string ||= reference.gsub(FORMAT_MATCHER, FORMATS)
|
43
|
+
end
|
44
|
+
|
45
|
+
def format(date)
|
46
|
+
date.strftime(format_string)
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date_by_example
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Noel Rappin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-11 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.57'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.57'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.25'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.25'
|
83
|
+
description: Go style date formatting by example
|
84
|
+
email:
|
85
|
+
- noelrappin@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".DS_Store"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".rubocop.yml"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CODE_OF_CONDUCT.md
|
96
|
+
- Gemfile
|
97
|
+
- Gemfile.lock
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- bin/console
|
102
|
+
- bin/setup
|
103
|
+
- date_by_example.gemspec
|
104
|
+
- lib/date_by_example.rb
|
105
|
+
- lib/date_by_example/date.rb
|
106
|
+
- lib/date_by_example/date_time.rb
|
107
|
+
- lib/date_by_example/example_formatter.rb
|
108
|
+
- lib/date_by_example/time.rb
|
109
|
+
- lib/date_by_example/version.rb
|
110
|
+
homepage: http://www.github.com/noelrappin/date_by_example
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.7.6
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Go style date formatting by example
|
134
|
+
test_files: []
|