Packages
A native Elixir SDK for the Hedera network: Ed25519 / ECDSA secp256k1 keys, gRPC over HTTP/2, and the Consensus, Crypto (incl. accounts), Token (HTS), File, Schedule and Smart Contract services — plus EIP-1559 Ethereum transactions and free/paid queries.
Current section
Files
Jump to
Current section
Files
lib/hedera/timestamp.ex
defmodule Hedera.Timestamp do
@moduledoc "A Hedera consensus timestamp: whole `seconds` plus `nanos`."
alias Hedera.Proto
@enforce_keys [:seconds]
defstruct [:seconds, nanos: 0]
@type t :: %__MODULE__{seconds: integer(), nanos: non_neg_integer()}
@doc "Format as Hedera's `seconds.nanos` (9-digit nanos)."
@spec to_string(t()) :: binary()
def to_string(%__MODULE__{seconds: s, nanos: n}) do
"#{s}.#{n |> Integer.to_string() |> String.pad_leading(9, "0")}"
end
@doc "Encode as a Hedera `Timestamp` protobuf message."
@spec to_proto(t()) :: binary()
def to_proto(%__MODULE__{seconds: s, nanos: n}) do
Proto.varint_field(1, s) <> Proto.varint_field(2, n)
end
end