From b2305937c5ec87416dffd08170ccd229322760a7 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 10 Aug 2015 11:30:06 -0700 Subject: [PATCH] GUAC-1213: Move date/time parsing code to own functions. --- .../form/controllers/dateFieldController.js | 19 ++++++++++++++++++- .../form/controllers/timeFieldController.js | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js index 1fc4eb8e0..af325be93 100644 --- a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js +++ b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js @@ -48,9 +48,26 @@ angular.module('form').controller('dateFieldController', ['$scope', '$injector', }; + /** + * Parses the date components of the given string into a Date with only the + * date components set. The resulting Date will be in the UTC timezone, + * with the time left as midnight. The input string must be in the format + * YYYY-MM-DD (zero-padded). + * + * @param {String} str + * The date string to parse. + * + * @returns {Date} + * A Date object, in the UTC timezone, with only the date components + * set. + */ + var parseDate = function parseDate(str) { + return new Date(str + 'T00:00Z'); + }; + // Update typed value when model is changed $scope.$watch('model', function modelChanged(model) { - $scope.typedValue = (model ? new Date(model + 'T00:00Z') : null); + $scope.typedValue = (model ? parseDate(model) : null); }); // Update string value in model when typed value is changed diff --git a/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js b/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js index 92b48d945..67547d23f 100644 --- a/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js +++ b/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js @@ -48,9 +48,26 @@ angular.module('form').controller('timeFieldController', ['$scope', '$injector', }; + /** + * Parses the time components of the given string into a Date with only the + * time components set. The resulting Date will be in the UTC timezone, + * with the date left as 1970-01-01. The input string must be in the format + * HH:MM:SS (zero-padded, 24-hour). + * + * @param {String} str + * The time string to parse. + * + * @returns {Date} + * A Date object, in the UTC timezone, with only the time components + * set. + */ + var parseTime = function parseTime(str) { + return new Date('1970-01-01T' + str + 'Z'); + }; + // Update typed value when model is changed $scope.$watch('model', function modelChanged(model) { - $scope.typedValue = (model ? new Date('1970-01-01T' + model + 'Z') : null); + $scope.typedValue = (model ? parseTime(model) : null); }); // Update string value in model when typed value is changed