Home | History | Annotate | Download | only in js
      1 /**
      2  * Copyright (c) 2017 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you
      5  * may not use this file except in compliance with the License. You may
      6  * obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
     13  * implied. See the License for the specific language governing
     14  * permissions and limitations under the License.
     15  */
     16 
     17 (function (moment) {
     18 
     19   /**
     20    * Renders a timestamp in the user timezone.
     21    * @param timestamp The long timestamp to render (in microseconds).
     22    * @param showTimezone True if the timezone should be rendered, false otherwise.
     23    * @returns the string-formatted version of the provided timestamp.
     24    */
     25   moment.prototype.renderTime = function (timestamp, showTimezone) {
     26     var time = moment(timestamp / 1000);
     27     var format = 'YYYY-M-DD H:mm:ss';
     28     if (!!showTimezone) {
     29         format = format + 'ZZ';
     30     }
     31     return time.format(format);
     32   }
     33 
     34   /**
     35    * Renders a date in the user timezone.
     36    * @param timestamp The long timestamp to render (in microseconds).
     37    * @param showTimezone True if the timezone should be rendered, false otherwise.
     38    * @returns the string-formatted version of the provided timestamp.
     39    */
     40   moment.prototype.renderDate = function (timestamp, showTimezone) {
     41     var time = moment(timestamp / 1000);
     42     var format = 'YYYY-M-DD';
     43     if (!!showTimezone) {
     44         format = format + 'ZZ';
     45     }
     46     return time.format(format);
     47   }
     48 
     49   /**
     50    * Renders a duration in the user timezone.
     51    * @param durationTimestamp The long duration to render (in microseconds).
     52    * @returns the string-formatted duration of the provided duration timestamp.
     53    */
     54   moment.prototype.renderDuration = function (durationTimestamp) {
     55     var fmt = 's[s]';
     56     var duration = moment.utc(durationTimestamp / 1000);
     57     if (duration.hours() > 0) {
     58       fmt = 'H[h], m[m], ' + fmt;
     59     } else if (duration.minutes() > 0) {
     60       fmt = 'm[m], ' + fmt;
     61     }
     62     return duration.format(fmt);
     63   }
     64 
     65 })(moment);
     66