add trend analysis api

This commit is contained in:
MaysWind
2024-05-20 00:01:40 +08:00
parent 72619f3dad
commit 0884af038d
7 changed files with 267 additions and 24 deletions
+34
View File
@@ -17,6 +17,29 @@ const (
easternmostTimezoneUtcOffset = 840 // Pacific/Kiritimati (UTC+14:00)
)
// ParseNumericYearMonth returns numeric year and month from textual content
func ParseNumericYearMonth(yearMonth string) (int32, int32, error) {
yearMonthParts := strings.Split(yearMonth, "-")
if len(yearMonthParts) != 2 {
return 0, 0, errs.ErrParameterInvalid
}
year, err := StringToInt32(yearMonthParts[0])
if err != nil {
return 0, 0, err
}
month, err := StringToInt32(yearMonthParts[1])
if err != nil {
return 0, 0, err
}
return year, month, nil
}
// FormatUnixTimeToLongDateTimeInServerTimezone returns a textual representation of the unix time formatted by long date time format
func FormatUnixTimeToLongDateTimeInServerTimezone(unixTime int64) string {
return parseFromUnixTime(unixTime).Format(longDateTimeFormat)
@@ -44,6 +67,17 @@ func FormatUnixTimeToYearMonth(unixTime int64, timezone *time.Location) string {
return t.Format(yearMonthDateTimeFormat)
}
// FormatUnixTimeToNumericYearMonth returns numeric year and month of specified unix time
func FormatUnixTimeToNumericYearMonth(unixTime int64, timezone *time.Location) int32 {
t := parseFromUnixTime(unixTime)
if timezone != nil {
t = t.In(timezone)
}
return int32(t.Year())*100 + int32(t.Month())
}
// 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)
+23
View File
@@ -7,6 +7,15 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParseNumericYearMonth(t *testing.T) {
expectedYear := int32(2024)
expectedMonth := int32(3)
actualYear, actualMonth, err := ParseNumericYearMonth("2024-03")
assert.Equal(t, nil, err)
assert.Equal(t, expectedYear, actualYear)
assert.Equal(t, expectedMonth, actualMonth)
}
func TestFormatUnixTimeToLongDateTimeWithoutSecond(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
@@ -35,6 +44,20 @@ func TestFormatUnixTimeToYearMonth(t *testing.T) {
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToNumericYearMonth(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC
utc8Timezone := time.FixedZone("Test Timezone", 28800) // UTC+8
expectedValue := int32(202103)
actualValue := FormatUnixTimeToNumericYearMonth(unixTime, utcTimezone)
assert.Equal(t, expectedValue, actualValue)
expectedValue = int32(202104)
actualValue = FormatUnixTimeToNumericYearMonth(unixTime, utc8Timezone)
assert.Equal(t, expectedValue, actualValue)
}
func TestFormatUnixTimeToNumericLocalDateTime(t *testing.T) {
unixTime := int64(1617228083)
utcTimezone := time.FixedZone("Test Timezone", 0) // UTC