Path: blob/main/vendor/github.com/pelletier/go-toml/v2/localtime.go
2880 views
package toml12import (3"fmt"4"strings"5"time"67"github.com/pelletier/go-toml/v2/unstable"8)910// LocalDate represents a calendar day in no specific timezone.11type LocalDate struct {12Year int13Month int14Day int15}1617// AsTime converts d into a specific time instance at midnight in zone.18func (d LocalDate) AsTime(zone *time.Location) time.Time {19return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, zone)20}2122// String returns RFC 3339 representation of d.23func (d LocalDate) String() string {24return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)25}2627// MarshalText returns RFC 3339 representation of d.28func (d LocalDate) MarshalText() ([]byte, error) {29return []byte(d.String()), nil30}3132// UnmarshalText parses b using RFC 3339 to fill d.33func (d *LocalDate) UnmarshalText(b []byte) error {34res, err := parseLocalDate(b)35if err != nil {36return err37}38*d = res39return nil40}4142// LocalTime represents a time of day of no specific day in no specific43// timezone.44type LocalTime struct {45Hour int // Hour of the day: [0; 24[46Minute int // Minute of the hour: [0; 60[47Second int // Second of the minute: [0; 60[48Nanosecond int // Nanoseconds within the second: [0, 1000000000[49Precision int // Number of digits to display for Nanosecond.50}5152// String returns RFC 3339 representation of d.53// If d.Nanosecond and d.Precision are zero, the time won't have a nanosecond54// component. If d.Nanosecond > 0 but d.Precision = 0, then the minimum number55// of digits for nanoseconds is provided.56func (d LocalTime) String() string {57s := fmt.Sprintf("%02d:%02d:%02d", d.Hour, d.Minute, d.Second)5859if d.Precision > 0 {60s += fmt.Sprintf(".%09d", d.Nanosecond)[:d.Precision+1]61} else if d.Nanosecond > 0 {62// Nanoseconds are specified, but precision is not provided. Use the63// minimum.64s += strings.Trim(fmt.Sprintf(".%09d", d.Nanosecond), "0")65}6667return s68}6970// MarshalText returns RFC 3339 representation of d.71func (d LocalTime) MarshalText() ([]byte, error) {72return []byte(d.String()), nil73}7475// UnmarshalText parses b using RFC 3339 to fill d.76func (d *LocalTime) UnmarshalText(b []byte) error {77res, left, err := parseLocalTime(b)78if err == nil && len(left) != 0 {79err = unstable.NewParserError(left, "extra characters")80}81if err != nil {82return err83}84*d = res85return nil86}8788// LocalDateTime represents a time of a specific day in no specific timezone.89type LocalDateTime struct {90LocalDate91LocalTime92}9394// AsTime converts d into a specific time instance in zone.95func (d LocalDateTime) AsTime(zone *time.Location) time.Time {96return time.Date(d.Year, time.Month(d.Month), d.Day, d.Hour, d.Minute, d.Second, d.Nanosecond, zone)97}9899// String returns RFC 3339 representation of d.100func (d LocalDateTime) String() string {101return d.LocalDate.String() + "T" + d.LocalTime.String()102}103104// MarshalText returns RFC 3339 representation of d.105func (d LocalDateTime) MarshalText() ([]byte, error) {106return []byte(d.String()), nil107}108109// UnmarshalText parses b using RFC 3339 to fill d.110func (d *LocalDateTime) UnmarshalText(data []byte) error {111res, left, err := parseLocalDateTime(data)112if err == nil && len(left) != 0 {113err = unstable.NewParserError(left, "extra characters")114}115if err != nil {116return err117}118119*d = res120return nil121}122123124