vitess.io/vitess@v0.16.2/go/vt/vitessdriver/time.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vitessdriver
    18  
    19  import (
    20  	"errors"
    21  	"time"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  )
    25  
    26  // ErrInvalidTime is returned when we fail to parse a datetime
    27  // string from MySQL. This should never happen unless things are
    28  // seriously messed up.
    29  var ErrInvalidTime = errors.New("invalid MySQL time string")
    30  
    31  var isoTimeFormat = "2006-01-02 15:04:05.999999"
    32  var isoNullTime = "0000-00-00 00:00:00.000000"
    33  var isoTimeLength = len(isoTimeFormat)
    34  
    35  // parseISOTime pases a time string in MySQL's textual datetime format.
    36  // This is very similar to ISO8601, with some differences:
    37  //
    38  //   - There is no T separator between the date and time sections;
    39  //     a space is used instead.
    40  //   - There is never a timezone section in the string, as these datetimes
    41  //     are not timezone-aware. There isn't a Z value for UTC times for
    42  //     the same reason.
    43  //
    44  // Note that this function can handle both DATE (which should _always_ have
    45  // a length of 10) and DATETIME strings (which have a variable length, 18+
    46  // depending on the number of decimal sub-second places).
    47  //
    48  // Also note that this function handles the case where MySQL returns a NULL
    49  // time (with a string where all sections are zeroes) by returning a zeroed
    50  // out time.Time object. NULL time strings are not considered a parsing error.
    51  //
    52  // See: isoTimeFormat
    53  func parseISOTime(tstr string, loc *time.Location, minLen, maxLen int) (t time.Time, err error) {
    54  	tlen := len(tstr)
    55  	if tlen < minLen || tlen > maxLen {
    56  		err = ErrInvalidTime
    57  		return
    58  	}
    59  
    60  	if tstr == isoNullTime[:tlen] {
    61  		// This is what MySQL would send when the date is NULL,
    62  		// so return an empty time.Time instead.
    63  		// This is not a parsing error
    64  		return
    65  	}
    66  
    67  	if loc == nil {
    68  		loc = time.UTC
    69  	}
    70  
    71  	// Since the time format returned from MySQL never has a Timezone
    72  	// section, ParseInLocation will initialize the time.Time struct
    73  	// with the default `loc` we're passing here.
    74  	return time.ParseInLocation(isoTimeFormat[:tlen], tstr, loc)
    75  }
    76  
    77  // DatetimeToNative converts a Datetime Value into a time.Time
    78  func DatetimeToNative(v sqltypes.Value, loc *time.Location) (time.Time, error) {
    79  	// Valid format string offsets for a DATETIME
    80  	//  |DATETIME          |19+
    81  	//  |------------------|------|
    82  	// "2006-01-02 15:04:05.999999"
    83  	return parseISOTime(v.ToString(), loc, 19, isoTimeLength)
    84  }
    85  
    86  // DateToNative converts a Date Value into a time.Time.
    87  // Note that there's no specific type in the Go stdlib to represent
    88  // dates without time components, so the returned Time will have
    89  // their hours/mins/seconds zeroed out.
    90  func DateToNative(v sqltypes.Value, loc *time.Location) (time.Time, error) {
    91  	// Valid format string offsets for a DATE
    92  	//  |DATE     |10
    93  	//  |---------|
    94  	// "2006-01-02 00:00:00.000000"
    95  	return parseISOTime(v.ToString(), loc, 10, 10)
    96  }
    97  
    98  // NewDatetime builds a Datetime Value
    99  func NewDatetime(t time.Time, defaultLoc *time.Location) sqltypes.Value {
   100  	if t.Location() != defaultLoc {
   101  		t = t.In(defaultLoc)
   102  	}
   103  	return sqltypes.MakeTrusted(sqltypes.Datetime, []byte(t.Format(isoTimeFormat)))
   104  }