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 ( |
| 19 | "github.com/golang/protobuf/ptypes" |
| 20 | timestamppb "github.com/golang/protobuf/ptypes/timestamp" |
| 21 | "time" |
| 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 | } |
| 29 | ts, err := ptypes.Timestamp(tsproto) |
| 30 | if err != nil { |
| 31 | return "", err |
| 32 | } |
| 33 | return ts.Truncate(time.Second).Format(time.RFC3339), nil |
| 34 | } |
| 35 | |
| 36 | // Computes the age of a timestamp and returns it in HMS format |
| 37 | func formatSince(tsproto *timestamppb.Timestamp) (string, error) { |
| 38 | if tsproto == nil { |
| 39 | return "", nil |
| 40 | } |
| 41 | ts, err := ptypes.Timestamp(tsproto) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | return time.Since(ts).Truncate(time.Second).String(), nil |
| 46 | } |