Skip to content

Commit

Permalink
Fixes #7 - Add ripple time to unix time conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pkcs8 committed Sep 10, 2024
1 parent 326537c commit 130ddc3
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package xrpl

import (
"time"
)

/*
* Ripple time to Unix time conversion
*/

// Seconds since UNIX Epoch to Ripple Epoch (2000-01-01T00:00 UTC)
// https://xrpl.org/docs/references/protocol/data-types/basic-data-types#specifying-time
const RIPPLE_EPOCH_DIFF int64 = 946684800

// Convert a Ripple timestamp to a unix timestamp.
func RippleTimeToUnixTime(rippleTime int64) int64 {
return (rippleTime + RIPPLE_EPOCH_DIFF)
}

// Convert a unix timestamp to a Ripple timestamp.
func UnixTimeToRippleTime(unixTime int64) int64 {
return (unixTime - RIPPLE_EPOCH_DIFF)
}

// Convert a Ripple timestamp to an ISO8601 time.
func RippleTimeToISOTime(rippleTime int64) string {
unixTime := RippleTimeToUnixTime(rippleTime)
return time.Unix(unixTime, 0).Format(time.RFC3339)
}

// Convert an ISO8601 timestamp to a Ripple timestamp.
func IsoTimeToRippleTime(isoTime string) (int64, error) {
theTime, err := time.Parse(time.RFC3339, isoTime)
if err != nil {
return 0, err
}
rippleTime := UnixTimeToRippleTime(theTime.Unix())
return rippleTime, nil
}

0 comments on commit 130ddc3

Please sign in to comment.