-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtimestamp.pb.ex
More file actions
50 lines (37 loc) · 1.43 KB
/
timestamp.pb.ex
File metadata and controls
50 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
defmodule Google.Protobuf.Timestamp do
@moduledoc false
use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"
field :seconds, 1, type: :int64
field :nanos, 2, type: :int32
@doc """
Converts a `DateTime` struct to a `Google.Protobuf.Timestamp` struct.
Note: Elixir `DateTime.from_unix!/2` will convert units to
microseconds internally. Nanosecond precision is not guaranteed.
See examples for details.
## Examples
iex> Timestamp.to_datetime(%Timestamp{seconds: 5, nanos: 0})
~U[1970-01-01 00:00:05.000000Z]
iex> one = Timestamp.to_datetime(%Timestamp{seconds: 10, nanos: 100})
...> two = Timestamp.to_datetime(%Timestamp{seconds: 10, nanos: 105})
...> DateTime.diff(one, two, :nanosecond)
0
"""
@spec to_datetime(__MODULE__.t()) :: DateTime.t()
def to_datetime(%{seconds: seconds, nanos: nanos}) do
DateTime.from_unix!(seconds * 1_000_000_000 + nanos, :nanosecond)
end
@doc """
Converts a `Google.Protobuf.Timestamp` struct to a `DateTime` struct.
## Examples
iex> Timestamp.from_datetime(~U[1970-01-01 00:00:05.000000Z])
%Timestamp{seconds: 5, nanos: 0}
"""
@spec from_datetime(DateTime.t()) :: __MODULE__.t()
def from_datetime(%DateTime{} = datetime) do
nanoseconds = DateTime.to_unix(datetime, :nanosecond)
struct(__MODULE__, %{
seconds: div(nanoseconds, 1_000_000_000),
nanos: rem(nanoseconds, 1_000_000_000)
})
end
end