Skip to content

Commit

Permalink
Fix ISO week
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Aug 24, 2013
1 parent 5c2bd38 commit 4db664b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Date format – [![Build][1]][2] [![Coverage][3]][4]
Lite version of Date format and parse for node.js and browser
that extends native objects.
Download [compressed][min]
(1895 bytes, 1044 bytes gzipped)
(1979 bytes, 1079 bytes gzipped)
or [uncompressed][src] source.


Expand Down Expand Up @@ -116,9 +116,15 @@ See [tests][test] for more examples
- **a** - Lowercase Ante meridiem and Post meridiem. am or pm
- **A** - Uppercase Ante meridiem and Post meridiem. AM or PM
- **Z** - Difference to Greenwich time (GMT) with colon between hours and minutes. Example: GMT +02:00
- **w** - Week number of year, first week is the week with 4 January in it
- **"text"** - text, quotes should be escaped, eg '"a \\"quoted text\\"" YYYY'

###### ISO-8601
- **w** - Day of the week. 1 (for Monday) through 7 (for Sunday)
- **W** - Week number of year, first week is the week with 4 January in it
- **o** - ISO-8601 year number. This has the same value as YYYY,
except that if the ISO week number (W) belongs to the previous or next year,
that year is used instead

### Browser Support

[![browser support][7]][8]
Expand Down
40 changes: 14 additions & 26 deletions date-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,40 @@


/*
* @version 0.2.0
* @version 0.3.0
* @author Lauri Rooden - https://github.com/litejs/date-format-lite
* @license MIT License - http://lauri.rooden.ee/mit-license.txt
*/



!function(Date, proto) {
var maskRe = /(["'])((?:[^\\]|\\.)*?)\1|YYYY|[MD]{3,4}|SS|([YMDHhmsw])(\3?)|[uUaAZS]/g
var maskRe = /(["'])((?:[^\\]|\\.)*?)\1|YYYY|[MD]{3,4}|SS|([YMDHhmsW])(\3?)|[uUaAZSwo]/g
, yearFirstRe = /(\d{4})[-.\/](\d\d?)[-.\/](\d\d?)/
, dateFirstRe = /(\d\d?)[-.\/](\d\d?)[-.\/](\d{4})/
, isoDateRe = /(\d{4})[-.\/]W(\d\d?)[-.\/](\d)/
, timeRe = /(\d\d?):(\d\d):?(\d\d)?\.?(\d{3})?/
, periodRe = /pm/i
, wordRe = /.[a-z]+/g
, unescapeRe = /\\(.)/g


function p2(n) {
return n>9?n:"0"+n
function nearestThursday(self) {
return new Date(+self + ((4 - (self.getDay()||7)) * 86400000))
}

function p3(n) {
return (n>99?n:(n>9?"0":"00")+n)
}

//** Date.format
// ISO 8601 specifies numeric representations of date and time.
// The international standard date notation is
//
// The international standard date notation is
// YYYY-MM-DD
// The international standard notation for the time of day is
//
// The international standard notation for the time of day is
// hh:mm:ss
//
// TODO:2012-03-05:lauriro:Date week number not complete
// http://en.wikipedia.org/wiki/ISO_week_date
//
// Time zone
//
// The strings
//
// +hh:mm, +hhmm, or +hh
//
// can be added to the time to indicate that the used local time zone is hh hours and mm minutes ahead of UTC. For time zones west of the zero meridian, which are behind UTC, the notation
//
// -hh:mm, -hhmm, or -hh
//
// is used instead. For example, Central European Time (CET) is +0100 and U.S./Canadian Eastern Standard Time (EST) is -0500. The following strings all indicate the same point of time:
// The strings +hh:mm, +hhmm, or +hh (ahead of UTC)
// -hh:mm, -hhmm, or -hh (time zones west of the zero meridian, which are behind UTC)
//
// 12:00Z = 13:00+01:00 = 0700-0500

Expand All @@ -73,16 +59,18 @@
: single == "m" ? self[get + "Minutes"]()
: single == "s" ? self[get + "Seconds"]()
: match == "S" ? self[get + "Milliseconds"]()
: match == "SS" ? p3(self[get + "Milliseconds"]())
: match == "SS" ? (quote = self[get + "Milliseconds"](), quote > 99 ? quote : (quote > 9 ? "0" : "00" ) + quote)
: match == "u" ? (self/1000)>>>0
: match == "U" ? +self
: match == "a" ? (self[get + "Hours"]() > 11 ? "pm" : "am")
: match == "A" ? (self[get + "Hours"]() > 11 ? "PM" : "AM")
: match == "Z" ? "GMT " + (-self.getTimezoneOffset()/60)
: single == "w" ? 1+Math.floor((self - new Date(self[get + "FullYear"](),0,4))/604800000)
: match == "w" ? self[get + "Day"]() || 7
: single == "W" ? (quote = nearestThursday(self), Math.ceil((1+((+quote-quote.setMonth(0,1))/86400000))/7) )
: match == "o" ? nearestThursday(self)[get + "FullYear"]()
: quote ? text.replace(unescapeRe, "$1")
: match
return pad ? p2(text) : text
return pad && text < 10 ? "0"+text : text
})
}

Expand Down
8 changes: 4 additions & 4 deletions min.date-format.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "date-format-lite",
"version": "0.2.0",
"version": "0.3.0",
"license": "MIT",
"description": "Date format and parser for node.js and browser",
"main": "date-format.js",
Expand Down
36 changes: 36 additions & 0 deletions tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ describe ("Date.format").
equal( d2.format("UTC:YY-M-D h:m:s"), "01-9-9 1:46:40" ).
equal( d3.format("UTC:Y MMM DDD H A"), "9 Feb Fri 11 PM" ).

it ( "should format ISO 8601 week numbers" ).
equal( "2005-01-01".date("o-'W'WW-w"), "2004-W53-6").
equal( "2005-01-02".date("o-'W'WW-w"), "2004-W53-7").
equal( "2005-12-31".date("o-'W'WW-w"), "2005-W52-6").
equal( "2007-01-01".date("o-'W'WW-w"), "2007-W01-1").
equal( "2007-12-30".date("o-'W'WW-w"), "2007-W52-7").
equal( "2007-12-31".date("o-'W'WW-w"), "2008-W01-1").
equal( "2008-01-01".date("o-'W'WW-w"), "2008-W01-2").
equal( "2008-12-28".date("o-'W'WW-w"), "2008-W52-7").
equal( "2008-12-29".date("o-'W'WW-w"), "2009-W01-1").
equal( "2008-12-30".date("o-'W'WW-w"), "2009-W01-2").
equal( "2008-12-31".date("o-'W'WW-w"), "2009-W01-3").
equal( "2009-01-01".date("o-'W'WW-w"), "2009-W01-4").
equal( "2009-12-31".date("o-'W'WW-w"), "2009-W53-4").
equal( "2010-01-01".date("o-'W'WW-w"), "2009-W53-5").
equal( "2010-01-02".date("o-'W'WW-w"), "2009-W53-6").
equal( "2010-01-03".date("o-'W'WW-w"), "2009-W53-7").

it ( "should parse ISO 8601 week numbers" , { skip: "Not implemented" }).
equal( "2004-W53-6".date("YYYY-MM-DD"), "2005-01-01" ).
equal( "2004-W53-7".date("YYYY-MM-DD"), "2005-01-02" ).
equal( "2005-W52-6".date("YYYY-MM-DD"), "2005-12-31" ).
equal( "2007-W01-1".date("YYYY-MM-DD"), "2007-01-01" ).
equal( "2007-W52-7".date("YYYY-MM-DD"), "2007-12-30" ).
equal( "2008-W01-1".date("YYYY-MM-DD"), "2007-12-31" ).
equal( "2008-W01-2".date("YYYY-MM-DD"), "2008-01-01" ).
equal( "2008-W52-7".date("YYYY-MM-DD"), "2008-12-28" ).
equal( "2009-W01-1".date("YYYY-MM-DD"), "2008-12-29" ).
equal( "2009-W01-2".date("YYYY-MM-DD"), "2008-12-30" ).
equal( "2009-W01-3".date("YYYY-MM-DD"), "2008-12-31" ).
equal( "2009-W01-4".date("YYYY-MM-DD"), "2009-01-01" ).
equal( "2009-W53-4".date("YYYY-MM-DD"), "2009-12-31" ).
equal( "2009-W53-5".date("YYYY-MM-DD"), "2010-01-01" ).
equal( "2009-W53-6".date("YYYY-MM-DD"), "2010-01-02" ).
equal( "2009-W53-7".date("YYYY-MM-DD"), "2010-01-03" ).

it ( "should accept text in quotes" ).
equal( d3.format('UTC:"Bla \\"a\\":"hh:mm'), 'Bla "a":23:31' ).
equal( d3.format("UTC:'Bla \\'a\\':'hh:mm"), "Bla 'a':23:31" ).
Expand Down

0 comments on commit 4db664b

Please sign in to comment.