-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #7 - Add ripple time to unix time conversion functions
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |