From c5362e748c43c6673be83cec92e8887bf92cb60b Mon Sep 17 00:00:00 2001 From: Andrew Schaaf Date: Tue, 7 Jun 2011 15:18:33 -0400 Subject: [PATCH] add %L for 3-digit milliseconds --- lib/index.js | 14 ++++++++++++++ test/test.coffee | 12 +++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1772807..347d2fa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -42,6 +42,19 @@ return n < 10 ? (padding + n) : n; } + function pad3(n, padding) { + padding = padding || '0'; + if (n < 10) { + return padding + padding + n; + } + else if (n < 100) { + return padding + n; + } + else { + return n; + } + } + function hours12(d) { var hour = d.getHours(); if (hour == 0) hour = 12; @@ -101,6 +114,7 @@ case 'r': return strftime(locale.formats.r || '%I:%M:%S %p', d, locale); case 'S': return pad(d.getSeconds()); case 's': return Math.floor(d.getTime() / 1000); + case 'L': return pad3(Math.floor(d.getTime() % 1000)); case 'T': return strftime(locale.formats.T || '%H:%M:%S', d, locale); case 't': return '\t'; case 'u': diff --git a/test/test.coffee b/test/test.coffee index f29abdb..9ab9f0e 100644 --- a/test/test.coffee +++ b/test/test.coffee @@ -10,19 +10,21 @@ t = new Date 1307472705867 TESTS = [ - ["%Y", "2011", "2011"] + ["%L", "867"] + + ["%Y", "2011"] ["%m", "06"] - ["%b", "Jun", "Jun"] - ["%B", "June", "June"] + ["%b", "Jun"] + ["%B", "June"] ["%d", null, "07"] ["%H", null, "18"] - ["%M", "51", "51"] + ["%M", "51"] - ["%S", "45", "45"] + ["%S", "45"] ["%s", "1307472705"] ]