github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/timestamp.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekatime
     7  
     8  import (
     9  	"time"
    10  )
    11  
    12  type (
    13  	// Timestamp is just unix timestamp type (stores a seconds that has been passed
    14  	// since 01 January 1970 00:00:00 (12:00:00 AM)).
    15  	Timestamp int64
    16  
    17  	// TimestampPair is just two some unix timestamps.
    18  	// Used in those methods that returns 2 timestamps.
    19  	TimestampPair [2]Timestamp
    20  )
    21  
    22  // Cmp compares current timestamp and provided one.
    23  // Returns -1 if current < another, 0 if they're equal, 1 if current > another.
    24  func (ts Timestamp) Cmp(anotherTimestamp Timestamp) int {
    25  	if ts < anotherTimestamp {
    26  		return -1
    27  	} else if ts > anotherTimestamp {
    28  		return 1
    29  	} else {
    30  		return 0
    31  	}
    32  }
    33  
    34  // I64 returns int64 representation of the current Timestamp 'ts.
    35  func (ts Timestamp) I64() int64 {
    36  	return int64(ts)
    37  }
    38  
    39  // Std returns standard Golang's time.Time object with the same values
    40  // as current Timestamp have.
    41  func (ts Timestamp) Std() time.Time {
    42  	return time.Unix(ts.I64(), 0).UTC()
    43  }
    44  
    45  // Split splits the current TimestampPair 'tsp' into two separate Timestamps.
    46  func (tsp TimestampPair) Split() (Timestamp, Timestamp) {
    47  	return tsp[0], tsp[1]
    48  }
    49  
    50  // I64 returns two int64 values which are int64 representation of each value
    51  // in the current TimestampPair 'tsp'.
    52  func (tsp TimestampPair) I64() (int64, int64) {
    53  	return int64(tsp[0]), int64(tsp[1])
    54  }
    55  
    56  // Date returns the Date object, the current Timestamp includes which.
    57  // Returns 0/0/0 Date if Timestamp is 0 (01 Jan 1970 00:00:00).
    58  func (ts Timestamp) Date() Date {
    59  	if ts == 0 {
    60  		return 0
    61  	}
    62  	return NewDate(dateFromUnix(ts)) | ts.Weekday().asPartOfDate()
    63  }
    64  
    65  // Time returns the Time object, the current Timestamp includes which.
    66  func (ts Timestamp) Time() Time {
    67  	if ts == 0 {
    68  		return 0
    69  	}
    70  	return NewTime(timeFromUnix(ts))
    71  }
    72  
    73  // Year returns the year number, the current Timestamp includes which.
    74  //
    75  // If you need also at least one more Date's parameter (Month or Day),
    76  // avoid to call many methods explicitly. Call Date() instead (and Split() then).
    77  func (ts Timestamp) Year() Year {
    78  	y, _, _ := dateFromUnix(ts)
    79  	return y
    80  }
    81  
    82  // Month returns the month number, the current Timestamp includes which.
    83  //
    84  // If you need also at least one more Date's parameter (Year or Day),
    85  // avoid to call many methods explicitly. Call Date() instead (and Split() then).
    86  func (ts Timestamp) Month() Month {
    87  	_, m, _ := dateFromUnix(ts)
    88  	return m
    89  }
    90  
    91  // Day returns the day number, the current Timestamp includes which.
    92  //
    93  // If you need also at least one more Date's parameter (Month or Year),
    94  // avoid to call many methods explicitly. Call Date() instead (and Split() then).
    95  func (ts Timestamp) Day() Day {
    96  	_, _, d := dateFromUnix(ts)
    97  	return d
    98  }
    99  
   100  // Hour returns the hour number, the current Timestamp includes which.
   101  //
   102  // If you need also at least one more Time's parameter (Minute or Second),
   103  // avoid to call many methods explicitly. Call Time() instead (and Split() then).
   104  func (ts Timestamp) Hour() Hour {
   105  	h, _, _ := timeFromUnix(ts)
   106  	return h
   107  }
   108  
   109  // Minute returns the minute number, the current Timestamp includes which.
   110  //
   111  // If you need also at least one more Time's parameter (Hour or Second),
   112  // avoid to call many methods explicitly. Call Time() instead (and Split() then).
   113  func (ts Timestamp) Minute() Minute {
   114  	_, m, _ := timeFromUnix(ts)
   115  	return m
   116  }
   117  
   118  // Second returns the second number, the current Timestamp includes which.
   119  //
   120  // If you need also at least one more Time's parameter (Minute or Hour),
   121  // avoid to call many methods explicitly. Call Time() instead (and Split() then).
   122  func (ts Timestamp) Second() Second {
   123  	_, _, s := timeFromUnix(ts)
   124  	return s
   125  }
   126  
   127  // Split returns the Date and Time, the current Timestamp includes which.
   128  // It's just like a separate Date(), Time() calls.
   129  func (ts Timestamp) Split() (d Date, t Time) {
   130  	return ts.Date(), ts.Time()
   131  }
   132  
   133  // Weekday returns the current Timestamp ts the number of day in week.
   134  func (ts Timestamp) Weekday() Weekday {
   135  	return Weekday(((ts + SECONDS_IN_DAY) % SECONDS_IN_WEEK) / SECONDS_IN_DAY)
   136  }
   137  
   138  // NewTimestampNow is just the same as time.Now().
   139  func NewTimestampNow() Timestamp {
   140  	return NewTimestampFromStd(time.Now())
   141  }
   142  
   143  // NewTimestamp creates and returns Timestamp object from the presented Date 'd'
   144  // and Time 't'.
   145  //
   146  // WARNING!
   147  // It's 0 value Timestamp if you use 01 Jan 1970 00:00:00,
   148  // and Timestamp.MarshalJSON() will return JSON null for that value,
   149  // and Timestamp.Date() will return 0/0/0 Date, NOT 01 Jan 1970!
   150  // To avoid it, use 00:00:01 as Time.
   151  func NewTimestamp(y Year, m Month, d Day, hh Hour, mm Minute, ss Second) Timestamp {
   152  	if y > 4095 {
   153  		y = 4095
   154  	}
   155  	tt := time.Date(int(y), time.Month(m), int(d), int(hh), int(mm), int(ss), 0, time.UTC)
   156  	return Timestamp(tt.Unix())
   157  }
   158  
   159  // NewTimestampFromStd creates and returns Timestamp object from the standard Golang's
   160  // time.Time object (UTC time).
   161  func NewTimestampFromStd(t time.Time) Timestamp {
   162  	return Timestamp(t.Unix())
   163  }
   164  
   165  // BeginningAndEndOf returns the [A,B] pair, and A <= 'ts' <= B, and A-B == 'range_'.
   166  // There is a special formula that allows to get start and ending of 'ts'
   167  // related: day, month, year.
   168  //
   169  // In most cases you don't need to use this method, but use any of predefined
   170  // instead: BeginningOfDay(), BeginningAndEndOfMonth(), etc.
   171  func (ts Timestamp) BeginningAndEndOf(range_ Timestamp) TimestampPair {
   172  	x := ts + (range_ - ts%range_)
   173  	return TimestampPair{x - range_, x - 1}
   174  }
   175  
   176  // BeginningOfDay returns the day beginning of the current timestamp 'ts'.
   177  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 12/11/2019 00:00:00 (12:00:00 AM).
   178  func (ts Timestamp) BeginningOfDay() Timestamp {
   179  	return ts.BeginningAndEndOf(SECONDS_IN_DAY)[0]
   180  }
   181  
   182  // EndOfDay returns the day ending of the current timestamp 'ts'.
   183  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 12/11/2019 23:59:59 (11:59:59 PM).
   184  func (ts Timestamp) EndOfDay() Timestamp {
   185  	return ts.BeginningAndEndOf(SECONDS_IN_DAY)[1]
   186  }
   187  
   188  // BeginningAndEndOfDay is the same as BeginningOfDay() and EndOfDay() calls.
   189  func (ts Timestamp) BeginningAndEndOfDay() TimestampPair {
   190  	return ts.BeginningAndEndOf(SECONDS_IN_DAY)
   191  }
   192  
   193  // BeginningOfMonth returns the month beginning of the current timestamp 'ts'.
   194  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 1/11/2019 00:00:00 (12:00:00 AM).
   195  func (ts Timestamp) BeginningOfMonth() Timestamp {
   196  	y, m, _ := dateFromUnix(ts)
   197  	return ts.BeginningAndEndOf(InMonth(y, m))[0]
   198  }
   199  
   200  // EndOfMonth returns the month ending of the current timestamp 'ts'.
   201  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 30/11/2019 23:59:59 (11:59:59 PM).
   202  func (ts Timestamp) EndOfMonth() Timestamp {
   203  	y, m, _ := dateFromUnix(ts)
   204  	return ts.BeginningAndEndOf(InMonth(y, m))[1]
   205  }
   206  
   207  // BeginningAndEndOfMonth is the same as BeginningOfMonth() and EndOfMonth() calls.
   208  func (ts Timestamp) BeginningAndEndOfMonth() TimestampPair {
   209  	y, m, _ := dateFromUnix(ts)
   210  	return ts.BeginningAndEndOf(InMonth(y, m))
   211  }
   212  
   213  // BeginningOfYear returns the year beginning of the current timestamp 'ts'.
   214  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 1/1/2019 00:00:00 (12:00:00 AM).
   215  func (ts Timestamp) BeginningOfYear() Timestamp {
   216  	return ts.BeginningAndEndOf(InYear(ts.Year()))[0]
   217  }
   218  
   219  // EndOfYear returns the year ending of the current timestamp 'ts'.
   220  // E.g: 12/11/2019, 15:46:40 (3:46:40 PM) -> 31/12/2019 23:59:59 (11:59:59 PM).
   221  func (ts Timestamp) EndOfYear() Timestamp {
   222  	return ts.BeginningAndEndOf(InYear(ts.Year()))[1]
   223  }
   224  
   225  // BeginningAndEndOfYear is the same as BeginningOfYear() and EndOfYear() calls.
   226  func (ts Timestamp) BeginningAndEndOfYear() TimestampPair {
   227  	return ts.BeginningAndEndOf(InYear(ts.Year()))
   228  }