github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztime/time.go (about)

     1  // Package ztime provides time related operations
     2  package ztime
     3  
     4  import (
     5  	"sync/atomic"
     6  	"time"
     7  )
     8  
     9  var inlay = New()
    10  
    11  // Now format current time
    12  func Now(format ...string) string {
    13  	return inlay.FormatTime(UnixMicro(Clock()), format...)
    14  }
    15  
    16  // Time With the time zone of the time
    17  func Time() time.Time {
    18  	return inlay.In(UnixMicro(Clock()))
    19  }
    20  
    21  // SetTimeZone SetTimeZone
    22  func SetTimeZone(zone int) *TimeEngine {
    23  	return inlay.SetTimeZone(zone)
    24  }
    25  
    26  // GetTimeZone getTimeZone
    27  func GetTimeZone() *time.Location {
    28  	return inlay.GetTimeZone()
    29  }
    30  
    31  // FormatTime format time
    32  func FormatTime(t time.Time, format ...string) string {
    33  	return inlay.FormatTime(t, format...)
    34  }
    35  
    36  // FormatTimestamp format timestamp
    37  func FormatTimestamp(timestamp int64, format ...string) string {
    38  	return inlay.FormatTimestamp(timestamp, format...)
    39  }
    40  
    41  func Week(t time.Time) int {
    42  	return inlay.Week(t)
    43  }
    44  
    45  func MonthRange(year int, month int) (beginTime, endTime int64, err error) {
    46  	return inlay.MonthRange(year, month)
    47  }
    48  
    49  // Parse string to time
    50  func Parse(str string, format ...string) (time.Time, error) {
    51  	return inlay.Parse(str, format...)
    52  }
    53  
    54  // Unix int to time
    55  func Unix(tt int64) time.Time {
    56  	return inlay.Unix(tt)
    57  }
    58  
    59  // UnixMicro int to time
    60  func UnixMicro(tt int64) time.Time {
    61  	return inlay.UnixMicro(tt)
    62  }
    63  
    64  // In time to time
    65  func In(tt time.Time) time.Time {
    66  	return inlay.In(tt)
    67  }
    68  
    69  var clock = time.Now().UnixNano() / 1000
    70  
    71  func init() {
    72  	go func() {
    73  		m := 10 * time.Millisecond
    74  		t := int64(10000)
    75  		ticker := time.NewTicker(m)
    76  		defer ticker.Stop()
    77  		for {
    78  			atomic.StoreInt64(&clock, time.Now().UnixNano()/1000)
    79  			for i := 0; i < 10; i++ {
    80  				<-ticker.C
    81  				atomic.AddInt64(&clock, t)
    82  			}
    83  			<-ticker.C
    84  		}
    85  	}()
    86  }
    87  
    88  // Clock The current microsecond timestamp has an accuracy of 100ms
    89  func Clock() int64 {
    90  	return atomic.LoadInt64(&clock)
    91  }