import transaction from firefly iii
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
const (
|
||||
longDateTimeFormat = "2006-01-02 15:04:05"
|
||||
longDateTimeWithTimezoneFormat = "2006-01-02T15:04:05Z07:00"
|
||||
longDateTimeWithoutSecondFormat = "2006-01-02 15:04"
|
||||
shortDateTimeFormat = "2006-1-2 15:4:5"
|
||||
yearMonthDateTimeFormat = "2006-01"
|
||||
@@ -135,6 +136,11 @@ func ParseFromLongDateTime(t string, utcOffset int16) (time.Time, error) {
|
||||
return time.ParseInLocation(longDateTimeFormat, t, timezone)
|
||||
}
|
||||
|
||||
// ParseFromLongDateTimeWithTimezone parses a formatted string in long date time format
|
||||
func ParseFromLongDateTimeWithTimezone(t string) (time.Time, error) {
|
||||
return time.Parse(longDateTimeWithTimezoneFormat, t)
|
||||
}
|
||||
|
||||
// ParseFromLongDateTimeWithoutSecond parses a formatted string in long date time format (no second)
|
||||
func ParseFromLongDateTimeWithoutSecond(t string, utcOffset int16) (time.Time, error) {
|
||||
timezone := time.FixedZone("Timezone", int(utcOffset)*60)
|
||||
|
||||
@@ -131,6 +131,15 @@ func TestParseFromLongDateTime(t *testing.T) {
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestParseFromLongDateTimeWithTimezone(t *testing.T) {
|
||||
expectedValue := int64(1617238883)
|
||||
actualTime, err := ParseFromLongDateTimeWithTimezone("2021-04-01T06:01:23+05:00")
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
actualValue := actualTime.Unix()
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
|
||||
func TestParseFromLongDateTimeWithoutSecond(t *testing.T) {
|
||||
expectedValue := int64(1691947440)
|
||||
actualTime, err := ParseFromLongDateTimeWithoutSecond("2023-08-13 17:24", 0)
|
||||
|
||||
@@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetRandomInteger returns a random number, the max parameter represents upper limit
|
||||
@@ -15,3 +16,32 @@ func GetRandomInteger(max int) (int, error) {
|
||||
|
||||
return int(result.Int64()), nil
|
||||
}
|
||||
|
||||
// TrimTrailingZerosInDecimal returns a textual number without trailing zeros in decimal
|
||||
func TrimTrailingZerosInDecimal(num string) string {
|
||||
if len(num) < 1 {
|
||||
return num
|
||||
}
|
||||
|
||||
dotPosition := strings.Index(num, ".")
|
||||
|
||||
if dotPosition < 0 {
|
||||
return num
|
||||
}
|
||||
|
||||
lastNonZeroPosition := len(num)
|
||||
|
||||
for i := len(num) - 1; i > dotPosition+1; i-- {
|
||||
if num[i] == '0' {
|
||||
lastNonZeroPosition = i
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if lastNonZeroPosition >= len(num) {
|
||||
return num
|
||||
}
|
||||
|
||||
return num[0:lastNonZeroPosition]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTrimTrailingZerosInDecimal(t *testing.T) {
|
||||
expectedValue := "123.45"
|
||||
actualValue := TrimTrailingZerosInDecimal("123.45000000000")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = "0.12"
|
||||
actualValue = TrimTrailingZerosInDecimal("0.12000000000")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = "0.120000000001"
|
||||
actualValue = TrimTrailingZerosInDecimal("0.120000000001")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = ".12"
|
||||
actualValue = TrimTrailingZerosInDecimal(".12000000000")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = "12345000000000"
|
||||
actualValue = TrimTrailingZerosInDecimal("12345000000000")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
|
||||
expectedValue = ""
|
||||
actualValue = TrimTrailingZerosInDecimal("")
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
}
|
||||
Reference in New Issue
Block a user