-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change/timezone locale #6
Changes from 4 commits
ac0f6a5
aca7024
2f17691
a044b20
194789e
9af9c20
c5568e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var SugarDate = require('sugar-date').Date; | ||
require('sugar-date/locales'); | ||
|
||
var Utils = {}; | ||
|
||
|
@@ -25,30 +27,28 @@ var formatParsedObject = function(type, input, valid, parsed) { | |
}; | ||
|
||
/* istanbul ignore next */ | ||
Utils._getCurrentDate = function() { | ||
Utils._getCurrentDate = function(options) { | ||
// Makes unit testing possible, by allowing this | ||
// function to be mocked. | ||
return new Date(); | ||
return SugarDate.create('now', options); | ||
}; | ||
|
||
/* istanbul ignore next */ | ||
Utils._getFutureDate = function(str) { | ||
Utils._getFutureDate = function(str, options) { | ||
// Makes unit testing possible, by allowing this | ||
// function to be mocked. | ||
return Date.future(str); | ||
return SugarDate.create(str, _.assign({future: true}, options)); | ||
}; | ||
|
||
Utils.activateDateParser = function() { | ||
// Add support for the 'enhanced' date object. | ||
// This augments the Date prototype with extra methods, | ||
// and (importantly) allows use to use the Sugar.js | ||
// date parsing algorithms. | ||
// In an ideal world, we wouldn't augment the prototype, | ||
// but our hands are ties if we want to use the date parsing | ||
// feature that Sugar.js gives us. | ||
// Perhaps one day this can be extracted and added as a | ||
// moment.js plugin. | ||
require('sugar-date'); | ||
|
||
// v2 of SugarDate supports optionally extending. | ||
// This is still here to support legacy dependencies | ||
SugarDate.extend(); | ||
}; | ||
|
||
/** | ||
|
@@ -405,6 +405,8 @@ Utils.cloneTerse = function(input) { | |
* - Utils.parseDateTimeField('may 25th of next year') | ||
* - Utils.parseDateTimeField('2014-01-18 09:30:00') | ||
* - Utils.parseDateTimeField('2014-01-18 09:30:00 -0400') | ||
* - Utils.parseDateTimeField('2014-01-18 09:30:00', 'en-GB') | ||
* - Utils.parseDateTimeField('2014-01-18 09:30:00', {fromUTC: true, setUTC: true}) | ||
* - Utils.parseDateTimeField('in 2 days') | ||
* - Utils.parseDateTimeField('5 minutes from now') | ||
* - Utils.parseDateTimeField('2014-01-18 09:30:00 -0400 +2d +30m') | ||
|
@@ -417,24 +419,29 @@ Utils.cloneTerse = function(input) { | |
* - -4d -6h will reduce the parsed date by 4 days and 6 hours | ||
* | ||
* @param {String} field the date string to parse. | ||
* @param {String} locale the locale string OR options object. | ||
* @return {Object} an object containing the parsed date, or the passed field if it was not a String. | ||
*/ | ||
Utils.parseDateTimeField = function(field) { | ||
Utils.parseDateTimeField = function(field, locale) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment in Flow XO core, it would be good to allow the SugarDate creation to be customised for the lifetime of this function invocation by passing in an optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since I wrote this comment, this feature has been implemented! So please ignore. 😄 |
||
var getRtnObject = function(parsed, isValid) { | ||
return formatParsedObject('date', field, isValid, parsed); | ||
}; | ||
|
||
var parsedDate; | ||
var options = {}; | ||
if (_.isString(locale)) { | ||
options.locale = locale; | ||
} else if (locale) { | ||
options = locale; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused why we need this if/else statement? This appears to be an addition to the API so why not just make every caller pass in an object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My original thinking was to mimic the second param of |
||
|
||
// Ensure we have Date superpowers | ||
Utils.activateDateParser(); | ||
var parsedDate; | ||
|
||
if(_.isDate(field)) { | ||
parsedDate = field; | ||
|
||
} else if(!_.isString(field)) { | ||
// Just create a date from the passed value. | ||
parsedDate = Date.create(field); | ||
parsedDate = SugarDate.create(field, options); | ||
|
||
} else { | ||
// Regex for parsing a offset modifier. | ||
|
@@ -482,18 +489,18 @@ Utils.parseDateTimeField = function(field) { | |
// Otherwise, parse the string to create a date in the future. | ||
parsedDate = | ||
withoutOffsetModifiers === '' && hasOffsetModifier ? | ||
Utils._getCurrentDate() : | ||
Utils._getFutureDate(withoutOffsetModifiers); | ||
Utils._getCurrentDate(options) : | ||
Utils._getFutureDate(withoutOffsetModifiers, options); | ||
|
||
if(parsedDate.isValid() && hasOffsetModifier && offsetSecs) { | ||
if(SugarDate.isValid(parsedDate) && hasOffsetModifier && offsetSecs) { | ||
// Apply the offset modifier. | ||
// If it is negative, it will subtract. | ||
parsedDate.addSeconds(offsetSecs); | ||
SugarDate.addSeconds(parsedDate, offsetSecs); | ||
} | ||
} | ||
|
||
// parsedDate will always be a date at this point. | ||
return getRtnObject(parsedDate, parsedDate.isValid()); | ||
return getRtnObject(parsedDate, SugarDate.isValid(parsedDate)); | ||
}; | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,6 @@ | |
"dependencies": { | ||
"bluebird": "^3.4.1", | ||
"lodash": "^4.14.2", | ||
"sugar-date": "1.5.1" | ||
"sugar-date": "^2.0.4" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be better written as:
The advantage here is that an
options
object containingfuture: false
would not cause issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! I see what you are saying.