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

Parsing of ISO Dates #6

Open
JayChandler opened this issue Jul 31, 2017 · 0 comments
Open

Parsing of ISO Dates #6

JayChandler opened this issue Jul 31, 2017 · 0 comments

Comments

@JayChandler
Copy link

In the function stringToValue you check for a date string. Currently you can only pass a value in this

For example this works

2007-12-24T18:21:00+00:00

but

2007-12-24T18:21:00Z

does not

function stringToValue(string, parameters){
		var converter = exports.converters["default"];
		if(string.charAt(0) === "$"){
			var param_index = parseInt(string.substring(1), 10) - 1;
			return param_index >= 0 && parameters ? parameters[param_index] : undefined;
		}
		if(string.indexOf(":") > -1){
			var parts = string.split(":");
			if(parts.length == 2) {
				converter = exports.converters[parts[0]];
				string = parts[1];
			}
			if(!converter){
				throw new URIError("Unknown converter " + parts[0]);
			}
		}
		if(/^\d{4}-[0-1]\d-[0-3][0-9](T([0-2]\d:){2}[0-2]\d([-+][0-2]\d:[0-2]\d)*)*$/.test(string))
			converter = exports.converters["date"];

		return converter(string);
	}

But then in the date converter you check for the ISO date with the Z notation ? How does that match ?

date: function(x){
			var isoDate = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,3}))?Z$/.exec(x);
			var date;
			if (isoDate) {
				date = new Date(Date.UTC(+isoDate[1], +isoDate[2] - 1, +isoDate[3], +isoDate[4], +isoDate[5], +isoDate[6], +isoDate[7] || 0));
			}else{
				date = new Date(x);
			}
			if (isNaN(date.getTime())){
				throw new URIError("Invalid date " + x);
			}
			return date;
		}

Please correct that. It should be possible to pass a correct ISO date string.

This is the right regex

^(?:[1-9]\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:Z|[+-][01]\d:[0-5]\d)$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant