full-day-range
Advanced tools
Comparing version 0.0.2 to 0.1.0
var dayInterval = require('../') | ||
var parseTime = require('../parse-time') | ||
var parseTime = require('parse-time-to-ms') | ||
var day = new Date('2017-01-01T14:30:00') | ||
var timeRange = [parseTime('18:00'), parseTime('23:30')] | ||
var timeRange = parseTime('18:00', '23:30') | ||
var dayCustomRange = dayInterval(day, { range: timeRange }) | ||
console.log(dayCustomRange.map(d => d.toString())) |
// process.env.TZ = 'Pacific/Noumea' // local timezone used in this example | ||
var tzVancouver = new Intl.DateTimeFormat('en-CA', { | ||
timeZone: 'America/Vancouver', | ||
year: 'numeric', month: 'long', day: 'numeric', | ||
hour: 'numeric', minute: 'numeric', second: 'numeric', | ||
timeZone: 'America/Vancouver', | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
hour12: false | ||
@@ -13,3 +17,2 @@ }) | ||
// wrong day range, as JavaScript is not anymore aware of the initial timezone: | ||
@@ -19,7 +22,5 @@ | ||
console.log(range.map(d => d.toString())) | ||
console.log(range.map(d => d.toString())) | ||
// => [ 'Mon Jan 02 2017 00:00:00 GMT+1100 (DST)', 'Tue Jan 03 2017 00:00:00 GMT+1100 (DST)' ] | ||
// first option is to pass a timezone option: | ||
@@ -36,7 +37,5 @@ | ||
// or easier if you have acces to the initial timezoned string: | ||
var easyTzRange = dayRange(timezoneISODateString) | ||
var easyTzRange = dayRange(timezoneISODateString) | ||
@@ -46,4 +45,2 @@ console.log(easyTzRange.map(d => d.toString())) | ||
// finally if you simply want the right day, but keep local time range: | ||
@@ -50,0 +47,0 @@ var localRange = dayRange(day, { timezone: '-08:00', localTime: true }) |
29
index.js
var timezoneShift = require('shift-timezone-offset') | ||
var defaults = { | ||
range: [0, 24*60*60*1000], | ||
range: [0, 24 * 60 * 60 * 1000], | ||
localTime: false, | ||
@@ -10,18 +10,8 @@ exclusive: false, | ||
/** | ||
* Trim a date range to a specific day | ||
* | ||
* @param {Date|string} - Date to get range | ||
* @param {object} options | ||
* @prop {Array<int|string>} range - default: [0, 24*60*60*1000]. Day time limits in ms. | ||
* @prop {string} timezone - default: local timezone. Parsable timezone, eg. '+05:00', '-1100' or an ISO date: '2017-01-01T12:00:00+05:00'. | ||
* @prop {bool} localTime - default: false. Return interval in local time to improve working with time values. | ||
* @prop {bool} exclusive - default: false. If true limits day interval to the last mis before end of range, eg. 23:59:59.999. | ||
* @returns {Array<Date>} - day range | ||
*/ | ||
module.exports = dayRange | ||
module.exports = function(day, options) { | ||
function dayRange (day, options) { | ||
options = options || defaults | ||
var localTime = options.localTime || defaults.localTime | ||
var timezone = options.timezone || (typeof day == 'string' ? day : null) | ||
var timezone = options.timezone || (typeof day === 'string' ? day : null) | ||
options.range = options.range || defaults.range | ||
@@ -35,10 +25,9 @@ var start = options.range[0] || defaults.range[0] | ||
var dayStart = floor(workDate, start) | ||
var dayEnd = floor(workDate, options.exclusive ? end -1 : end) | ||
return localTime ? [dayStart, dayEnd] : | ||
[converter.fromLocal(dayStart), converter.fromLocal(dayEnd)] | ||
var dayEnd = floor(workDate, options.exclusive ? end - 1 : end) | ||
return localTime ? [dayStart, dayEnd] | ||
: [converter.fromLocal(dayStart), converter.fromLocal(dayEnd)] | ||
} | ||
function floor(date, msOffset) { | ||
function floor (date, msOffset) { | ||
var d = new Date(date) | ||
@@ -45,0 +34,0 @@ d.setHours(0) |
{ | ||
"name": "full-day-range", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Given a date, provide a full day range, or custom subset. Timezone aware.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test/*.js" | ||
"test": "standard && tape test/*.js" | ||
}, | ||
@@ -22,4 +22,6 @@ "author": "krazylek (http://pohl.fr)", | ||
"devDependencies": { | ||
"parse-time-to-ms": "0.0.1", | ||
"standard": "^10.0.3", | ||
"tape": "^4.8.0" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # full-day-range | ||
``` js | ||
var dayRange = require('../') | ||
var dayRange = require('full-day-range') | ||
var day = new Date('2017-01-01T14:30:00') | ||
@@ -42,3 +42,3 @@ var range = dayRange(day) | ||
var dayRange = require('../') | ||
var dayRange = require('full-day-range') | ||
var timezoneISODateString = '2017-01-01T14:30:00-08:00' | ||
@@ -64,3 +64,3 @@ var day = new Date(timezoneISODateString) | ||
// check up: | ||
// verify the previous result: | ||
console.log(timezoneRange.map(tzVancouver.format)) | ||
@@ -87,5 +87,3 @@ // => [ 'January 1, 2017, 00:00:00', 'January 2, 2017, 00:00:00' ] | ||
Unfortunately, sometimes you could want to work with other timezone, which could to lead to unexpected behaviors. | ||
# custom day range | ||
@@ -95,12 +93,12 @@ | ||
The range could be set with an array of milliseconds (from `00:00` current day). Default is `[0, 24*60*60*1000]`. | ||
The range is be set with an array of milliseconds (from `00:00` current day). Default is `[0, 24*60*60*1000]`. | ||
To allow more convenience, a tiny parser is provided: `var parseTime = require('full-day-range/parse-time')`. | ||
To allow more convenience, the tiny `parse-time-to-ms` module could be use. | ||
``` | ||
var dayInterval = require('../') | ||
var parseTime = require('../parse-time') | ||
var dayRange = require('full-day-range') | ||
var parseTime = require('parse-time-to-ms') | ||
var day = new Date('2017-01-01T14:30:00') | ||
var timeRange = [parseTime('18:00'), parseTime('23:30')] | ||
var timeRange = parseTime.s('18:00', '23:30') | ||
var dayCustomRange = dayInterval(day, { range: timeRange }) | ||
@@ -126,20 +124,9 @@ | ||
* `dayDate` - any valid value for the `Date` constructor. This is the base date for the range. | ||
* `optse` - the date range to be trimmed to fit inside the provided day. Hqave to be ordered: `[startDate, endDate]`. | ||
* `opts.timezone` - Valid ISO 8601 date string or timezone string. Change the timezone to work with. | ||
* `opts.range` - Default is `[0, 24*60*60*1000]`. Array of milliseconds to offset the range: `[startMs, endMs]`. | ||
* `opts.exclusive` - default `false`. If you want to exclude the last millisecond, so the day range is from current day `00:00:00.000` to `23:59:59.999`. | ||
* `opts.localTime` - default `false`. If you want to convert the range to local timezone. | ||
* `opts` | ||
* `opts.timezone` - Valid ISO 8601 date string or timezone string. Change the timezone to work with. | ||
* `opts.range` - Default is `[0, 24*60*60*1000]`. Array of milliseconds to offset the range: `[startMs, endMs]`. | ||
* `opts.exclusive` - default `false`. Exclude the last millisecond, so the day range is from current day `00:00:00.000` to `23:59:59.999`. | ||
* `opts.localTime` - default `false`. Convert the range to local timezone. | ||
--- | ||
`var parseTime = require('full-day-range/parse-time')` | ||
## `parseTime(timeString)` | ||
10 lines module to convert a time string into milliseconds. | ||
* `timeString` - a valid time string, have to start with hours (eg. '35:10' would be interpreted as 35 hours 10 minutes). Some valid formats: `'02:35'`, `'02:35:55'` or `'2:35:55.010'`. | ||
# license | ||
@@ -155,1 +142,8 @@ | ||
``` | ||
# see also | ||
- https://github.com/krazylek/parse-time-to-ms Parse ISO time string to milliseconds. | ||
- https://github.com/unshiftio/millisecond Parse natural language to milliseconds. | ||
var dayInterval = require('../') | ||
var parseTime = require('../parse-time') | ||
var parseTime = require('parse-time-to-ms') | ||
var test = require('tape') | ||
test('valid local day range', function (t) { | ||
var day = new Date(2017,0,1,12) | ||
var day = new Date(2017, 0, 1, 12) | ||
var expectedStart = new Date('2017-01-01T00:00:00') | ||
@@ -15,3 +15,3 @@ var expectedEnd = new Date('2017-01-02T00:00:00') | ||
test('day could be a timestamp', function (t) { | ||
var day = new Date(2017,0,1,12).getTime() | ||
var day = new Date(2017, 0, 1, 12).getTime() | ||
var expectedStart = new Date('2017-01-01T00:00:00') | ||
@@ -76,3 +76,2 @@ var expectedEnd = new Date('2017-01-02T00:00:00') | ||
test('custom day range in alt timezone time', function (t) { | ||
@@ -90,3 +89,2 @@ var timezoneISODateString = '2017-01-01T12:00:00+08:00' | ||
test('all together: custom day range converted to local', function (t) { | ||
@@ -93,0 +91,0 @@ var timezoneISODateString = '2017-01-01T17:00:00-08:00' |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12479
3
9
158
143