|
| 1 | +/***************************************************************************** |
| 2 | + * Open MCT Web, Copyright (c) 2014-2015, United States Government |
| 3 | + * as represented by the Administrator of the National Aeronautics and Space |
| 4 | + * Administration. All rights reserved. |
| 5 | + * |
| 6 | + * Open MCT Web is licensed under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * Open MCT Web includes source code licensed under additional open source |
| 18 | + * licenses. See the Open Source Licenses file (LICENSES.md) included with |
| 19 | + * this source code distribution or the Licensing information page available |
| 20 | + * at runtime from the About dialog for additional information. |
| 21 | + *****************************************************************************/ |
| 22 | + |
| 23 | +define([ |
| 24 | + 'moment' |
| 25 | +], function ( |
| 26 | + moment |
| 27 | +) { |
| 28 | + |
| 29 | + var DATE_FORMAT = "YYYY-MM-DD hh:mm:ss.SSS", |
| 30 | + DATE_FORMATS = [ |
| 31 | + DATE_FORMAT, |
| 32 | + "YYYY-MM-DD hh:mm:ss", |
| 33 | + "YYYY-MM-DD hh:mm", |
| 34 | + "YYYY-MM-DD" |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @typedef Scale |
| 39 | + * @property {number} min the minimum scale value, in ms |
| 40 | + * @property {number} max the maximum scale value, in ms |
| 41 | + */ |
| 42 | + |
| 43 | + /** |
| 44 | + * Formatter for UTC timestamps. Interprets numeric values as |
| 45 | + * milliseconds since the start of 1970. |
| 46 | + * |
| 47 | + * @implements {Format} |
| 48 | + * @constructor |
| 49 | + * @memberof platform/commonUI/formats |
| 50 | + */ |
| 51 | + function LocalTimeFormat() { |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns an appropriate time format based on the provided value and |
| 56 | + * the threshold required. |
| 57 | + * @private |
| 58 | + */ |
| 59 | + function getScaledFormat (d) { |
| 60 | + var m = moment.utc(d); |
| 61 | + /** |
| 62 | + * Uses logic from d3 Time-Scales, v3 of the API. See |
| 63 | + * https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Scales.md |
| 64 | + * |
| 65 | + * Licensed |
| 66 | + */ |
| 67 | + return [ |
| 68 | + [".SSS", function(m) { return m.milliseconds(); }], |
| 69 | + [":ss", function(m) { return m.seconds(); }], |
| 70 | + ["hh:mm", function(m) { return m.minutes(); }], |
| 71 | + ["hh", function(m) { return m.hours(); }], |
| 72 | + ["ddd DD", function(m) { |
| 73 | + return m.days() && |
| 74 | + m.date() != 1; |
| 75 | + }], |
| 76 | + ["MMM DD", function(m) { return m.date() != 1; }], |
| 77 | + ["MMMM", function(m) { |
| 78 | + return m.month(); |
| 79 | + }], |
| 80 | + ["YYYY", function() { return true; }] |
| 81 | + ].filter(function (row){ |
| 82 | + return row[1](m); |
| 83 | + })[0][0]; |
| 84 | + }; |
| 85 | + |
| 86 | + /** |
| 87 | + * |
| 88 | + * @param value |
| 89 | + * @param {Scale} [scale] Optionally provides context to the |
| 90 | + * format request, allowing for scale-appropriate formatting. |
| 91 | + * @returns {string} the formatted date |
| 92 | + */ |
| 93 | + LocalTimeFormat.prototype.format = function (value, scale) { |
| 94 | + if (scale !== undefined){ |
| 95 | + var scaledFormat = getScaledFormat(value, scale); |
| 96 | + if (scaledFormat) { |
| 97 | + return moment.utc(value).format(scaledFormat); |
| 98 | + } |
| 99 | + } |
| 100 | + return moment(value).format(DATE_FORMAT); |
| 101 | + }; |
| 102 | + |
| 103 | + LocalTimeFormat.prototype.parse = function (text) { |
| 104 | + return moment(text, DATE_FORMATS).valueOf(); |
| 105 | + }; |
| 106 | + |
| 107 | + LocalTimeFormat.prototype.validate = function (text) { |
| 108 | + return moment(text, DATE_FORMATS).isValid(); |
| 109 | + }; |
| 110 | + |
| 111 | + return LocalTimeFormat; |
| 112 | +}); |
0 commit comments