github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/vhdTimeStamp.go (about)

     1  package vhdcore
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  )
     7  
     8  // TimeStamp represents the the creation time of a hard disk image. This is the number
     9  // of seconds since vhd base time which is January 1, 2000 12:00:00 AM in UTC/GMT.
    10  // The disk creation time is stored in the vhd footer in big-endian format.
    11  //
    12  type TimeStamp struct {
    13  	TotalSeconds uint32
    14  }
    15  
    16  // NewVhdTimeStamp creates new VhdTimeStamp with creation time as dateTime. This function
    17  // will panic if the given datetime is before the vhd base time.
    18  //
    19  func NewVhdTimeStamp(dateTime *time.Time) *TimeStamp {
    20  	vhdBaseTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
    21  	if !dateTime.After(vhdBaseTime) {
    22  		log.Panicf("DateTime must be after Base Vhd Time: %v", dateTime)
    23  	}
    24  
    25  	return &TimeStamp{
    26  		TotalSeconds: uint32(dateTime.Sub(vhdBaseTime).Seconds()),
    27  	}
    28  }
    29  
    30  // NewVhdTimeStampFromSeconds creates new VhdTimeStamp, creation time is calculated by adding
    31  // given total seconds with the vhd base time.
    32  //
    33  func NewVhdTimeStampFromSeconds(totalSeconds uint32) *TimeStamp {
    34  	return &TimeStamp{TotalSeconds: totalSeconds}
    35  }
    36  
    37  // ToDateTime returns the time.Time representation of this instance.
    38  //
    39  func (v *TimeStamp) ToDateTime() time.Time {
    40  	vhdBaseTime := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
    41  	return vhdBaseTime.Add(time.Duration(v.TotalSeconds) * time.Second)
    42  }