Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sshaw/yymmdd
Tiny DSL for idiomatic date parsing and formatting in Ruby
https://github.com/sshaw/yymmdd
date dsl metaprogramming ruby syntax-sugar
Last synced: 26 days ago
JSON representation
Tiny DSL for idiomatic date parsing and formatting in Ruby
- Host: GitHub
- URL: https://github.com/sshaw/yymmdd
- Owner: sshaw
- Created: 2014-08-09T14:34:35.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-08-11T03:04:12.000Z (over 10 years ago)
- Last Synced: 2024-11-27T17:54:31.710Z (about 1 month ago)
- Topics: date, dsl, metaprogramming, ruby, syntax-sugar
- Language: Ruby
- Homepage:
- Size: 149 KB
- Stars: 77
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-ruby - yymmdd - Tiny DSL for idiomatic date parsing and formatting. (Date and Time Processing)
README
# YYMMDD
[![Build Status](https://travis-ci.org/sshaw/yymmdd.png?branch=master)](https://travis-ci.org/sshaw/yymmdd)
Tiny DSL for idiomatic date parsing and formatting.
## Overview
require "yymmdd"
include YYMMDD
puts yy/mm # 14/08 (i.e., today's date)
puts yyyy/mm # 2014/08
date = ymd(411207) # Date.new(1941, 12, 7)date = Date.today
puts yyyy.mm.dd(date) # 2014.08.09
puts dd/mm/yy(date) # 08/09/14
puts ymd(date) # 1489
puts yymmdd(date) # 140809date = yyyy.mm.dd("1941.12.07") # Date.new(1941, 12, 7)
date = mm.dd.yy("11.22.63") # Date.new(1963, 11, 22)
date = mm/dd/yy("11/21/99") # ...
date = mm/dd/yyyy("11/21/1999")
date = mm-dd-yyyy("11-21-1999")
date = m-d-y("11-21-99")## Installation
### Rubygems:
gem install yymmdd
### Bundler:
gem "yymmdd"
## Usage
All functions are [`module_function`s](http://www.ruby-doc.org/core-2.1.2/Module.html#method-i-module_function)
so you must `include YYMMDD` to use them.When given a `String` it will attempt to parse it as the specified format and return a `Date`.
When given a `Date` it will return a `String` in the specified format.
An `ArgumentError` is raised if the date can't be parsed or formatted.
With no arguments it will return an instance of a `String`-like object (it overrides `to_s` and `to_str`) representing
today's date in the specified format. In the most common cases you can treat it like a `String`:date = yyyy/mm/dd
puts "Today's date: #{date}"
text = ["Dates: ", yy/mm, yyyy/mm].join(", ")
text = "A great date: " << dateBut in some instances you'll have to expilictly call `to_s`:
printf "Today's date: %s\n", date.to_s
All the heavy lifting is done by `Date#strftime` and `Date.strptime`.
### Format Specifiers
The table below lists the available format specifiers. All of these can be separated by one of the supported
delimiters: `"/"`, `"."`, or `"-"`.
NameFormat
d
day of the year, no 0 padding
dd
day of the year
m
day of the month, no 0 padding
mm
day of the month
y
2 digit year
yy
2 digit year
yyyy
4 digit year
There are also combined, delimiterless functions for all combinations of the above, e.g., `ymd`, `mdy`, `yymmdd`, etc...
## Caveats
Due to operator precedence you can't mix delimiters.
## Author
Skye Shaw [sshaw AT gmail.com]
## License
Released under the MIT License: www.opensource.org/licenses/MIT