// UnmarshalXML parses date from Expiration and validates date format
func (eDate *ExpirationDate) UnmarshalXML(d *xml.Decoder, startElement xml.StartElement) error {
var dateStr string // zp: 表示时间和时区,如2020-11-08T00:00:00Z;2020-11-08T00:00:00+08:00
err := d.DecodeElement(&dateStr, &startElement)
if err != nil {
return err
}
// While AWS documentation mentions that the date specified
// must be present in ISO 8601 format, in reality they allow
// users to provide RFC 3339 compliant dates.
expDate, err := time.Parse(time.RFC3339, dateStr) // zp: 解析成time
if err != nil {
return errLifecycleInvalidDate
}
// Allow only date timestamp specifying midnight GMT
hr, min, sec := expDate.Clock()
nsec := expDate.Nanosecond()
loc := expDate.Location()
if !(hr == 0 && min == 0 && sec == 0 && nsec == 0 && loc.String() == time.UTC.String()) { // zp: 判断时区,time.UTC.String返回"UTC"
return errLifecycleDateNotMidnight
}
*eDate = ExpirationDate{expDate}
return nil
}
上面这段代码来自minio
若dateStr
是"2020-11-08T00:00:00+08:00"则loc.String
返回的是Local
若dateStr
是"2020-11-08T00:00:00Z"则loc.String
返回的是UTC
,即若希望才有RFC3339解析时时区采用UTC格式,则格式应该是"2020-11-08T00:00:00Z"
rfc3339 是一种包含时区信息的字符串标准格式。格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。
网友评论