Skip to content

Commit 176b0ce

Browse files
committed
fix(outputer): fix #328
1 parent e728b7c commit 176b0ce

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

outputer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,12 @@ func (c *Carbon) Format(format string, timezone ...string) string {
794794
buffer.WriteString(strconv.FormatInt(c.TimestampMicro(), 10))
795795
case 'X': // timestamp with nanoseconds, such as 1596604455000000000
796796
buffer.WriteString(strconv.FormatInt(c.TimestampNano(), 10))
797+
case 'u':
798+
buffer.WriteString(fmt.Sprintf("%03d", c.Millisecond()))
799+
case 'v':
800+
buffer.WriteString(fmt.Sprintf("%06d", c.Microsecond()))
801+
case 'x':
802+
buffer.WriteString(fmt.Sprintf("%09d", c.Nanosecond()))
797803
default: // common symbols
798804
buffer.WriteString(c.StdTime().Format(layout))
799805
}

outputer_unit_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package carbon
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/stretchr/testify/suite"
78
)
@@ -1778,6 +1779,16 @@ func (s *OutputerSuite) TestCarbon_Format() {
17781779
s.Equal("1596633255999999", Parse("2020-08-05 13:14:15.999999999").Format(TimestampMicroFormat))
17791780
s.Equal("1596633255999999999", Parse("2020-08-05 13:14:15.999999999").Format(TimestampNanoFormat))
17801781
})
1782+
1783+
// https://github.com/dromara/carbon/issues/328
1784+
s.Run("issue328", func() {
1785+
inputTime := time.Date(2026, 1, 22, 11, 30, 52, 624_225_666, time.UTC)
1786+
c := CreateFromStdTime(inputTime)
1787+
1788+
s.Equal(inputTime.Format("2006-01-02 15:04:05.999"), c.Format("Y-m-d H:i:s.u"))
1789+
s.Equal(inputTime.Format("2006-01-02 15:04:05.999999"), c.Format("Y-m-d H:i:s.v"))
1790+
s.Equal(inputTime.Format("2006-01-02 15:04:05.999999999"), c.Format("Y-m-d H:i:s.x"))
1791+
})
17811792
}
17821793

17831794
func (s *OutputerSuite) TestFormat2Layout() {

0 commit comments

Comments
 (0)