support setting timezone type for the time range of statistical data
This commit is contained in:
@@ -44,6 +44,34 @@ func FormatUnixTimeToYearMonth(unixTime int64, timezone *time.Location) string {
|
||||
return t.Format(yearMonthDateTimeFormat)
|
||||
}
|
||||
|
||||
// FormatUnixTimeToNumericLocalDateTime returns numeric year, month, day, hour, minute and second of specified unix time
|
||||
func FormatUnixTimeToNumericLocalDateTime(unixTime int64, timezone *time.Location) int64 {
|
||||
t := parseFromUnixTime(unixTime)
|
||||
|
||||
if timezone != nil {
|
||||
t = t.In(timezone)
|
||||
}
|
||||
|
||||
localDateTime := int64(t.Year())
|
||||
localDateTime = localDateTime*100 + int64(t.Month())
|
||||
localDateTime = localDateTime*100 + int64(t.Day())
|
||||
localDateTime = localDateTime*100 + int64(t.Hour())
|
||||
localDateTime = localDateTime*100 + int64(t.Minute())
|
||||
localDateTime = localDateTime*100 + int64(t.Second())
|
||||
|
||||
return localDateTime
|
||||
}
|
||||
|
||||
// GetMinUnixTimeWithSameLocalDateTime returns the minimum UnixTime for date with the same local date
|
||||
func GetMinUnixTimeWithSameLocalDateTime(unixTime int64, currentUtcOffset int16) int64 {
|
||||
return unixTime + int64(currentUtcOffset)*60 - easternmostTimezoneUtcOffset*60
|
||||
}
|
||||
|
||||
// GetMaxUnixTimeWithSameLocalDateTime returns the maximum UnixTime for date with the same local date
|
||||
func GetMaxUnixTimeWithSameLocalDateTime(unixTime int64, currentUtcOffset int16) int64 {
|
||||
return unixTime + int64(currentUtcOffset)*60 - westernmostTimezoneUtcOffset*60
|
||||
}
|
||||
|
||||
// ParseFromLongDateTimeToMinUnixTime parses a formatted string in long date time format to minimal unix time (the westernmost timezone)
|
||||
func ParseFromLongDateTimeToMinUnixTime(t string) (time.Time, error) {
|
||||
timezone := time.FixedZone("Timezone", easternmostTimezoneUtcOffset*60)
|
||||
|
||||
@@ -35,6 +35,38 @@ func TestFormatUnixTimeToYearMonth(t *testing.T) {
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestFormatUnixTimeToNumericLocalDateTime(t *testing.T) {
|
||||
unixTime := int64(1617228083)
|
||||
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
|
||||
utc8Timezone := time.FixedZone("Test Timezone", 28800) // UTC+8
|
||||
|
||||
expectedValue := int64(20210331220123)
|
||||
actualValue := FormatUnixTimeToNumericLocalDateTime(unixTime, utcTimezone)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = int64(20210401060123)
|
||||
actualValue = FormatUnixTimeToNumericLocalDateTime(unixTime, utc8Timezone)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestGetMinUnixTimeWithSameLocalDateTime(t *testing.T) {
|
||||
expectedValue := int64(1690797600)
|
||||
actualValue := GetMinUnixTimeWithSameLocalDateTime(1690819200, 480)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
actualValue = GetMinUnixTimeWithSameLocalDateTime(1690873200, -420)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestGetMaxUnixTimeWithSameLocalDateTime(t *testing.T) {
|
||||
expectedValue := int64(1690891200)
|
||||
actualValue := GetMaxUnixTimeWithSameLocalDateTime(1690819200, 480)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
actualValue = GetMaxUnixTimeWithSameLocalDateTime(1690873200, -420)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestParseFromLongDateTimeToMinUnixTime(t *testing.T) {
|
||||
expectedValue := int64(1690797600)
|
||||
actualTime, err := ParseFromLongDateTimeToMinUnixTime("2023-08-01 00:00:00")
|
||||
|
||||
Reference in New Issue
Block a user