yymmdd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +111 -0
  3. data/lib/yymmdd.rb +104 -0
  4. metadata +61 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 696bd0a2bba0616dd2ff927d26a761f7735d97d0
4
+ data.tar.gz: 74c7a0ae9df8974c85baf80cb5f783571f095bc5
5
+ SHA512:
6
+ metadata.gz: 595d8186269bd5d7b5ad799de0fe260e9b38a68fe5c8bc2c94d97a7946a4fea6dc6cc6568d767448d0f483287f7efc0510cd620040ad1bd6183271fd4bf5eee7
7
+ data.tar.gz: c30dee3c333a7912c872aa2aebe1ec62cac621eb40fb325049657bb5648c2a21ff3d9d51fc787c0ca72d9afc13e904eb3ced4d45a5aa302a67e4f002b8c1f7f1
@@ -0,0 +1,111 @@
1
+ # YYMMDD
2
+
3
+ [![Build Status](https://travis-ci.org/sshaw/yymmdd.png?branch=master)](https://travis-ci.org/sshaw/yymmdd)
4
+
5
+ Tiny DSL for idiomatic date parsing and formatting.
6
+
7
+ ## Overview
8
+
9
+ require "yymmdd"
10
+
11
+ include YYMMDD
12
+
13
+ puts yy/mm # 14/08 (i.e., today's date)
14
+ puts yyyy/mm # 2014/08
15
+ date = ymd(411207) # Date.new(1941, 12, 7)
16
+
17
+ date = Date.today
18
+ puts yyyy.mm.dd(date) # 2014.08.09
19
+ puts dd/mm/yy(date) # 08/09/14
20
+ puts ymd(date) # 1489
21
+ puts yymmdd(date) # 140809
22
+
23
+ date = yyyy.mm.dd("1941.12.07") # Date.new(1941, 12, 7)
24
+ date = mm.dd.yy("11.22.63") # Date.new(1963, 11, 22)
25
+ date = mm/dd/yy("11/21/99") # ...
26
+ date = mm/dd/yyyy("11/21/1999")
27
+ date = mm-dd-yyyy("11-21-1999")
28
+ date = m-d-y("11-21-99")
29
+
30
+ ## Installation
31
+
32
+ ### Rubygems:
33
+
34
+ gem install yymmdd
35
+
36
+ ### Bundler:
37
+
38
+ gem "yymmdd"
39
+
40
+ ## Usage
41
+
42
+ All functions are [`module_function`s](http://www.ruby-doc.org/core-2.1.2/Module.html#method-i-module_function)
43
+ so you must `include YYMMDD` to use them.
44
+
45
+ When given a `String` it will attempt to parse it as the specified format and return a `Date`.
46
+
47
+ When given a `Date` it will return a `String` in the specified format.
48
+
49
+ An `ArgumentError` is raised if the date can't be parsed or formatted.
50
+
51
+ With no arguments it will return an instance of a `String`-like object (it overrides `to_s` and `to_str`) representing
52
+ today's date in the specified format. In the most common cases you can treat it like a `String`:
53
+
54
+ date = yyyy/mm/dd
55
+ puts "Today's date: #{date}"
56
+ text = ["Dates: ", yy/mm, yyyy/mm].join(", ")
57
+ text = "A great date: " << date
58
+
59
+ But in some instances you'll have to expilictly call `to_s`:
60
+
61
+ printf "Today's date: %s\n", date.to_s
62
+
63
+ All the heavy lifting is done by `Date#strftime` and `Date.strptime`.
64
+
65
+ ### Format Specifiers
66
+
67
+ The table below lists the available format specifiers. All of these can be separated by one of the supported
68
+ delimiters: `"/"`, `"."`, or `"-"`.
69
+
70
+ <table>
71
+ <thead>
72
+ <th>Name</th><th>Format</th>
73
+ </thead>
74
+ <tbody>
75
+ <tr>
76
+ <td><code>d</code></td><td>day of the year, no 0 padding</td>
77
+ </tr>
78
+ <tr>
79
+ <td><code>dd</code></td><td>day of the year</td>
80
+ </tr>
81
+ <tr>
82
+ <td><code>m</code></td><td>day of the month, no 0 padding</td>
83
+ </tr>
84
+ <tr>
85
+ <td><code>mm</code></td><td>day of the month</td>
86
+ </tr>
87
+ <tr>
88
+ <td><code>y</code></td><td>2 digit year</td>
89
+ </tr>
90
+ <tr>
91
+ <td><code>yy</code></td><td>2 digit year</td>
92
+ </tr>
93
+ <tr>
94
+ <td><code>yyyy</code></td><td>4 digit year</td>
95
+ </tr>
96
+ </tbody>
97
+ </table>
98
+
99
+ There are also combined, delimiterless functions for all combinations of the above, e.g., `ymd`, `mdy`, `yymmdd`, etc...
100
+
101
+ ## Caveats
102
+
103
+ Due to operator precedence you can't mix delimiters.
104
+
105
+ ## Author
106
+
107
+ Skye Shaw [sshaw AT gmail.com]
108
+
109
+ ## License
110
+
111
+ Released under the MIT License: www.opensource.org/licenses/MIT
@@ -0,0 +1,104 @@
1
+ require "date"
2
+
3
+ module YYMMDD
4
+ VERSION = "0.0.1"
5
+
6
+ FORMATS = %w[yyyy yy y mm m dd d]
7
+ FORMATS.each do |abbr|
8
+ define_method(abbr) { |date = nil| DatePart.new(abbr, date) }
9
+ module_function abbr
10
+ end
11
+
12
+ groups = FORMATS.group_by { |fmt| fmt[0] }
13
+ groups["y"].each do |y|
14
+ groups["m"].each do |m|
15
+ groups["d"].each do |d|
16
+ [y, m, d].permutation.each do |set|
17
+ name = set.join("")
18
+ module_eval(<<-YMD, __FILE__, __LINE__)
19
+ def #{name}(date = nil)
20
+ #{set[0]} << #{set[1]} << #{set[2]}(date)
21
+ end
22
+ YMD
23
+
24
+ module_function name
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ class DatePart # :nodoc:
31
+ # Output formats
32
+ STRFTIME = {
33
+ "y" => "%y",
34
+ "yy" => "%y",
35
+ "yyyy" => "%Y",
36
+ "m" => "%-m",
37
+ "mm" => "%m",
38
+ "d" => "%-d",
39
+ "dd" => "%d"
40
+ }.freeze
41
+
42
+ # Input formats, strptime() doesn't support "%-X" formats
43
+ STRPTIME = STRFTIME.merge("d" => "%d", "m" => "%m").freeze
44
+
45
+ attr :format
46
+ attr :date
47
+
48
+ def initialize(format, date)
49
+ @format = format
50
+ @date = date
51
+ @parts = [[self, ""]]
52
+ end
53
+
54
+ def /(date = nil)
55
+ process("/", date)
56
+ end
57
+
58
+ def -(date = nil)
59
+ process("-", date)
60
+ end
61
+
62
+ def << (date = nil)
63
+ process("", date)
64
+ end
65
+
66
+ FORMATS.each do |abbr|
67
+ define_method(abbr) { |date = nil| process(".", DatePart.new(abbr, date)) }
68
+ end
69
+
70
+ def to_s
71
+ build_date.to_s
72
+ end
73
+
74
+ def to_str
75
+ to_s
76
+ end
77
+
78
+ def inspect
79
+ to_s
80
+ end
81
+
82
+ private
83
+ def process(tok, part)
84
+ raise ArgumentError, "invalid format" unless part.is_a?(DatePart)
85
+
86
+ @parts << [part, tok]
87
+ return build_date if part.date
88
+ self
89
+ end
90
+
91
+ def build_date
92
+ date = @parts[-1][0].date || Date.today
93
+
94
+ if date.is_a?(Date)
95
+ fmt = @parts.map { |part, tok| sprintf("%s%s", tok, STRFTIME[part.format]) }.join("")
96
+ date.strftime(fmt)
97
+ else
98
+ fmt = @parts.map { |part, tok| sprintf("%s%s", tok, STRPTIME[part.format]) }.join("")
99
+ date = date.to_s if date.is_a?(Fixnum)
100
+ Date.strptime(date, fmt)
101
+ end
102
+ end
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yymmdd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ description: |2
28
+ Tiny DSL for idiomatic parsing and formatting of numeric date components.
29
+ email: skye.shaw@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README.md
34
+ files:
35
+ - README.md
36
+ - lib/yymmdd.rb
37
+ homepage: http://github.com/sshaw/yymmdd
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Tiny DSL for idiomatic date parsing and formatting
61
+ test_files: []