Skip to content

Commit

Permalink
use daysPerYear to calculate month / year seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 25, 2017
1 parent 6a85f0f commit f7366f6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog

## 5.0.0

- Add `daysPerYear` configuration option
- Use `daysPerYear` configuration option to convert months or years to seconds

## 4.0.0

- Drop support for Node.js `< 4.0.0`
Expand Down
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -111,6 +111,7 @@ A few assumptions are made by default:
2. There are 7 days per week
3. There are 4 weeks per month
4. There are 12 months per year
5. There are 365.25 days per year

These options can be changed by passing an options object as an argument to `timestring`.

Expand All @@ -120,6 +121,7 @@ The following options are configurable:
2. `daysPerWeek`
3. `weeksPerMonth`
4. `monthsPerYear`
5. `daysPerYear`

```js
const timestring = require('timestring')
Expand All @@ -138,7 +140,7 @@ In the example above `hoursPerDay` is being set to `1`. When the time string is

This would be useful for specific application needs.

*Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type `1d` i want 7.5 hours to be tracked. When they type `1w` i want 5 days to be tracked etc.*
*Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type `1d` i want 7.5 hours to be tracked. When they type `1w` i want 5 days to be tracked etc.*

```js
const timestring = require('timestring')
Expand All @@ -154,3 +156,5 @@ let daysThisWeek = timestring('1w', 'd', opts)
console.log(hoursToday) // will log 7.5
console.log(daysThisWeek) // will log 5
```

It is important to note that the `daysPerYear` configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years.
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -16,7 +16,8 @@ const DEFAULT_OPTS = {
hoursPerDay: 24,
daysPerWeek: 7,
weeksPerMonth: 4,
monthsPerYear: 12
monthsPerYear: 12,
daysPerYear: 365.25
}

/**
Expand Down Expand Up @@ -88,8 +89,8 @@ function getUnitValues (opts) {

unitValues.d = opts.hoursPerDay * unitValues.h
unitValues.w = opts.daysPerWeek * unitValues.d
unitValues.mth = opts.weeksPerMonth * unitValues.w
unitValues.y = opts.monthsPerYear * unitValues.mth
unitValues.mth = (opts.daysPerYear / opts.monthsPerYear) * unitValues.d
unitValues.y = opts.daysPerYear * unitValues.d

return unitValues
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "timestring",
"version": "4.0.0",
"version": "5.0.0",
"description": "Parse a human readable time string into a time based value",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 7 additions & 6 deletions test.js
Expand Up @@ -15,8 +15,8 @@ describe('timestring', () => {
expect(timestring('1h')).to.equal(3600)
expect(timestring('1d')).to.equal(86400)
expect(timestring('1w')).to.equal(604800)
expect(timestring('1mth')).to.equal(2419200)
expect(timestring('1y')).to.equal(29030400)
expect(timestring('1mth')).to.equal(2629800)
expect(timestring('1y')).to.equal(31557600)
})

it('can parse different unit identifiers', () => {
Expand Down Expand Up @@ -56,11 +56,11 @@ describe('timestring', () => {
})

unitMap.mth.forEach(unit => {
expect(timestring(`9 ${unit}`)).to.equal(21772800)
expect(timestring(`9 ${unit}`)).to.equal(23668200)
})

unitMap.y.forEach(unit => {
expect(timestring(`1 ${unit}`)).to.equal(29030400)
expect(timestring(`1 ${unit}`)).to.equal(31557600)
})
})

Expand All @@ -75,12 +75,13 @@ describe('timestring', () => {
hoursPerDay: 1,
daysPerWeek: 2,
weeksPerMonth: 3,
monthsPerYear: 4
monthsPerYear: 4,
daysPerYear: 30
}

expect(timestring('1d', 'h', opts)).to.equal(1)
expect(timestring('1w', 'd', opts)).to.equal(2)
expect(timestring('1mth', 'w', opts)).to.equal(3)
expect(timestring('1mth', 'w', opts)).to.equal(3.75)
expect(timestring('1y', 'mth', opts)).to.equal(4)
})

Expand Down

0 comments on commit f7366f6

Please sign in to comment.