github.com/fraugster/parquet-go@v0.12.0/floor/time.go (about)

     1  package floor
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  )
     8  
     9  // Time represents a time, with no date information.
    10  type Time struct {
    11  	nsec        int64
    12  	utcAdjusted bool
    13  }
    14  
    15  const (
    16  	nsPerSecond      = 1000000000
    17  	hoursPerDay      = 24
    18  	secondsPerMinute = 60
    19  	minutesPerHour   = 60
    20  	secondsPerHour   = secondsPerMinute * minutesPerHour
    21  	nsPerMillisecond = 1000000
    22  	nsPerMicrosecond = 1000
    23  )
    24  
    25  // NewTime creates a new time object.
    26  func NewTime(hour, min, sec, nanosec int) (Time, error) {
    27  	if hour < 0 || hour >= hoursPerDay {
    28  		return Time{}, errors.New("invalid hour (must be between 0 and 23)")
    29  	}
    30  	if min < 0 || min >= minutesPerHour {
    31  		return Time{}, errors.New("invalid minute (must be between 0 and 59)")
    32  	}
    33  	if sec < 0 || sec >= secondsPerMinute {
    34  		return Time{}, errors.New("invalid second (must be between 0 and 59)")
    35  	}
    36  	if nanosec < 0 || nanosec >= nsPerSecond {
    37  		return Time{}, fmt.Errorf("invalid nanosecond (must be between 0 and %d)", nsPerSecond-1)
    38  	}
    39  
    40  	total := int64((hour*secondsPerHour+min*secondsPerMinute+sec)*nsPerSecond + nanosec)
    41  
    42  	return Time{
    43  		nsec: total,
    44  	}, nil
    45  }
    46  
    47  // Hour returns the hour of the time value.
    48  func (t Time) Hour() int {
    49  	return int(t.nsec / (secondsPerHour * nsPerSecond))
    50  }
    51  
    52  // Minute returns the minute of the time value.
    53  func (t Time) Minute() int {
    54  	return int((t.nsec / (secondsPerMinute * nsPerSecond)) % minutesPerHour)
    55  }
    56  
    57  // Second returns the second of the time value.
    58  func (t Time) Second() int {
    59  	return int((t.nsec / nsPerSecond) % secondsPerMinute)
    60  }
    61  
    62  // Nanosecond returns the sub-second nanosecond value of the time value.
    63  func (t Time) Nanosecond() int {
    64  	return int(t.nsec % nsPerSecond)
    65  }
    66  
    67  // Millisecond returns the sub-second millisecond value of the time value
    68  func (t Time) Millisecond() int {
    69  	return t.Nanosecond() / nsPerMillisecond
    70  }
    71  
    72  // Microsecond returns the sub-second microsend value of the time value
    73  func (t Time) Microsecond() int {
    74  	return t.Nanosecond() / nsPerMicrosecond
    75  }
    76  
    77  // Nanoseconds returns the total number of nanoseconds in the day.
    78  func (t Time) Nanoseconds() int64 {
    79  	return t.nsec
    80  }
    81  
    82  // Milliseconds returns the total number of milliseconds in the day.
    83  func (t Time) Milliseconds() int32 {
    84  	return int32(t.nsec / nsPerMillisecond)
    85  }
    86  
    87  // Microseconds returns the total number of microseconds in the day.
    88  func (t Time) Microseconds() int64 {
    89  	return t.nsec / nsPerMicrosecond
    90  }
    91  
    92  // TimeFromNanoseconds returns a new Time object based on the provided nanoseconds.
    93  func TimeFromNanoseconds(ns int64) Time {
    94  	return Time{nsec: ns}
    95  }
    96  
    97  // TimeFromMicroseconds returns a new Time object based on the provided microseconds.
    98  func TimeFromMicroseconds(micros int64) Time {
    99  	return Time{nsec: micros * nsPerMicrosecond}
   100  }
   101  
   102  // TimeFromMilliseconds returns a new Time object based on the provided milliseconds.
   103  func TimeFromMilliseconds(ms int32) Time {
   104  	return Time{nsec: int64(ms) * nsPerMillisecond}
   105  }
   106  
   107  func (t Time) String() string {
   108  	str := fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
   109  
   110  	if nsec := t.Nanosecond(); nsec != 0 {
   111  		str += fmt.Sprintf(".%09d", nsec)
   112  	}
   113  
   114  	return str
   115  }
   116  
   117  // UTC returns the same Time, but marked as UTC-adjusted.
   118  func (t Time) UTC() Time {
   119  	return Time{
   120  		nsec:        t.nsec,
   121  		utcAdjusted: true,
   122  	}
   123  }
   124  
   125  // Today returns a time.Time, combining the current date with the provided time.
   126  func (t Time) Today() time.Time {
   127  	return t.OnThatDay(time.Now())
   128  }
   129  
   130  // OnThatDay returns a time.Time, combining the provided date with the time of this object.
   131  func (t Time) OnThatDay(day time.Time) time.Time {
   132  	loc := time.Local
   133  	if t.utcAdjusted {
   134  		loc = time.UTC
   135  	}
   136  
   137  	return time.Date(day.Year(), day.Month(), day.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc)
   138  }
   139  
   140  // MustTime panics if err is not nil, otherwise it returns t.
   141  func MustTime(t Time, err error) Time {
   142  	if err != nil {
   143  		panic(err)
   144  	}
   145  	return t
   146  }