Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for dates

Parses dates in the following format:
yyyy-mm-dd
yyyy-m-d
yyyy-mm-dd 2:20
yyyy-mm-dd 20:20
yyyy-mm-dd 20:20:30

You can also fina an example of date formats in examples/dates.yaml

P.S. No unit tests yet. Can't get jspec to work.

Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
  • Loading branch information...
commit ef7f511134cfafd881959cbba3a1c21701c3da6f 1 parent 1ecabc6
@adrianolaru adrianolaru authored committed
Showing with 35 additions and 1 deletion.
  1. +2 −1  Makefile
  2. +7 −0 examples/dates.yaml
  3. +26 −0 lib/yaml.js
View
3  Makefile
@@ -9,5 +9,6 @@ examples:
@node examples/run.js examples/list.nested.yml
@node examples/run.js examples/hash.yml
@node examples/run.js examples/config.yml
+ @node examples/run.js examples/dates.yml
-.PHONY: test examples
+.PHONY: test examples
View
7 examples/dates.yaml
@@ -0,0 +1,7 @@
+---
+ date0: 2011-05-21
+ date1: 2011-5-21
+ date2: 2011-1-2
+ date3: 2011-02-21 17:56
+ date4: 2011-04-21 5:35
+ date5: 2011-06-02 20:45:30
View
26 lib/yaml.js
@@ -41,6 +41,7 @@ var tokens = [
['false', /^(disabled|false|no|off)/],
['string', /^"(.*?)"/],
['string', /^'(.*?)'/],
+ ['timestamp', /^((\d\d\d\d)-(\d\d?)-(\d\d?)(?:(?:[ \t]+)(\d\d?):(\d\d)(?::(\d\d))?)?)/],
['float', /^(\d+\.\d+)/],
['int', /^(\d+)/],
['doc', /^---/],
@@ -233,6 +234,8 @@ Parser.prototype.parse = function() {
return this.parseHash()
case 'string':
return this.advanceValue()
+ case 'timestamp':
+ return this.parseTimestamp()
case 'float':
return parseFloat(this.advanceValue())
case 'int':
@@ -338,6 +341,29 @@ Parser.prototype.parseInlineList = function() {
}
/**
+ * yyyy-mm-dd hh:mm:ss
+ *
+ * For full format: http://yaml.org/type/timestamp.html
+ */
+
+Parser.prototype.parseTimestamp = function() {
+ var token = this.advance()[1]
+ var date = new Date
+ var year = token[2]
+ , month = token[3]
+ , day = token[4]
+ , hour = token[5] || 0
+ , min = token[6] || 0
+ , sec = token[7] || 0
+
+ date.setUTCFullYear(year, month-1, day)
+ date.setUTCHours(hour)
+ date.setUTCMinutes(min)
+ date.setUTCSeconds(sec)
+ return date
+}
+
+/**
* Evaluate a _str_ of yaml.
*
* @param {string} str
Please sign in to comment.
Something went wrong with that request. Please try again.