github.com/cloudposse/helm@v2.2.3+incompatible/pkg/timeconv/timeconv.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  
    17  package timeconv
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/golang/protobuf/ptypes/timestamp"
    23  )
    24  
    25  // Now creates a timestamp.Timestamp representing the current time.
    26  func Now() *timestamp.Timestamp {
    27  	return Timestamp(time.Now())
    28  }
    29  
    30  // Timestamp converts a time.Time to a protobuf *timestamp.Timestamp.
    31  func Timestamp(t time.Time) *timestamp.Timestamp {
    32  	return &timestamp.Timestamp{
    33  		Seconds: t.Unix(),
    34  		Nanos:   int32(t.Nanosecond()),
    35  	}
    36  }
    37  
    38  // Time converts a protobuf *timestamp.Timestamp to a time.Time.
    39  func Time(ts *timestamp.Timestamp) time.Time {
    40  	return time.Unix(ts.Seconds, int64(ts.Nanos))
    41  }
    42  
    43  // Format formats a *timestamp.Timestamp into a string.
    44  //
    45  // This follows the rules for time.Time.Format().
    46  func Format(ts *timestamp.Timestamp, layout string) string {
    47  	return Time(ts).Format(layout)
    48  }
    49  
    50  // String formats the timestamp into a user-friendly string.
    51  //
    52  // Currently, this uses the 'time.ANSIC' format string, but there is no guarantee
    53  // that this will not change.
    54  //
    55  // This is a convenience function for formatting timestamps for user display.
    56  func String(ts *timestamp.Timestamp) string {
    57  	return Format(ts, time.ANSIC)
    58  }