date-formatter 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -7
- data/lib/date-formatter.rb +5 -18
- data/lib/date-formatter/version.rb +1 -1
- data/test/test_format.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc13bc04778d52fe9b85d5526ef59659ba439171
|
4
|
+
data.tar.gz: f3c3b990a63d3008ea8ff312fdc87b4e0f0e40f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc49cd429825bcd25a2929480ffe9b8b84e1cbcb447cb5fa65f5f96e1a964e19f751e38e20a5d3d937dda1fc121e8c36b58bbc40d63ed41b6b8a2ac2accdcc3d
|
7
|
+
data.tar.gz: a84af32c4a0541a3a1f8b095de19a33e4b43f61a4448ce5888544b102eeb3a550f5529620f0a8e782316faaf4a81bea370084287292b0b8dd3c0274ef758888c
|
data/README.md
CHANGED
@@ -11,8 +11,8 @@
|
|
11
11
|
|
12
12
|
## Usage
|
13
13
|
|
14
|
-
The date by example lets you format dates e.g. "January
|
15
|
-
using an example as a format string e.g "January
|
14
|
+
The date by example lets you format dates e.g. "January 02, 2006"
|
15
|
+
using an example as a format string e.g "January 02, 2006" instead of the classic strftime format specifier e.g. "%B %d, %Y". The date by example adds:
|
16
16
|
|
17
17
|
- `String#to_strfime`
|
18
18
|
- `Date#format`
|
@@ -23,8 +23,13 @@ using an example as a format string e.g "January 04, 2020" instead of the classi
|
|
23
23
|
to the built-in classes.
|
24
24
|
|
25
25
|
|
26
|
+
### The `String#to_strftime` Method
|
26
27
|
|
27
|
-
|
28
|
+
```ruby
|
29
|
+
class String
|
30
|
+
def to_strftime() DateByExample.to_strftime( self ); end
|
31
|
+
end
|
32
|
+
```
|
28
33
|
|
29
34
|
The new `String#to_strftime` method auto-builds the `strftime` format string
|
30
35
|
from an example date:
|
@@ -42,7 +47,13 @@ p '2 Mon 2006 03:00'.to_strftime #=> "%-d %b %Y %H:%M"
|
|
42
47
|
```
|
43
48
|
|
44
49
|
|
45
|
-
### `Date#format`
|
50
|
+
### The `Date#format` Method
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
class Date
|
54
|
+
def format( spec ) self.strftime( spec.to_strftime ); end
|
55
|
+
end
|
56
|
+
```
|
46
57
|
|
47
58
|
The new `Date#format` method formats the date like the passed in example:
|
48
59
|
|
@@ -57,9 +68,15 @@ p date.format( 'Monday, January 2, 2006' ) #=> "Sunday, February 9, 2020"
|
|
57
68
|
|
58
69
|
|
59
70
|
|
60
|
-
### `Time#format`
|
71
|
+
### The `Time#format` Method
|
72
|
+
|
73
|
+
``` ruby
|
74
|
+
class Time
|
75
|
+
def format( spec ) self.strftime( spec.to_strftime ); end
|
76
|
+
end
|
77
|
+
```
|
61
78
|
|
62
|
-
The new `Time#format` method
|
79
|
+
The new `Time#format` method formats the time like the passed in example:
|
63
80
|
|
64
81
|
``` ruby
|
65
82
|
time = Time.now ## test run on 2020-02-09 00:00
|
@@ -74,7 +91,13 @@ p time.format( '2 Mon 2006 03:00' ) #=> "9 Sun 2020 00:00"
|
|
74
91
|
```
|
75
92
|
|
76
93
|
|
77
|
-
### `NilClass#format`
|
94
|
+
### The `NilClass#format` Method
|
95
|
+
|
96
|
+
``` ruby
|
97
|
+
class NilClass
|
98
|
+
def format( spec ) ''; end
|
99
|
+
end
|
100
|
+
```
|
78
101
|
|
79
102
|
For convenience the new `NilClass#format` will catch format calls on `nil`
|
80
103
|
and NOT crash but return an empty string
|
data/lib/date-formatter.rb
CHANGED
@@ -85,29 +85,16 @@ module DateByExample
|
|
85
85
|
spec = spec.gsub( 'DD', '%d' )
|
86
86
|
spec = spec.gsub( 'D', '%-d')
|
87
87
|
|
88
|
+
spec = spec.gsub( 'hh', '%H' )
|
89
|
+
spec = spec.gsub( 'h', '%-H' )
|
90
|
+
spec = spec.gsub( 'mm', '%M' )
|
91
|
+
spec = spec.gsub( 'ss', '%S' )
|
92
|
+
|
88
93
|
spec
|
89
94
|
end
|
90
95
|
end
|
91
96
|
|
92
97
|
|
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
98
|
|
112
99
|
class String
|
113
100
|
def to_strftime() DateByExample.to_strftime( self ); end
|
data/test/test_format.rb
CHANGED
@@ -24,7 +24,11 @@ class TestFormat < MiniTest::Test
|
|
24
24
|
'YYYYMMDD' => ['%Y%m%d', '20060102'],
|
25
25
|
'YYMMDD' => ['%y%m%d', '060102'],
|
26
26
|
'YYMMD' => ['%y%m%-d', '06012'],
|
27
|
-
'YYYY' => ['%Y', '2006'],
|
27
|
+
'YYYY' => ['%Y', '2006'],
|
28
|
+
|
29
|
+
'hh:mm:ss' => ['%H:%M:%S', '03:00:00'],
|
30
|
+
'h:mm:ss' => ['%-H:%M:%S', '3:00:00'],
|
31
|
+
'hh:mm' => ['%H:%M', '03:00'],
|
28
32
|
}
|
29
33
|
|
30
34
|
dates.each do |date, exp|
|