Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing built in date formatting #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/number_format/indexed.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func init() {
0x0b: {ml.NumberFormat{ID: 0x0b, Code: `0.00E+00`}, Float},
0x0c: {ml.NumberFormat{ID: 0x0c, Code: `# ?/?`}, Float},
0x0d: {ml.NumberFormat{ID: 0x0d, Code: `# ??/??`}, Float},
0x0e: {ml.NumberFormat{ID: 0x0e, Code: `m-d-yy`}, Date},
0x0e: {ml.NumberFormat{ID: 0x0e, Code: `d/m/yyyy`}, Date},
0x0f: {ml.NumberFormat{ID: 0x0f, Code: `d-mmm-yy`}, Date},
0x10: {ml.NumberFormat{ID: 0x10, Code: `d-mmm`}, Date},
0x11: {ml.NumberFormat{ID: 0x11, Code: `mmm-yy`}, Date},
0x12: {ml.NumberFormat{ID: 0x12, Code: `h:mm AM/PM`}, Time},
0x13: {ml.NumberFormat{ID: 0x13, Code: `h:mm:ss AM/PM`}, Time},
0x14: {ml.NumberFormat{ID: 0x14, Code: `h:mm`}, Time},
0x15: {ml.NumberFormat{ID: 0x15, Code: `h:mm:ss`}, Time},
0x16: {ml.NumberFormat{ID: 0x16, Code: `m-d-yy h:mm`}, DateTime},
0x16: {ml.NumberFormat{ID: 0x16, Code: `m/d/yy H:mm`}, DateTime},
//...
0x25: {ml.NumberFormat{ID: 0x25, Code: `(#,##0_);(#,##0)`}, Integer},
0x26: {ml.NumberFormat{ID: 0x26, Code: `(#,##0_);[RED](#,##0)`}, Integer},
Expand Down
42 changes: 42 additions & 0 deletions internal/number_format/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package number

import (
"strings"

"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/internal/ml/primitives"
"github.com/plandem/xlsx/internal/number_format/convert"
)

//Type of underlying value of built-in number format
Expand All @@ -32,6 +35,27 @@
//LastReservedID is id of last built-in/reserved format
const LastReservedID = 163

const dateFormatChars = "dmyhH"

type dateFormatMapping struct {
excelFormat string
goFormat string
}

// the order of the mapping matters
var dateFormatParts = []dateFormatMapping{
{"d", "02"},
{"mmm", "Jan"},
{"mm", "04"},
{"m", "01"},
{"yyyy", "2006"},
{"yy", "06"},
{"h", "03"},
{"H", "15"},
{"ss", "05"},
{"AM/PM", "PM"},
}

//New create and return ml.NumberFormat type for provided values, respecting built-in number formats
func New(id int, code string) ml.NumberFormat {
return Normalize(ml.NumberFormat{ID: id, Code: code})
Expand Down Expand Up @@ -94,6 +118,24 @@

//Format tries to format value into required format code
func Format(value, code string, t primitives.CellType) string {
if strings.ContainsAny(code, dateFormatChars) {
return formatDate(value, code)
}

//TODO: implement formatting based on code and type
return value
}

func formatDate(value, code string) string {
dateFormat := code
for _, mapping := range dateFormatParts {
dateFormat = strings.ReplaceAll(dateFormat, mapping.excelFormat, mapping.goFormat)
}

dateValue, err := convert.ToDate(value)
if err != nil {
return value

Check warning on line 137 in internal/number_format/types.go

View check run for this annotation

Codecov / codecov/patch

internal/number_format/types.go#L137

Added line #L137 was not covered by tests
}

return dateValue.UTC().Format(dateFormat)
}
26 changes: 26 additions & 0 deletions internal/number_format/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package number

import (
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/internal/ml/primitives"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
Expand Down Expand Up @@ -44,3 +46,27 @@ func TestNumberFormat(t *testing.T) {
//custom ID was provided
require.Equal(t, ml.NumberFormat(ml.NumberFormat{ID: 1000, Code: ""}), New(1000, ""))
}

func TestDateFormat(t *testing.T) {
value := "36892.521"
var testCases = []struct {
code string
expected string
}{
{"d/m/yyyy", "01/01/2001"},
{"d-mmm-yy", "01-Jan-01"},
{"d-mmm", "01-Jan"},
{"mmm-yy", "Jan-01"},
{"h:mm AM/PM", "12:30 PM"},
{"h:mm:ss AM/PM", "12:30:14 PM"},
{"h:mm", "12:30"},
{"h:mm:ss", "12:30:14"},
{"m/d/yy H:mm", "01/01/01 12:30"},
}
for _, test := range testCases {
t.Run(test.code, func(t *testing.T) {
result := Format(value, test.code, primitives.CellType(0))
assert.Equal(t, test.expected, result)
})
}
}