Permalink
Browse files

fixes dates problem.

Wheat has a problem with dates in that it is usually off by one
for the day of the week.

Removes the dates code from wheat.
Adds a dependency to a dedicated module for formatting dates.
This should remove duplicated code between projects.

Small upgrade issue with the date formatters.

old format -> new format
F -> B
j -> e
l -> A
i -> M
s -> S
u -> XXXXX
  • Loading branch information...
1 parent 946deaa commit 0ae026609d6e9af22f89be5400cec2237875852b @CrypticSwarm CrypticSwarm committed Sep 27, 2011
Showing with 7 additions and 80 deletions.
  1. +6 −80 lib/wheat/tools.js
  2. +1 −0 package.json
View
@@ -3,36 +3,8 @@ var Step = require('step'),
Markdown = require('./markdown'),
MD5 = require('./md5'),
Buffer = require('buffer').Buffer,
- Git = require('git-fs');
-
-function pad(num, count) {
- count = count || 2;
- num = "" + num;
- for (i = num.length; i < count; i ++) num = "0" + num;
- return num;
-}
-
-// accepts the client's time zone offset from GMT in minutes as a parameter.
-// returns the timezone offset in the format [+|-}DDDD
-function getTZOString(timezoneOffset)
-{
- var hours = Math.floor(timezoneOffset / 60);
- var modMin = Math.abs(timezoneOffset % 60);
- var s = new String();
- s += (hours > 0) ? "-" : "+";
- var absHours = Math.abs(hours);
- s += (absHours < 10) ? "0" + absHours :absHours;
- s += ((modMin == 0) ? "00" : modMin);
- return(s);
-}
-
-var daysInWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
- "Saturday", "Sunday"];
-var monthsInYear = ["January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"];
-var aMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
-var aDays = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
+ Git = require('git-fs'),
+ datetime = require('datetime');
var Helpers = {
inspect: require(process.binding('natives').util ? 'util' : 'sys').inspect,
@@ -58,57 +30,11 @@ var Helpers = {
MD5.md5((email+"").trim().toLowerCase()) +
"?r=pg&s=" + size + ".jpg&d=identicon";
},
- formatDate: function formatDate(val, format) {
- var date = new Date(val),
- match, value;
- while (match = format.match(/(%[a-z%])/i)) {
- switch (match[1]) {
- case "%Y": // A full numeric representation of a year, 4 digits
- value = date.getFullYear(); break;
- case "%m": // Numeric representation of a month, with leading zeros
- value = pad(date.getMonth() + 1); break;
- case "%F": // A full textual representation of a month like March
- value = monthsInYear[date.getMonth()]; break;
- case "%d": // Day of the month, 2 digits with leading zeros
- value = pad(date.getDate()); break;
- case "%j": // Day of the month without leading zeros
- value = date.getDate(); break;
- case "%l": // A full textual representation of the day of the week
- value = daysInWeek[date.getDay()]; break;
- case "%H": // 24-hour format of an hour with leading zeros
- value = pad(date.getHours()); break;
- case "%i": // Minutes with leading zeros
- value = pad(date.getMinutes()); break;
- case "%s": // Seconds, with leading zeros
- value = pad(date.getSeconds()); break;
- case "%u": // milliseconds with leading zeroes
- value = pad(date.getMilliseconds(), 3); break;
- case "%z": // time zone offset
- value = getTZOString(date.getTimezoneOffset()); break;
- case "%%": // literal % sign
- value = "\0%\0"; break;
- default:
- value = ""; break;
- }
- format = format.replace(match[1], value);
- }
- format = format.split("\0%\0").join("%");
- return format;
+ formatDate: function formatDate(val, format, tz, locale) {
+ return datetime.format(new Date(val), format, tz, locale);
},
- formatRFC822Date: function formatRFC822Date(val)
- {
- var oDate = new Date(val);
- var dtm = new String();
-
- dtm = aDays[oDate.getDay()] + ", ";
- dtm += pad(oDate.getDate()) + " ";
- dtm += aMonths[oDate.getMonth()] + " ";
- dtm += oDate.getFullYear() + " ";
- dtm += pad(oDate.getHours()) + ":";
- dtm += pad(oDate.getMinutes()) + ":";
- dtm += pad(oDate.getSeconds()) + " " ;
- dtm += getTZOString(oDate.getTimezoneOffset());
- return dtm;
+ formatRFC822Date: function formatRFC822Date(val) {
+ return datetime.format(new Date(val), "%a, %d %b %Y %H:%M:%S %z");
}
};
View
@@ -16,6 +16,7 @@
"simple-mime": ">=0.0.1",
"haml": ">=0.2.5",
"step": ">=0.0.3",
+ "datetime": "0.0.3",
"proto": ">=0.1.0"
}
}

0 comments on commit 0ae0266

Please sign in to comment.