File: tai64n.py

package info (click to toggle)
python-eliot 1.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: python: 8,641; makefile: 151
file content (45 lines) | stat: -rw-r--r-- 1,301 bytes parent folder | download
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
"""
TAI64N encoding and decoding.

TAI64N encodes nanosecond-accuracy timestamps and is supported by logstash.

@see: U{http://cr.yp.to/libtai/tai64.html}.
"""

import struct
from binascii import b2a_hex, a2b_hex

_STRUCTURE = b">QI"
_OFFSET = (2**62) + 10  # last 10 are leap seconds


def encode(timestamp):
    """
    Convert seconds since epoch to TAI64N string.

    @param timestamp: Seconds since UTC Unix epoch as C{float}.

    @return: TAI64N-encoded time, as C{unicode}.
    """
    seconds = int(timestamp)
    nanoseconds = int((timestamp - seconds) * 1000000000)
    seconds = seconds + _OFFSET
    encoded = b2a_hex(struct.pack(_STRUCTURE, seconds, nanoseconds))
    return "@" + encoded.decode("ascii")


def decode(tai64n):
    """
    Convert TAI64N string to seconds since epoch.

    Note that dates before 2013 may not decode accurately due to leap second
    issues. If you need correct decoding for earlier dates you can try the
    tai64n package available from PyPI (U{https://pypi.python.org/pypi/tai64n}).

    @param tai64n: TAI64N-encoded time, as C{unicode}.

    @return: Seconds since UTC Unix epoch as C{float}.
    """
    seconds, nanoseconds = struct.unpack(_STRUCTURE, a2b_hex(tai64n[1:]))
    seconds -= _OFFSET
    return seconds + (nanoseconds / 1000000000.0)