dodo/node-formatdate — Issue #2: patch to make short times come up as 'a moment ago' 
Author: highfellow
Created: 2012-03-08 19:10:44+00:00
State: open

Hi dodo,

I think I've come up with a patch that should allow short times ago to display as 'a moment ago'. I added some options to formatdate.options, which allow you to configure the default unit, amount, and what string to display.

I've pasted the patch below:

```
*** formatdate-old.coffee	2012-03-08 19:10:38.000000000 +0100
--- formatdate.coffee	2012-03-08 19:47:43.000000000 +0100
***************
*** 41,46 ****
--- 41,50 ----
max:
amount: 42
unit: 9 # century
+ min:
+ amount: 1
+ unit: 2 # minute
+ string: "a moment" # string to show when below min.

exports.locale = locale =
'default':"%T"
***************
*** 66,73 ****
--- 70,83 ----
opts.max ?= {}
opts.max.unit ?= defaults.max.unit
opts.max.amount ?= defaults.max.amount
+ opts.min ?= {}
+ opts.min.unit ?= defaults.min.unit
+ opts.min.amount ?= defaults.min.amount
+ opts.min.string ?= defaults.min.string
# turn ago off when a special amount of special unit is reached (defaults to strftime)
return off if unit is opts.max.unit and amount > opts.max.amount
+ # return special string when below a minimum amount of a minimum unit.
+ return opts.min.string + " ago" if unit < opts.min.unit or ( unit is opts.min.unit and amount < opts.min.amount )
res = ""
res += amount if amount > 1
res += "a" if amount <= 1
```

I've tested this in the coffee-script interpreter, and it works OK:

```
coffee> fd=require('./formatdate')
...
coffee> console.log fd.strftime('%c',1)
a moment ago
undefined
coffee> console.log fd.strftime('%c',1000)
a moment ago
undefined
coffee> console.log fd.strftime('%c',60000)
a minute ago
undefined
coffee> console.log fd.strftime('%c',120000)
2 minutes ago
undefined
coffee> console.log fd.strftime('%c',59999)
a moment ago
undefined
coffee> console.log fd.strftime('%c',1200000)
20 minutes ago
undefined
coffee> console.log fd.options.min.amount
1
undefined
coffee> fd.options.min.amount=5
5
coffee> console.log fd.options.min.amount
5
undefined
coffee> console.log fd.strftime('%c',120000)
a moment ago
undefined
coffee> console.log fd.strftime('%c',300000)
5 minutes ago
```

If I've missed something, let me know and I'll try to fix it.