date-formatter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/Manifest.txt +9 -0
- data/README.md +42 -0
- data/Rakefile +28 -0
- data/lib/date-formatter.rb +138 -0
- data/lib/date-formatter/version.rb +20 -0
- data/lib/date/formatter.rb +1 -0
- data/test/helper.rb +7 -0
- data/test/test_format.rb +55 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c390fc86284148fbbd76384d5e0a2a81aa92db33
|
4
|
+
data.tar.gz: b0a78b8d0d1b342d99125b7b7b64a5cecc2265f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b470f1e3b04ec4146175e488c1b210129799b3083188f9e21e1cc44c94b12303e5a47a535d740e187334026a38cfe8852cfe44774e125b1ad9cf46f4d8f370f
|
7
|
+
data.tar.gz: b751629fdfb1be81962c61a00d1ae28cb5754a4112b59a06c0861d2c763bd4b8a0dbceea14e9be61834e8977a8ac048e672308bc21f685e1675a529501977790
|
data/CHANGELOG.md
ADDED
data/Manifest.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# date-formatter - date formatter by example; auto-builds the strftime format string from a example date
|
2
|
+
|
3
|
+
|
4
|
+
* home :: [github.com/feedreader/pluto](https://github.com/feedreader/pluto)
|
5
|
+
* bugs :: [github.com/feedreader/pluto/issues](https://github.com/feedreader/pluto/issues)
|
6
|
+
* gem :: [rubygems.org/gems/date-formatter](https://rubygems.org/gems/date-formatter)
|
7
|
+
* rdoc :: [rubydoc.info/gems/date-formatter](http://rubydoc.info/gems/date-formatter)
|
8
|
+
* forum :: [groups.google.com/group/wwwmake](http://groups.google.com/group/wwwmake)
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
Examples:
|
18
|
+
|
19
|
+
| Date by Example | => `strftime` Format |
|
20
|
+
|-------------------------|----------------|
|
21
|
+
| January 04, 2020 | => `%B %d, %Y` |
|
22
|
+
| Fri, Jan 04 | => `%a, %b %d` |
|
23
|
+
| 4 Jan 2020 | => `%-d %b %Y` |
|
24
|
+
| Friday, January 4, 2020 | => `%A, %B %-d, %Y` |
|
25
|
+
| Fri, Jan 04 8:00 | => `%a, %b %d %-H:%M` |
|
26
|
+
| 4 Jan 2020 08:00 | => `%-d %b %Y %H:%M` |
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
## License
|
33
|
+
|
34
|
+

|
35
|
+
|
36
|
+
The `date-formatter` scripts are dedicated to the public domain.
|
37
|
+
Use it as you please with no restrictions whatsoever.
|
38
|
+
|
39
|
+
## Questions? Comments?
|
40
|
+
|
41
|
+
Send them along to the [wwwmake Forum/Mailing List](http://groups.google.com/group/wwwmake).
|
42
|
+
Thanks!
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'hoe'
|
2
|
+
require './lib/date-formatter/version.rb'
|
3
|
+
|
4
|
+
Hoe.spec 'date-formatter' do
|
5
|
+
|
6
|
+
self.version = DateByExample::VERSION
|
7
|
+
|
8
|
+
self.summary = "date-formatter - date formatter by example; auto-builds the strftime format string from a example date"
|
9
|
+
self.description = summary
|
10
|
+
|
11
|
+
self.urls = ['https://github.com/feedreader/pluto']
|
12
|
+
|
13
|
+
self.author = 'Gerald Bauer'
|
14
|
+
self.email = 'wwwmake@googlegroups.com'
|
15
|
+
|
16
|
+
# switch extension to .markdown for gihub formatting
|
17
|
+
self.readme_file = 'README.md'
|
18
|
+
self.history_file = 'CHANGELOG.md'
|
19
|
+
|
20
|
+
self.extra_deps = []
|
21
|
+
|
22
|
+
self.licenses = ['Public Domain']
|
23
|
+
|
24
|
+
self.spec_extras = {
|
25
|
+
required_ruby_version: '>= 2.2.2'
|
26
|
+
}
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require 'time'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
###
|
8
|
+
# our own code
|
9
|
+
require 'date-formatter/version' # let version always go first
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
module DateByExample
|
14
|
+
|
15
|
+
##
|
16
|
+
## todo/fix: improve end of MONTH_RE
|
17
|
+
## do NOT use \b - also break on everything but a-z incl. numbers/digits - (double) check!!!
|
18
|
+
|
19
|
+
MONTH_NAME_ABBREV_RE = %r<
|
20
|
+
\b(
|
21
|
+
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
|
22
|
+
)\b
|
23
|
+
>x
|
24
|
+
|
25
|
+
|
26
|
+
## note: May is turned into an abbreviated month name (%b)
|
27
|
+
MONTH_NAME_RE = %r<
|
28
|
+
\b(
|
29
|
+
January|February|March|
|
30
|
+
April|June| ## note: May is "ambigious" and, thus, NOT included
|
31
|
+
July|August|September|
|
32
|
+
October|November|December
|
33
|
+
)\b
|
34
|
+
>x
|
35
|
+
|
36
|
+
DAY_NAME_ABBREV_RE = %r<
|
37
|
+
\b(
|
38
|
+
Mon|Tue|Wed|Thu|Fri|Sat|Sun
|
39
|
+
)\b
|
40
|
+
>x
|
41
|
+
|
42
|
+
DAY_NAME_RE = %r<
|
43
|
+
\b(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)\b
|
44
|
+
>x
|
45
|
+
|
46
|
+
YEAR_RE = %r<
|
47
|
+
\b([12][0-9][0-9][0-9])\b
|
48
|
+
>x
|
49
|
+
|
50
|
+
DAY_WITH_ZERO_RE = %r<
|
51
|
+
\b(0[1-9])\b
|
52
|
+
>x
|
53
|
+
|
54
|
+
DAY_RE = %r<
|
55
|
+
\b([1-9]|[123][0-9])\b
|
56
|
+
>x
|
57
|
+
|
58
|
+
TIME_WITH_ZERO_RE = %r<
|
59
|
+
\b(0[0-9]:[0-9][0-9])\b
|
60
|
+
>x
|
61
|
+
|
62
|
+
TIME_RE = %r<
|
63
|
+
\b(([1-9]|[12][0-9]):[0-9][0-9])\b
|
64
|
+
>x
|
65
|
+
|
66
|
+
|
67
|
+
def self.to_strftime( spec )
|
68
|
+
spec = spec.gsub( MONTH_NAME_RE, '%B' ) # %B - The full month name ("January")
|
69
|
+
spec = spec.gsub( MONTH_NAME_ABBREV_RE, '%b') # %b - The abbreviated month name ("Jan")
|
70
|
+
spec = spec.gsub( DAY_NAME_RE, '%A' ) # %A - The full weekday name ("Sunday")
|
71
|
+
spec = spec.gsub( DAY_NAME_ABBREV_RE, '%a') # %a - The abbreviated weekday name ("Sun")
|
72
|
+
|
73
|
+
spec = spec.gsub( TIME_WITH_ZERO_RE, '%H:%M' ) # %H - Hour of the day, 24-hour clock (00..23)
|
74
|
+
# %M - Minute of the hour (00..59)
|
75
|
+
spec = spec.gsub( TIME_RE, '%-H:%M' )
|
76
|
+
|
77
|
+
spec = spec.gsub( YEAR_RE, '%Y' ) # %Y - Year with century
|
78
|
+
spec = spec.gsub( DAY_WITH_ZERO_RE, '%d' ) # %d - Day of the month (01..31)
|
79
|
+
spec = spec.gsub( DAY_RE, '%-d' ) # %d - Day of the month without a leading zero (1..31)
|
80
|
+
|
81
|
+
## add some "higher-level" symbolic format shortcuts too
|
82
|
+
spec = spec.gsub( 'YYYY', '%Y' )
|
83
|
+
spec = spec.gsub( 'YY', '%y' )
|
84
|
+
spec = spec.gsub( 'MM', '%m' )
|
85
|
+
spec = spec.gsub( 'DD', '%d' )
|
86
|
+
spec = spec.gsub( 'D', '%-d')
|
87
|
+
|
88
|
+
spec
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
###
|
94
|
+
# String#to_strfime
|
95
|
+
#
|
96
|
+
|
97
|
+
# Date#format
|
98
|
+
# Time#format
|
99
|
+
# DateTime#format
|
100
|
+
|
101
|
+
|
102
|
+
## test with
|
103
|
+
# January 04, 2010
|
104
|
+
# Fri, Jan 04
|
105
|
+
# 4 Jan 2010
|
106
|
+
# Friday, January 4, 2010
|
107
|
+
# ...
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
class String
|
113
|
+
def to_strftime() DateByExample.to_strftime( self ); end
|
114
|
+
end
|
115
|
+
|
116
|
+
class Date
|
117
|
+
def format( spec ) self.strftime( spec.to_strftime ); end
|
118
|
+
end
|
119
|
+
|
120
|
+
class DateTime
|
121
|
+
def format( spec ) self.strftime( spec.to_strftime ); end
|
122
|
+
end
|
123
|
+
|
124
|
+
class Time
|
125
|
+
def format( spec ) self.strftime( spec.to_strftime ); end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
class NilClass
|
130
|
+
def format( spec ) ''; end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
# say hello
|
137
|
+
puts DateByExample.banner if $DEBUG || (defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG)
|
138
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module DateByExample
|
3
|
+
MAJOR = 0 ## todo: namespace inside version or something - why? why not??
|
4
|
+
MINOR = 0
|
5
|
+
PATCH = 1
|
6
|
+
VERSION = [MAJOR,MINOR,PATCH].join('.')
|
7
|
+
|
8
|
+
def self.version
|
9
|
+
VERSION
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.banner
|
13
|
+
"date-formatter/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.root
|
17
|
+
File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
|
18
|
+
end
|
19
|
+
end # module DateByExample
|
20
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative '../date-formatter' ## note: allow require 'date/formatter' too (in addition to require 'date-formatter')
|
data/test/helper.rb
ADDED
data/test/test_format.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
###
|
2
|
+
# to run use
|
3
|
+
# ruby -I ./lib -I ./test test/test_format.rb
|
4
|
+
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class TestFormat < MiniTest::Test
|
9
|
+
|
10
|
+
def test_format
|
11
|
+
dates = { 'January 02, 2006' => '%B %d, %Y',
|
12
|
+
'Mon, Jan 02' => '%a, %b %d',
|
13
|
+
'2 Jan 2006' => '%-d %b %Y',
|
14
|
+
'Monday, January 2, 2006' => '%A, %B %-d, %Y',
|
15
|
+
|
16
|
+
'Mon, Jan 02 3:00' => '%a, %b %d %-H:%M',
|
17
|
+
'2 Jan 2006 03:00' => '%-d %b %Y %H:%M',
|
18
|
+
'2006' => '%Y',
|
19
|
+
|
20
|
+
'YYYY-MM-DD' => ['%Y-%m-%d', '2006-01-02'],
|
21
|
+
'YY-MM-DD' => ['%y-%m-%d', '06-01-02'],
|
22
|
+
'YY-MM-D' => ['%y-%m-%-d', '06-01-2'],
|
23
|
+
|
24
|
+
'YYYYMMDD' => ['%Y%m%d', '20060102'],
|
25
|
+
'YYMMDD' => ['%y%m%d', '060102'],
|
26
|
+
'YYMMD' => ['%y%m%-d', '06012'],
|
27
|
+
'YYYY' => ['%Y', '2006'],
|
28
|
+
}
|
29
|
+
|
30
|
+
dates.each do |date, exp|
|
31
|
+
assert_format( date, exp )
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
def assert_format( spec, exp )
|
37
|
+
strf = DateByExample.to_strftime( spec )
|
38
|
+
now = Time.new( 2006, 1, 2, 3 ) ## Mon, Jan 2, 3:00 2006
|
39
|
+
str = now.strftime( strf )
|
40
|
+
|
41
|
+
puts "#{spec} => #{strf} => #{str}"
|
42
|
+
|
43
|
+
if exp.is_a?( Array )
|
44
|
+
exp_strf = exp[0]
|
45
|
+
exp_str = exp[1]
|
46
|
+
else
|
47
|
+
exp_strf = exp
|
48
|
+
exp_str = spec ## same as passed in format specifier/string
|
49
|
+
end
|
50
|
+
|
51
|
+
assert_equal exp_strf, strf
|
52
|
+
assert_equal exp_str, str
|
53
|
+
end
|
54
|
+
|
55
|
+
end # class TestFormat
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: date-formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gerald Bauer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hoe
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.16'
|
41
|
+
description: date-formatter - date formatter by example; auto-builds the strftime
|
42
|
+
format string from a example date
|
43
|
+
email: wwwmake@googlegroups.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files:
|
47
|
+
- CHANGELOG.md
|
48
|
+
- Manifest.txt
|
49
|
+
- README.md
|
50
|
+
files:
|
51
|
+
- CHANGELOG.md
|
52
|
+
- Manifest.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/date-formatter.rb
|
56
|
+
- lib/date-formatter/version.rb
|
57
|
+
- lib/date/formatter.rb
|
58
|
+
- test/helper.rb
|
59
|
+
- test/test_format.rb
|
60
|
+
homepage: https://github.com/feedreader/pluto
|
61
|
+
licenses:
|
62
|
+
- Public Domain
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- "--main"
|
67
|
+
- README.md
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.2.2
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.5.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: date-formatter - date formatter by example; auto-builds the strftime format
|
86
|
+
string from a example date
|
87
|
+
test_files: []
|