...

Source file src/go.mongodb.org/mongo-driver/bson/bsoncodec/time_codec.go

Documentation: go.mongodb.org/mongo-driver/bson/bsoncodec

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package bsoncodec
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  	"time"
    13  
    14  	"go.mongodb.org/mongo-driver/bson/bsonoptions"
    15  	"go.mongodb.org/mongo-driver/bson/bsonrw"
    16  	"go.mongodb.org/mongo-driver/bson/bsontype"
    17  	"go.mongodb.org/mongo-driver/bson/primitive"
    18  )
    19  
    20  const (
    21  	timeFormatString = "2006-01-02T15:04:05.999Z07:00"
    22  )
    23  
    24  // TimeCodec is the Codec used for time.Time values.
    25  //
    26  // Deprecated: TimeCodec will not be directly configurable in Go Driver 2.0.
    27  // To configure the time.Time encode and decode behavior, use the configuration
    28  // methods on a [go.mongodb.org/mongo-driver/bson.Encoder] or
    29  // [go.mongodb.org/mongo-driver/bson.Decoder]. To configure the time.Time encode
    30  // and decode behavior for a mongo.Client, use
    31  // [go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions].
    32  //
    33  // For example, to configure a mongo.Client to ..., use:
    34  //
    35  //	opt := options.Client().SetBSONOptions(&options.BSONOptions{
    36  //	    UseLocalTimeZone: true,
    37  //	})
    38  //
    39  // See the deprecation notice for each field in TimeCodec for the corresponding
    40  // settings.
    41  type TimeCodec struct {
    42  	// UseLocalTimeZone specifies if we should decode into the local time zone. Defaults to false.
    43  	//
    44  	// Deprecated: Use bson.Decoder.UseLocalTimeZone or options.BSONOptions.UseLocalTimeZone
    45  	// instead.
    46  	UseLocalTimeZone bool
    47  }
    48  
    49  var (
    50  	defaultTimeCodec = NewTimeCodec()
    51  
    52  	// Assert that defaultTimeCodec satisfies the typeDecoder interface, which allows it to be used
    53  	// by collection type decoders (e.g. map, slice, etc) to set individual values in a collection.
    54  	_ typeDecoder = defaultTimeCodec
    55  )
    56  
    57  // NewTimeCodec returns a TimeCodec with options opts.
    58  //
    59  // Deprecated: NewTimeCodec will not be available in Go Driver 2.0. See
    60  // [TimeCodec] for more details.
    61  func NewTimeCodec(opts ...*bsonoptions.TimeCodecOptions) *TimeCodec {
    62  	timeOpt := bsonoptions.MergeTimeCodecOptions(opts...)
    63  
    64  	codec := TimeCodec{}
    65  	if timeOpt.UseLocalTimeZone != nil {
    66  		codec.UseLocalTimeZone = *timeOpt.UseLocalTimeZone
    67  	}
    68  	return &codec
    69  }
    70  
    71  func (tc *TimeCodec) decodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
    72  	if t != tTime {
    73  		return emptyValue, ValueDecoderError{
    74  			Name:     "TimeDecodeValue",
    75  			Types:    []reflect.Type{tTime},
    76  			Received: reflect.Zero(t),
    77  		}
    78  	}
    79  
    80  	var timeVal time.Time
    81  	switch vrType := vr.Type(); vrType {
    82  	case bsontype.DateTime:
    83  		dt, err := vr.ReadDateTime()
    84  		if err != nil {
    85  			return emptyValue, err
    86  		}
    87  		timeVal = time.Unix(dt/1000, dt%1000*1000000)
    88  	case bsontype.String:
    89  		// assume strings are in the isoTimeFormat
    90  		timeStr, err := vr.ReadString()
    91  		if err != nil {
    92  			return emptyValue, err
    93  		}
    94  		timeVal, err = time.Parse(timeFormatString, timeStr)
    95  		if err != nil {
    96  			return emptyValue, err
    97  		}
    98  	case bsontype.Int64:
    99  		i64, err := vr.ReadInt64()
   100  		if err != nil {
   101  			return emptyValue, err
   102  		}
   103  		timeVal = time.Unix(i64/1000, i64%1000*1000000)
   104  	case bsontype.Timestamp:
   105  		t, _, err := vr.ReadTimestamp()
   106  		if err != nil {
   107  			return emptyValue, err
   108  		}
   109  		timeVal = time.Unix(int64(t), 0)
   110  	case bsontype.Null:
   111  		if err := vr.ReadNull(); err != nil {
   112  			return emptyValue, err
   113  		}
   114  	case bsontype.Undefined:
   115  		if err := vr.ReadUndefined(); err != nil {
   116  			return emptyValue, err
   117  		}
   118  	default:
   119  		return emptyValue, fmt.Errorf("cannot decode %v into a time.Time", vrType)
   120  	}
   121  
   122  	if !tc.UseLocalTimeZone && !dc.useLocalTimeZone {
   123  		timeVal = timeVal.UTC()
   124  	}
   125  	return reflect.ValueOf(timeVal), nil
   126  }
   127  
   128  // DecodeValue is the ValueDecoderFunc for time.Time.
   129  func (tc *TimeCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
   130  	if !val.CanSet() || val.Type() != tTime {
   131  		return ValueDecoderError{Name: "TimeDecodeValue", Types: []reflect.Type{tTime}, Received: val}
   132  	}
   133  
   134  	elem, err := tc.decodeType(dc, vr, tTime)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	val.Set(elem)
   140  	return nil
   141  }
   142  
   143  // EncodeValue is the ValueEncoderFunc for time.TIme.
   144  func (tc *TimeCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
   145  	if !val.IsValid() || val.Type() != tTime {
   146  		return ValueEncoderError{Name: "TimeEncodeValue", Types: []reflect.Type{tTime}, Received: val}
   147  	}
   148  	tt := val.Interface().(time.Time)
   149  	dt := primitive.NewDateTimeFromTime(tt)
   150  	return vw.WriteDateTime(int64(dt))
   151  }
   152  

View as plain text