Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package format |
| 17 | |
| 18 | import ( |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 19 | "time" |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 20 | |
| 21 | timestamppb "github.com/golang/protobuf/ptypes/timestamp" |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // formats a Timestamp proto as a RFC3339 date string |
| 25 | func formatTimestamp(tsproto *timestamppb.Timestamp) (string, error) { |
| 26 | if tsproto == nil { |
| 27 | return "", nil |
| 28 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 29 | 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 |
| 33 | func formatGoSince(ts time.Time) (string, error) { |
| 34 | return time.Since(ts).Truncate(time.Second).String(), nil |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Computes the age of a timestamp and returns it in HMS format |
| 38 | func formatSince(tsproto *timestamppb.Timestamp) (string, error) { |
| 39 | if tsproto == nil { |
| 40 | return "", nil |
| 41 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 42 | return time.Since(tsproto.AsTime()).Truncate(time.Second).String(), nil |
Scott Baker | 9173ed8 | 2020-05-19 08:30:12 -0700 | [diff] [blame] | 43 | } |