blob: 41be2f44b34abb25cdb763e25674c28766ad3370 [file] [log] [blame]
Scott Baker9173ed82020-05-19 08:30:12 -07001/*
2 * Copyright 2019-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may 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 implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package format
17
18import (
Scott Baker9173ed82020-05-19 08:30:12 -070019 "time"
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000020
21 timestamppb "github.com/golang/protobuf/ptypes/timestamp"
Scott Baker9173ed82020-05-19 08:30:12 -070022)
23
24// formats a Timestamp proto as a RFC3339 date string
25func formatTimestamp(tsproto *timestamppb.Timestamp) (string, error) {
26 if tsproto == nil {
27 return "", nil
28 }
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000029 return tsproto.AsTime().Truncate(time.Second).Format(time.RFC3339), nil
30}
31
32// Computes the age of a timestamp and returns it in HMS format
33func formatGoSince(ts time.Time) (string, error) {
34 return time.Since(ts).Truncate(time.Second).String(), nil
Scott Baker9173ed82020-05-19 08:30:12 -070035}
36
37// Computes the age of a timestamp and returns it in HMS format
38func formatSince(tsproto *timestamppb.Timestamp) (string, error) {
39 if tsproto == nil {
40 return "", nil
41 }
David K. Bainbridgebd6b2882021-08-26 13:31:02 +000042 return time.Since(tsproto.AsTime()).Truncate(time.Second).String(), nil
Scott Baker9173ed82020-05-19 08:30:12 -070043}