You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
10 years ago
|
/*
|
||
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
package drivers
|
||
10 years ago
|
|
||
|
import (
|
||
10 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
10 years ago
|
// Date - [0000-00-00]
|
||
10 years ago
|
type Date struct {
|
||
|
Year int16
|
||
|
Month byte
|
||
|
Day byte
|
||
|
}
|
||
|
|
||
10 years ago
|
// String output in yyyy-mm-dd format
|
||
10 years ago
|
func (d Date) String() string {
|
||
|
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
|
||
|
}
|
||
|
|
||
10 years ago
|
// IsZero true if date is 0000-00-00
|
||
10 years ago
|
func (d Date) IsZero() bool {
|
||
|
return d.Day == 0 && d.Month == 0 && d.Year == 0
|
||
|
}
|
||
|
|
||
|
// Convert string date in format YYYY-MM-DD to Date.
|
||
|
// Leading and trailing spaces are ignored. If format is invalid returns zero.
|
||
10 years ago
|
func parseDate(str string) (d Date, err error) {
|
||
10 years ago
|
str = strings.TrimSpace(str)
|
||
|
if str == "0000-00-00" {
|
||
|
return
|
||
|
}
|
||
|
var (
|
||
|
y, m, n int
|
||
|
)
|
||
|
if len(str) != 10 || str[4] != '-' || str[7] != '-' {
|
||
10 years ago
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
if y, err = strconv.Atoi(str[0:4]); err != nil {
|
||
|
return
|
||
|
}
|
||
|
if m, err = strconv.Atoi(str[5:7]); err != nil {
|
||
|
return
|
||
|
}
|
||
|
if m < 1 || m > 12 {
|
||
10 years ago
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
if n, err = strconv.Atoi(str[8:10]); err != nil {
|
||
|
return
|
||
|
}
|
||
|
if n < 1 || n > 31 {
|
||
10 years ago
|
err = errors.New("Invalid 0000-00-000 style DATE string: " + str)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
d.Year = int16(y)
|
||
|
d.Month = byte(m)
|
||
|
d.Day = byte(n)
|
||
|
return
|
||
|
}
|