datetime

package
v1.100.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: BSD-2-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package with support of Tarantool's datetime data type.

Datetime data type supported in Tarantool since 2.10.

Since: 1.7.0

See also:

* Datetime Internals https://github.com/tarantool/tarantool/wiki/Datetime-Internals

Example

Example demonstrates how to use tuples with datetime. To enable support of datetime import tarantool/datetime package.

opts := tarantool.Opts{
	User: "test",
	Pass: "test",
}
conn, err := tarantool.Connect("127.0.0.1:3013", opts)
if err != nil {
	fmt.Printf("Error in connect is %v", err)
	return
}

var datetime = "2013-10-28T17:51:56.000000009Z"
tm, err := time.Parse(time.RFC3339, datetime)
if err != nil {
	fmt.Printf("Error in time.Parse() is %v", err)
	return
}
dt, err := NewDatetime(tm)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tm, err)
	return
}

space := "testDatetime_1"
index := "primary"

// Replace a tuple with datetime.
resp, err := conn.Replace(space, []interface{}{dt})
if err != nil {
	fmt.Printf("Error in replace is %v", err)
	return
}
respDt := resp.Data[0].([]interface{})[0].(Datetime)
fmt.Println("Datetime tuple replace")
fmt.Printf("Code: %d\n", resp.Code)
fmt.Printf("Data: %v\n", respDt.ToTime())

// Select a tuple with datetime.
var offset uint32 = 0
var limit uint32 = 1
resp, err = conn.Select(space, index, offset, limit, tarantool.IterEq, []interface{}{dt})
if err != nil {
	fmt.Printf("Error in select is %v", err)
	return
}
respDt = resp.Data[0].([]interface{})[0].(Datetime)
fmt.Println("Datetime tuple select")
fmt.Printf("Code: %d\n", resp.Code)
fmt.Printf("Data: %v\n", respDt.ToTime())

// Delete a tuple with datetime.
resp, err = conn.Delete(space, index, []interface{}{dt})
if err != nil {
	fmt.Printf("Error in delete is %v", err)
	return
}
respDt = resp.Data[0].([]interface{})[0].(Datetime)
fmt.Println("Datetime tuple delete")
fmt.Printf("Code: %d\n", resp.Code)
fmt.Printf("Data: %v\n", respDt.ToTime())
Output:

Index

Examples

Constants

View Source
const (
	// NoTimezone allows to create a datetime without UTC timezone for
	// Tarantool. The problem is that Golang by default creates a time value
	// with UTC timezone. So it is a way to create a datetime without timezone.
	NoTimezone = ""
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adjust

type Adjust int

An Adjust is used as a parameter for date adjustions, see: https://github.com/tarantool/tarantool/wiki/Datetime-Internals#date-adjustions-and-leap-years

const (
	NoneAdjust   Adjust = 0 // adjust = "none" in Tarantool
	ExcessAdjust Adjust = 1 // adjust = "excess" in Tarantool
	LastAdjust   Adjust = 2 // adjust = "last" in Tarantool
)

type Datetime

type Datetime struct {
	// contains filtered or unexported fields
}

func NewDatetime

func NewDatetime(t time.Time) (*Datetime, error)

NewDatetime returns a pointer to a new datetime.Datetime that contains a specified time.Time. It may return an error if the Time value is out of supported range: [-5879610-06-22T00:00Z .. 5879611-07-11T00:00Z] or an invalid timezone or offset value is out of supported range: [-12 * 60 * 60, 14 * 60 * 60].

NOTE: Tarantool's datetime.tz value is picked from t.Location().String(). "Local" location is unsupported, see ExampleNewDatetime_localUnsupported.

Example (LocalUnsupported)

ExampleNewDatetime_localUnsupported demonstrates that "Local" location is unsupported.

tm := time.Now().Local()
loc := tm.Location()
fmt.Println("Location:", loc)
if _, err := NewDatetime(tm); err != nil {
	fmt.Printf("Could not create a Datetime with %s location.\n", loc)
} else {
	fmt.Printf("A Datetime with %s location created.\n", loc)
}
Output:

Location: Local
Could not create a Datetime with Local location.
Example (NoTimezone)

Example demonstrates how to create a datetime for Tarantool without UTC timezone in datetime.

var datetime = "2013-10-28T17:51:56.000000009Z"
tm, err := time.Parse(time.RFC3339, datetime)
if err != nil {
	fmt.Printf("Error in time.Parse() is %v", err)
	return
}

tm = tm.In(time.FixedZone(NoTimezone, 0))

dt, err := NewDatetime(tm)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tm, err)
	return
}

fmt.Printf("Time value: %v\n", dt.ToTime())
Output:

func (*Datetime) Add

func (dtime *Datetime) Add(ival Interval) (*Datetime, error)

Add creates a new Datetime as addition of the Datetime and Interval. It may return an error if a new Datetime is out of supported range.

Example

ExampleDatetime_Add demonstrates how to add an Interval to a Datetime value.

var datetime = "2013-01-31T17:51:56.000000009Z"
tm, err := time.Parse(time.RFC3339, datetime)
if err != nil {
	fmt.Printf("Error in time.Parse() is %s", err)
	return
}
dt, err := NewDatetime(tm)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tm, err)
	return
}

newdt, err := dt.Add(Interval{
	Year:   1,
	Month:  1,
	Sec:    333,
	Adjust: LastAdjust,
})
if err != nil {
	fmt.Printf("Unable to add to Datetime: %s", err)
	return
}

fmt.Printf("New time: %s\n", newdt.ToTime().String())
Output:

New time: 2014-02-28 17:57:29.000000009 +0000 UTC
Example (Dst)

ExampleDatetime_Add_dst demonstrates how to add an Interval to a Datetime value with a DST location.

loc, err := time.LoadLocation("Europe/Moscow")
if err != nil {
	fmt.Printf("Unable to load location: %s", err)
	return
}
tm := time.Date(2008, 1, 1, 1, 1, 1, 1, loc)
dt, err := NewDatetime(tm)
if err != nil {
	fmt.Printf("Unable to create Datetime: %s", err)
	return
}

fmt.Printf("Datetime time:\n")
fmt.Printf("%s\n", dt.ToTime())
fmt.Printf("Datetime time + 6 month:\n")
fmt.Printf("%s\n", dt.ToTime().AddDate(0, 6, 0))
dt, err = dt.Add(Interval{Month: 6})
if err != nil {
	fmt.Printf("Unable to add 6 month: %s", err)
	return
}
fmt.Printf("Datetime + 6 month time:\n")
fmt.Printf("%s\n", dt.ToTime())
Output:

Datetime time:
2008-01-01 01:01:01.000000001 +0300 MSK
Datetime time + 6 month:
2008-07-01 01:01:01.000000001 +0400 MSD
Datetime + 6 month time:
2008-07-01 01:01:01.000000001 +0400 MSD

func (*Datetime) Interval

func (dtime *Datetime) Interval(next *Datetime) Interval

Interval returns an Interval value to a next Datetime value.

Example

ExampleDatetime_Interval demonstrates how to get an Interval value between two Datetime values.

var first = "2013-01-31T17:51:56.000000009Z"
var second = "2015-03-20T17:50:56.000000009Z"

tmFirst, err := time.Parse(time.RFC3339, first)
if err != nil {
	fmt.Printf("Error in time.Parse() is %v", err)
	return
}
tmSecond, err := time.Parse(time.RFC3339, second)
if err != nil {
	fmt.Printf("Error in time.Parse() is %v", err)
	return
}

dtFirst, err := NewDatetime(tmFirst)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tmFirst, err)
	return
}
dtSecond, err := NewDatetime(tmSecond)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tmSecond, err)
	return
}

ival := dtFirst.Interval(dtSecond)
fmt.Printf("%v", ival)
Output:

{2 2 0 -11 0 -1 0 0 0}

func (*Datetime) MarshalMsgpack

func (dtime *Datetime) MarshalMsgpack() ([]byte, error)

func (*Datetime) Sub

func (dtime *Datetime) Sub(ival Interval) (*Datetime, error)

Sub creates a new Datetime as subtraction of the Datetime and Interval. It may return an error if a new Datetime is out of supported range.

Example

ExampleDatetime_Sub demonstrates how to subtract an Interval from a Datetime value.

var datetime = "2013-01-31T17:51:56.000000009Z"
tm, err := time.Parse(time.RFC3339, datetime)
if err != nil {
	fmt.Printf("Error in time.Parse() is %s", err)
	return
}
dt, err := NewDatetime(tm)
if err != nil {
	fmt.Printf("Unable to create Datetime from %s: %s", tm, err)
	return
}

newdt, err := dt.Sub(Interval{
	Year:   1,
	Month:  1,
	Sec:    333,
	Adjust: LastAdjust,
})
if err != nil {
	fmt.Printf("Unable to sub from Datetime: %s", err)
	return
}

fmt.Printf("New time: %s\n", newdt.ToTime().String())
Output:

New time: 2011-12-31 17:46:23.000000009 +0000 UTC

func (*Datetime) ToTime

func (dtime *Datetime) ToTime() time.Time

ToTime returns a time.Time that Datetime contains.

If a Datetime created from time.Time value then an original location is used for the time value.

If a Datetime created via unmarshaling Tarantool's datetime then we try to create a location with time.LoadLocation() first. In case of failure, we use a location created with time.FixedZone().

func (*Datetime) UnmarshalMsgpack

func (tm *Datetime) UnmarshalMsgpack(b []byte) error

type Interval

type Interval struct {
	Year   int64
	Month  int64
	Week   int64
	Day    int64
	Hour   int64
	Min    int64
	Sec    int64
	Nsec   int64
	Adjust Adjust
}

Interval type is GoLang implementation of Tarantool intervals.

func (Interval) Add

func (ival Interval) Add(add Interval) Interval

Add creates a new Interval as addition of intervals.

Example

ExampleInterval_Add demonstrates how to add two intervals.

orig := Interval{
	Year:   1,
	Month:  2,
	Week:   3,
	Sec:    10,
	Adjust: ExcessAdjust,
}
ival := orig.Add(Interval{
	Year:   10,
	Min:    30,
	Adjust: LastAdjust,
})

fmt.Printf("%v", ival)
Output:

{11 2 3 0 0 30 10 0 1}

func (Interval) Sub

func (ival Interval) Sub(sub Interval) Interval

Sub creates a new Interval as subtraction of intervals.

Example

ExampleInterval_Sub demonstrates how to subtract two intervals.

orig := Interval{
	Year:   1,
	Month:  2,
	Week:   3,
	Sec:    10,
	Adjust: ExcessAdjust,
}
ival := orig.Sub(Interval{
	Year:   10,
	Min:    30,
	Adjust: LastAdjust,
})

fmt.Printf("%v", ival)
Output:

{-9 2 3 0 0 -30 10 0 1}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL