timestamp

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DayLayout = "2006-01-02"

DayLayout is the string layout as well as DynamoDB string value of Day.

View Source
const FractionalSecondLayout = "2006-01-02T15:04:05.000Z"

FractionalSecondLayout is the string layout as well as DynamoDB string value of Timestamp.

Variables

This section is empty.

Functions

This section is empty.

Types

type Day

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

Day is a timestamp that is truncated to start of day.

Day is a struct to avoid the truncation pitfalls that other timestamps have.

func ParseDay

func ParseDay(value string) (Day, error)

ParseDay creates an instance of Day from parsing the specified string.

If the string fails to be parsed using layout DayLayout, a zero-value Day is returned.

func TodayInLocation

func TodayInLocation(loc *time.Location) Day

TodayInLocation creates a new Day with the specified location.

func TruncateToStartOfDay

func TruncateToStartOfDay(t time.Time) Day

TruncateToStartOfDay creates a new Day from the specified time.Time.

func (Day) After

func (d Day) After(other Day) bool

After is convenient method to time.Time.After.

func (Day) Before

func (d Day) Before(other Day) bool

Before is convenient method to time.Time.Before.

func (Day) Compare

func (d Day) Compare(other Day) int

Compare is convenient method to time.Time.Compare.

func (Day) Equal

func (d Day) Equal(other Day) bool

Equal is convenient method to time.Time.Equal.

func (Day) Format

func (d Day) Format(layout string) string

Format is convenient method to time.Time.Format.

func (Day) In

func (d Day) In(loc *time.Location) Day

In is convenient method to time.Time.In.

func (Day) IsZero

func (d Day) IsZero() bool

IsZero is convenient method to time.Time.IsZero.

func (Day) MarshalDynamoDBAttributeValue

func (d Day) MarshalDynamoDBAttributeValue() (types.AttributeValue, error)

MarshalDynamoDBAttributeValue must not use receiver pointer to allow both pointer and non-pointer usage.

func (Day) MarshalJSON

func (d Day) MarshalJSON() ([]byte, error)

MarshalJSON must not use receiver pointer to allow both pointer and non-pointer usage.

func (Day) String

func (d Day) String() string

String implements the fmt.Stringer interface.

func (Day) ToAttributeValueMap

func (d Day) ToAttributeValueMap(key string) map[string]types.AttributeValue

ToAttributeValueMap is convenient method to implement [.model.HasCreatedDay] or [.model.HasModifiedDay].

func (Day) ToTime

func (d Day) ToTime() time.Time

ToTime returns a copy of the underlying time.Time instance.

func (*Day) UnmarshalDynamoDBAttributeValue

func (d *Day) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error

func (*Day) UnmarshalJSON

func (d *Day) UnmarshalJSON(data []byte) error

type EpochMillisecond

type EpochMillisecond time.Time

EpochMillisecond is epoch millisecond in UTC, formatted and marshalled as a positive integer (e.g. 1136214245000).

Because EpochMillisecond wraps around time.Time and truncates its serialisation, deserialisation of EpochMillisecond values will not result in identical time.Time values. For example:

func TestEpochMillisecond_TruncateNanosecond(t *testing.T) {
	v, err := time.Parse(time.RFC3339Nano, "2006-01-02T15:04:05.999999Z")
	if err != nil {
		t.Error(err)
	}

	data, err := json.Marshal(EpochMillisecond(v))
	if err != nil {
		t.Error(err)
	}

	got := EpochMillisecond(time.Time{})
	if err := json.Unmarshal(data, &got); err != nil {
		t.Error(err)
	}

	// got's underlying time.time is truncated to 2006-01-02T15:04:05.
	if reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("shouldn't be equal; got %v, want %v", got, v)
	}

	// if we reset v's nano time, then they are equal.
	v = time.Date(v.Year(), v.Month(), v.Day(), v.Hour(), v.Minute(), v.Second(), got.ToTime().Nanosecond(), v.Location())
	if !reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("got %#v, want %#v", got.ToTime(), v)
	}
}

func (EpochMillisecond) After

func (e EpochMillisecond) After(other EpochMillisecond) bool

After is convenient method to time.Time.After.

func (EpochMillisecond) Before

func (e EpochMillisecond) Before(other EpochMillisecond) bool

Before is convenient method to time.Time.Before.

func (EpochMillisecond) Compare

func (e EpochMillisecond) Compare(other EpochMillisecond) int

Compare is convenient method to time.Time.Compare.

func (EpochMillisecond) Equal

func (e EpochMillisecond) Equal(other EpochMillisecond) bool

Equal is convenient method to time.Time.Equal.

func (EpochMillisecond) Format

func (e EpochMillisecond) Format(layout string) string

Format is convenient method to time.Time.Format.

func (EpochMillisecond) IsZero

func (e EpochMillisecond) IsZero() bool

IsZero is convenient method to time.Time.IsZero.

func (EpochMillisecond) MarshalDynamoDBAttributeValue

func (e EpochMillisecond) MarshalDynamoDBAttributeValue() (types.AttributeValue, error)

MarshalDynamoDBAttributeValue must not use receiver pointer to allow both pointer and non-pointer usage.

func (EpochMillisecond) MarshalJSON

func (e EpochMillisecond) MarshalJSON() ([]byte, error)

MarshalJSON must not use receiver pointer to allow both pointer and non-pointer usage.

func (EpochMillisecond) String

func (e EpochMillisecond) String() string

String implements the fmt.Stringer interface.

func (EpochMillisecond) ToAttributeValueMap

func (e EpochMillisecond) ToAttributeValueMap(key string) map[string]types.AttributeValue

ToAttributeValueMap is convenient method to implement [.model.HasCreatedTimestamp] or [.model.HasModifiedTimestamp].

func (*EpochMillisecond) ToTime

func (e *EpochMillisecond) ToTime() time.Time

ToTime returns the underlying time.Time instance.

func (*EpochMillisecond) UnmarshalDynamoDBAttributeValue

func (e *EpochMillisecond) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error

func (*EpochMillisecond) UnmarshalJSON

func (e *EpochMillisecond) UnmarshalJSON(data []byte) error

type EpochSecond

type EpochSecond time.Time

EpochSecond is epoch second in UTC, formatted and marshalled as a positive integer (e.g. 1136214245).

This can be used as DynamoDB's time-to-live value (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html).

Because EpochSecond wraps around time.Time and truncates its serialisation, deserialisation of EpochSecond values will not result in identical time.Time values. For example:

func TestEpochSecond_TruncateNanosecond(t *testing.T) {
	v, err := time.Parse(time.RFC3339Nano, "2006-01-02T15:04:05.999999Z")
	if err != nil {
		t.Error(err)
	}

	data, err := json.Marshal(EpochSecond(v))
	if err != nil {
		t.Error(err)
	}

	got := EpochSecond(time.Time{})
	if err := json.Unmarshal(data, &got); err != nil {
		t.Error(err)
	}

	// got's underlying time.time is truncated to 2006-01-02T15:04:05.
	if reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("shouldn't be equal; got %v, want %v", got, v)
	}

	// if we reset v's nano time, then they are equal.
	v = time.Date(v.Year(), v.Month(), v.Day(), v.Hour(), v.Minute(), v.Second(), got.ToTime().Nanosecond(), v.Location())
	if !reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("got %#v, want %#v", got.ToTime(), v)
	}
}

func (EpochSecond) After

func (e EpochSecond) After(other EpochSecond) bool

After is convenient method to time.Time.After.

func (EpochSecond) Before

func (e EpochSecond) Before(other EpochSecond) bool

Before is convenient method to time.Time.Before.

func (EpochSecond) Compare

func (e EpochSecond) Compare(other EpochSecond) int

Compare is convenient method to time.Time.Compare.

func (EpochSecond) Equal

func (e EpochSecond) Equal(other EpochSecond) bool

Equal is convenient method to time.Time.Equal.

func (EpochSecond) Format

func (e EpochSecond) Format(layout string) string

Format is convenient method to time.Time.Format.

func (EpochSecond) IsZero

func (e EpochSecond) IsZero() bool

IsZero is convenient method to time.Time.IsZero.

func (EpochSecond) MarshalDynamoDBAttributeValue

func (e EpochSecond) MarshalDynamoDBAttributeValue() (types.AttributeValue, error)

MarshalDynamoDBAttributeValue must not use receiver pointer to allow both pointer and non-pointer usage.

func (EpochSecond) MarshalJSON

func (e EpochSecond) MarshalJSON() ([]byte, error)

MarshalJSON must not use receiver pointer to allow both pointer and non-pointer usage.

func (EpochSecond) String

func (e EpochSecond) String() string

String implements the fmt.Stringer interface.

func (EpochSecond) ToAttributeValueMap

func (e EpochSecond) ToAttributeValueMap(key string) map[string]types.AttributeValue

ToAttributeValueMap is convenient method to implement [.model.HasCreatedTimestamp] or [.model.HasModifiedTimestamp].

func (*EpochSecond) ToTime

func (e *EpochSecond) ToTime() time.Time

ToTime returns the underlying time.Time instance.

func (*EpochSecond) UnmarshalDynamoDBAttributeValue

func (e *EpochSecond) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error

func (*EpochSecond) UnmarshalJSON

func (e *EpochSecond) UnmarshalJSON(data []byte) error

type Timestamp

type Timestamp time.Time

Timestamp is a UTC timestamp formatted and marshalled as a string using FractionalSecondLayout ("2006-01-02T15:04:05.000Z") layout.

Because Timestamp wraps around time.Time and truncates its serialisation, deserialisation of Timestamp values will not result in identical time.Time values. For example:

func TestTimestamp_TruncateNanosecond(t *testing.T) {
	v, err := time.Parse(time.RFC3339Nano, "2006-01-02T15:04:05.999999Z")
	if err != nil {
		t.Error(err)
	}

	data, err := json.Marshal(Timestamp(v))
	if err != nil {
		t.Error(err)
	}

	got := Timestamp(time.Time{})
	if err := json.Unmarshal(data, &got); err != nil {
		t.Error(err)
	}

	// got's underlying time.time is truncated to 2006-01-02T15:04:05.999.
	if reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("shouldn't be equal; got %v, want %v", got, v)
	}

	// if we reset v's nano time, then they are equal.
	v = time.Date(v.Year(), v.Month(), v.Day(), v.Hour(), v.Minute(), v.Second(), got.ToTime().Nanosecond(), v.Location())
	if !reflect.DeepEqual(got.ToTime(), v) {
		t.Errorf("got %#v, want %#v", got.ToTime(), v)
	}
}

func ParseTimestamp

func ParseTimestamp(value string) (Timestamp, error)

ParseTimestamp creates an instance of Timestamp from parsing the specified string.

If the string fails to be parsed using layout FractionalSecondLayout, a zero-value Timestamp is returned.

func (Timestamp) After

func (t Timestamp) After(other Timestamp) bool

After is convenient method to time.Time.After.

func (Timestamp) Before

func (t Timestamp) Before(other Timestamp) bool

Before is convenient method to time.Time.Before.

func (Timestamp) Compare

func (t Timestamp) Compare(other Timestamp) int

Compare is convenient method to time.Time.Compare.

func (Timestamp) Equal

func (t Timestamp) Equal(other Timestamp) bool

Equal is convenient method to time.Time.Equal.

func (Timestamp) Format

func (t Timestamp) Format(layout string) string

Format is convenient method to time.Time.Format.

func (Timestamp) IsZero

func (t Timestamp) IsZero() bool

IsZero is convenient method to time.Time.IsZero.

func (Timestamp) MarshalDynamoDBAttributeValue

func (t Timestamp) MarshalDynamoDBAttributeValue() (types.AttributeValue, error)

MarshalDynamoDBAttributeValue must not use receiver pointer to allow both pointer and non-pointer usage.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON must not use receiver pointer to allow both pointer and non-pointer usage.

func (Timestamp) String

func (t Timestamp) String() string

String implements the fmt.Stringer interface.

func (Timestamp) ToAttributeValueMap

func (t Timestamp) ToAttributeValueMap(key string) map[string]types.AttributeValue

ToAttributeValueMap is convenient method to implement [.model.HasCreatedTimestamp] or [.model.HasModifiedTimestamp].

func (*Timestamp) ToTime

func (t *Timestamp) ToTime() time.Time

ToTime returns the underlying time.Time instance.

func (*Timestamp) UnmarshalDynamoDBAttributeValue

func (t *Timestamp) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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