Skip to content

Commit

Permalink
- 24 hour time format supported, relate issue qax-os#163;
Browse files Browse the repository at this point in the history
- godoc and go test updated
  • Loading branch information
xuri committed Dec 1, 2017
1 parent 2dc3854 commit 07a4140
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions sheetpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
*o = AutoPageBreaks(pr.PageSetUpPr.AutoPageBreaks)
}

// SetSheetPrOptions sets sheet properties.
// SetSheetPrOptions provides function to sets worksheet properties.
//
// Available options:
// CodeName(string)
Expand All @@ -120,7 +120,7 @@ func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
return nil
}

// SetSheetPrOptions sets sheet properties.
// GetSheetPrOptions provides function to gets worksheet properties.
//
// Available options:
// CodeName(string)
Expand Down
3 changes: 1 addition & 2 deletions sheetpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"testing"

"github.com/mohae/deepcopy"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/xuri/excelize"
)

var _ = []excelize.SheetPrOption{
Expand Down
2 changes: 1 addition & 1 deletion sheetview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/xuri/excelize"
)

var _ = []excelize.SheetViewOption{
Expand Down
18 changes: 16 additions & 2 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ func formatToE(i int, v string) string {
// March, or the 'd' in Tuesday) below. First we convert them to arbitrary
// characters unused in Excel Date formats, and then at the end, turn them to
// what they should actually be.
// Based off: http://www.ozgrid.com/Excel/CustomFormats.htm
func parseTime(i int, v string) string {
f, err := strconv.ParseFloat(v, 64)
if err != nil {
Expand All @@ -943,8 +944,6 @@ func parseTime(i int, v string) string {
{"mmm", "Jan"},
{"mmss", "0405"},
{"ss", "05"},
{"hh", "15"},
{"h", "3"},
{"mm:", "04:"},
{":mm", ":04"},
{"mm", "01"},
Expand All @@ -953,13 +952,23 @@ func parseTime(i int, v string) string {
{"%%%%", "January"},
{"&&&&", "Monday"},
}
// It is the presence of the "am/pm" indicator that determins if this is a
// 12 hour or 24 hours time format, not the number of 'h' characters.
if is12HourTime(format) {
format = strings.Replace(format, "hh", "03", 1)
format = strings.Replace(format, "h", "3", 1)
} else {
format = strings.Replace(format, "hh", "15", 1)
format = strings.Replace(format, "h", "15", 1)
}
for _, repl := range replacements {
format = strings.Replace(format, repl.xltime, repl.gotime, 1)
}
// If the hour is optional, strip it out, along with the possible dangling
// colon that would remain.
if val.Hour() < 1 {
format = strings.Replace(format, "]:", "]", 1)
format = strings.Replace(format, "[03]", "", 1)
format = strings.Replace(format, "[3]", "", 1)
format = strings.Replace(format, "[15]", "", 1)
} else {
Expand All @@ -969,6 +978,11 @@ func parseTime(i int, v string) string {
return val.Format(format)
}

// is12HourTime checks whether an Excel time format string is a 12 hours form.
func is12HourTime(format string) bool {
return strings.Contains(format, "am/pm") || strings.Contains(format, "AM/PM") || strings.Contains(format, "a/p") || strings.Contains(format, "A/P")
}

// stylesReader provides function to get the pointer to the structure after
// deserialization of xl/styles.xml.
func (f *File) stylesReader() *xlsxStyleSheet {
Expand Down

0 comments on commit 07a4140

Please sign in to comment.