timespace

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 3 Imported by: 0

README

timespace Package

The timespace package provides utility functions for converting between various timestamp formats and time representations.

Functions

FromJSTimestamp

The FromJSTimestamp function converts a JavaScript timestamp string (in milliseconds) to a time.Time object. It parses the string to an int64 and converts it to time.Time. If parsing fails, it returns the current time and an error.

func FromJSTimestamp(s string) (time.Time, error)
Parameters
  • s string: The JavaScript timestamp string (in milliseconds) to convert.
Returns
  • time.Time: The converted time.Time object.
  • error: An error if the parsing fails.
FromJSTSToTime

The FromJSTSToTime function converts a JavaScript timestamp (in milliseconds) represented as an int64 to a time.Time object. It divides the timestamp by 1000 to get seconds and calculates nanoseconds for the remainder.

func FromJSTSToTime(ts int64) time.Time
Parameters
  • ts int64: The JavaScript timestamp (in milliseconds) to convert.
Returns
  • time.Time: The converted time.Time object.
FromUnixToTimestamp

The FromUnixToTimestamp function converts a Unix timestamp (in seconds) represented as an int64 to a *timestamppb.Timestamp. If the timestamp is zero, it returns nil.

func FromUnixToTimestamp(ts int64) *timestamppb.Timestamp
Parameters
  • ts int64: The Unix timestamp (in seconds) to convert.
Returns
  • timestamppb.Timestamp: The converted *timestamppb.Timestamp, or nil if the timestamp is zero.
Usage Example
package main

import (
    "fmt"
    "github.com/Sectoid-Systems/sectoid-go-kit/timespace"
)

func main() {
    // Example of FromJSTimestamp
    jsTimestamp := "1625097600000"
    t, err := timespace.FromJSTimestamp(jsTimestamp)
    if err != nil {
        fmt.Println("Error converting JS timestamp:", err)
    } else {
        fmt.Println("Converted time:", t)
    }

    // Example of FromUnixToTimestamp
    unixTimestamp := int64(1625097600)
    ts := timespace.FromUnixToTimestamp(unixTimestamp)
    if ts != nil {
        fmt.Println("Converted timestamp:", ts)
    } else {
        fmt.Println("Timestamp is zero or invalid")
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromJSTSToTime

func FromJSTSToTime(ts int64) time.Time

FromJSTSToTime converts a JavaScript timestamp (in milliseconds) represented as an int64 to a time.Time object. It divides the timestamp by 1000 to get seconds and calculates nanoseconds for the remainder.

func FromJSTimestamp

func FromJSTimestamp(s string) (time.Time, error)

FromJSTimestamp converts a JavaScript timestamp string (in milliseconds) to a time.Time object. It parses the string to an int64 and converts it to time.Time. If parsing fails, it returns the current time and an error.

func FromUnixToTimestamp

func FromUnixToTimestamp(ts int64) *timestamppb.Timestamp

FromUnixToTimestamp converts a Unix timestamp (in seconds) represented as an int64 to a *timestamppb.Timestamp. If the timestamp is zero, it returns nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

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