Skip to content
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

Duration method is not correctly parsing string passed with a positive sign #4002

Closed
kshitijb opened this issue Jun 8, 2017 · 4 comments
Closed
Labels

Comments

@kshitijb
Copy link
Contributor

kshitijb commented Jun 8, 2017

Description
I am trying to add or subtract a duration from UTC 0000 hours. I am using duration method to create a duration and then passing that into the add method. The documentation mentions that support of mixed negative and positive sign was added in V 2.13.0. But using a positive sign is returning 0. The code is available in this fiddle - https://jsfiddle.net/kshitij/9jqvfk3g/

Code Snippet

const m = moment().utcOffset(0);
m.set({hour:0,minute:0,second:0,millisecond:0})
m.utc().format('HH:mm')

let d = moment.duration('+03:30').asHours();
console.log('Positive offset: ', d); // Positive offset: 0
console.log('Positive time: ', m.add(d, 'H').format()); // Positive time: 2017-06-08T00:00:00Z

let b = moment.duration('-03:30').asHours();
console.log('Negative offset: ', b); // Negative offset:  -3.5
console.log('Negative time: ', m.add(b, 'H').format()); // Negative time:  2017-06-07T20:30:00Z

I have tested this on my machine as well as jsfiddle. I had created an issue on StackOverflow for this but didn't get a satisfactory answer, so posting it here https://stackoverflow.com/questions/44430239/momentjs-duration-is-not-parsing-positive-sign-correctly?noredirect=1#comment75871221_44430239

@kshitijb kshitijb changed the title Duration method is not parsing string passed with positive sign correctly Duration method is not correctly parsing string passed with positive sign Jun 8, 2017
@kshitijb kshitijb changed the title Duration method is not correctly parsing string passed with positive sign Duration method is not correctly parsing string passed with a positive sign Jun 8, 2017
@icambron
Copy link
Member

icambron commented Jun 10, 2017

This seems like a bug, yeah. If we accept -, then we should accept +. Pull request wanted.

@icambron icambron added the Bug label Jun 10, 2017
@mathcavalieri
Copy link

@kshitijb from the docs, it seems

As of 2.13.0, mixed negative and positive signs are supported when parsing durations.

is referring to the ISO 8601 format (you're using the ASP.NET time spans).

As for the solution of this bug, it seems like patching the aspNetRegex from src/lib/duration/create.js from

var aspNetRegex = /^(\-)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)(\.\d*)?)?$/;

to

var aspNetRegex = /^(\-|\+)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)(\.\d*)?)?$/;

solves it, but I don't know enough about this to be sure.

@kshitijb
Copy link
Contributor Author

@mathcavalieri Actually it doesn't. + has to be handled like - sign. Otherwise, it is ignored.

@icambron I have created a PR - #4007

PS - I have tested it locally and it is giving the expected results.

@ashsearle
Copy link
Contributor

PR #4007 has been merged and fixed this; this issue can be closed.

@icambron icambron closed this as completed Dec 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants