github.com/goravel/framework@v1.13.9/support/carbon/carbon.go (about)

     1  package carbon
     2  
     3  import (
     4  	stdtime "time"
     5  
     6  	"github.com/golang-module/carbon/v2"
     7  )
     8  
     9  type Clock struct {
    10  	testNow  bool
    11  	testTime Carbon
    12  	timezone string
    13  }
    14  
    15  var clock = &Clock{}
    16  
    17  // SetTestNow Set the test time. Remember to unset after used.
    18  func SetTestNow(testTime Carbon) {
    19  	clock.testNow = true
    20  	clock.testTime = testTime
    21  }
    22  
    23  // UnsetTestNow Unset the test time.
    24  func UnsetTestNow() {
    25  	clock.testNow = false
    26  }
    27  
    28  // IsTestNow Determine if the test now time is set.
    29  func IsTestNow() bool {
    30  	return clock.testNow
    31  }
    32  
    33  // SetTimezone sets timezone.
    34  // 设置时区
    35  func SetTimezone(timezone string) {
    36  	clock.timezone = timezone
    37  }
    38  
    39  // Now return a Carbon object of now.
    40  func Now(timezone ...string) Carbon {
    41  	if IsTestNow() {
    42  		return clock.testTime
    43  	}
    44  
    45  	return carbon.Now(getTimezone(timezone))
    46  }
    47  
    48  // Parse return a Carbon object of given value.
    49  func Parse(value string, timezone ...string) Carbon {
    50  	return carbon.Parse(value, getTimezone(timezone))
    51  }
    52  
    53  // ParseByFormat return a Carbon object of given value and format.
    54  func ParseByFormat(value, format string, timezone ...string) Carbon {
    55  	return carbon.ParseByFormat(value, format, getTimezone(timezone))
    56  }
    57  
    58  // ParseByLayout return a Carbon object of given value and layout.
    59  func ParseByLayout(value, layout string, timezone ...string) Carbon {
    60  	return carbon.ParseByLayout(value, layout, getTimezone(timezone))
    61  }
    62  
    63  // FromTimestamp return a Carbon object of given timestamp.
    64  func FromTimestamp(timestamp int64, timezone ...string) Carbon {
    65  	return carbon.CreateFromTimestamp(timestamp, getTimezone(timezone))
    66  }
    67  
    68  // FromTimestampMilli return a Carbon object of given millisecond timestamp.
    69  func FromTimestampMilli(timestamp int64, timezone ...string) Carbon {
    70  	return carbon.CreateFromTimestampMilli(timestamp, getTimezone(timezone))
    71  }
    72  
    73  // FromTimestampMicro return a Carbon object of given microsecond timestamp.
    74  func FromTimestampMicro(timestamp int64, timezone ...string) Carbon {
    75  	return carbon.CreateFromTimestampMicro(timestamp, getTimezone(timezone))
    76  }
    77  
    78  // FromTimestampNano return a Carbon object of given nanosecond timestamp.
    79  func FromTimestampNano(timestamp int64, timezone ...string) Carbon {
    80  	return carbon.CreateFromTimestampNano(timestamp, getTimezone(timezone))
    81  }
    82  
    83  // FromDateTime return a Carbon object of given date and time.
    84  func FromDateTime(year int, month int, day int, hour int, minute int, second int, timezone ...string) Carbon {
    85  	return carbon.CreateFromDateTime(year, month, day, hour, minute, second, getTimezone(timezone))
    86  }
    87  
    88  // FromDateTimeMilli return a Carbon object of given date and millisecond time.
    89  func FromDateTimeMilli(year int, month int, day int, hour int, minute int, second int, millisecond int, timezone ...string) Carbon {
    90  	return carbon.CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond, getTimezone(timezone))
    91  }
    92  
    93  // FromDateTimeMicro return a Carbon object of given date and microsecond time.
    94  func FromDateTimeMicro(year int, month int, day int, hour int, minute int, second int, microsecond int, timezone ...string) Carbon {
    95  	return carbon.CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond, getTimezone(timezone))
    96  }
    97  
    98  // FromDateTimeNano return a Carbon object of given date and nanosecond time.
    99  func FromDateTimeNano(year int, month int, day int, hour int, minute int, second int, nanosecond int, timezone ...string) Carbon {
   100  	return carbon.CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond, getTimezone(timezone))
   101  }
   102  
   103  // FromDate return a Carbon object of given date.
   104  func FromDate(year int, month int, day int, timezone ...string) Carbon {
   105  	return carbon.CreateFromDate(year, month, day, getTimezone(timezone))
   106  }
   107  
   108  // FromDateMilli return a Carbon object of given millisecond date.
   109  func FromDateMilli(year int, month int, day int, millisecond int, timezone ...string) Carbon {
   110  	return carbon.CreateFromDateMilli(year, month, day, millisecond, getTimezone(timezone))
   111  }
   112  
   113  // FromDateMicro return a Carbon object of given microsecond date.
   114  func FromDateMicro(year int, month int, day int, microsecond int, timezone ...string) Carbon {
   115  	return carbon.CreateFromDateMicro(year, month, day, microsecond, getTimezone(timezone))
   116  }
   117  
   118  // FromDateNano return a Carbon object of given nanosecond date.
   119  func FromDateNano(year int, month int, day int, nanosecond int, timezone ...string) Carbon {
   120  	return carbon.CreateFromDateNano(year, month, day, nanosecond, getTimezone(timezone))
   121  }
   122  
   123  // FromTime return a Carbon object of given time.
   124  func FromTime(hour int, minute int, second int, timezone ...string) Carbon {
   125  	return carbon.CreateFromTime(hour, minute, second, getTimezone(timezone))
   126  }
   127  
   128  // FromTimeMilli return a Carbon object of given millisecond time.
   129  func FromTimeMilli(hour int, minute int, second int, millisecond int, timezone ...string) Carbon {
   130  	return carbon.CreateFromTimeMilli(hour, minute, second, millisecond, getTimezone(timezone))
   131  }
   132  
   133  // FromTimeMicro return a Carbon object of given microsecond time.
   134  func FromTimeMicro(hour int, minute int, second int, microsecond int, timezone ...string) Carbon {
   135  	return carbon.CreateFromTimeMicro(hour, minute, second, microsecond, getTimezone(timezone))
   136  }
   137  
   138  // FromTimeNano return a Carbon object of given nanosecond time.
   139  func FromTimeNano(hour int, minute int, second int, nanosecond int, timezone ...string) Carbon {
   140  	return carbon.CreateFromTimeNano(hour, minute, second, nanosecond, getTimezone(timezone))
   141  }
   142  
   143  // FromStdTime return a Carbon object of given time.Time object.
   144  func FromStdTime(time stdtime.Time) Carbon {
   145  	return carbon.CreateFromStdTime(time)
   146  }
   147  
   148  func getTimezone(timezone []string) string {
   149  	if len(timezone) == 0 {
   150  		if clock.timezone == "" {
   151  			return UTC
   152  		} else {
   153  			return clock.timezone
   154  		}
   155  	}
   156  
   157  	return timezone[0]
   158  }