Published on

# Zhu Li Timestamp

Authors

## Motivation ​

The Unix timestamp, uses an 32 bit signed integer to represent a point in time. However, it has 3 problems:

1. The precision is a too low, it can only record seconds
2. The range of time is very limited, from 13 December 1901 (at 20:45:52 UTC) to 19 January 2038 (at 03:14:07 UTC)
3. It has an Epoch that is arbitrary

Zhu Li's timestamp is designed to communiate time between different networked machines accurately and efficiently. It can be efficiently serialized and deserialized because it is an integer, instead of a ISO date string. It is also easily comparable, between two timestamps.

## Definition ​

Number of seconds passed since 0:00 UTC on 1 January 1 times ${2}^{16}$

Epoch: 0:00 UTC on 1 January 1

Precision: $\frac {1} {2^{16}}$s or Aproximatly 15µs

Type: 64 bit signed integer

Range: Aproximatly 4.5 million years before and after epoch

## Tools ​

### Now ​

The time is Wed Sep 06 2023 11:03:37 GMT+0800 (China Standard Time)
The Zhu Li's Timestamp is 253719413874950 for now

### From date to timestamp ​

is 253719413874950

### From timestamp to date ​

is 2023-09-06T03:03:37.379Z

## Implementation ​

reference implementation

Python
from datetime import datetime, timezone, timedelta

epoch = datetime(1, 1, 1, tzinfo=timezone.utc)

def datetime_to_zhuli_timestamp(date:datetime) -> int:
"""
convert datetime to zhuli timestamp

Returns
-------
an integer represents zhuli timestamp
"""
return round((date - epoch).total_seconds() * (1 << 16)))

def datetime_from_zhuli_timestamp(timestamp: int) -> datetime:
"""
convert zhuli timestamp to zhuli datetime

Returns
-------
a datetime object
"""
return epoch + timedelta(seconds=timestamp/(1 << 16))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))
from datetime import datetime, timezone, timedelta

epoch = datetime(1, 1, 1, tzinfo=timezone.utc)

def datetime_to_zhuli_timestamp(date:datetime) -> int:
"""
convert datetime to zhuli timestamp

Returns
-------
an integer represents zhuli timestamp
"""
return round((date - epoch).total_seconds() * (1 << 16)))

def datetime_from_zhuli_timestamp(timestamp: int) -> datetime:
"""
convert zhuli timestamp to zhuli datetime

Returns
-------
a datetime object
"""
return epoch + timedelta(seconds=timestamp/(1 << 16))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))

def zhuli_timestamp_now() -> int:
"""
get zhuli time stamp of now

Returns
-------
zhuli timestamp that represents now
"""
return datetime_to_zhuli_timestamp(datetime.now(tz=timezone.utc))
C#
using System;

namespace Zhuli
{
public static class ZhuliTimestamp
{
public static readonly DateTime Epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public const long TicksPerSecond = 1 << 16;
private const long ToTimeSpanTicks = TimeSpan.TicksPerSecond / TicksPerSecond;

public static long FromDateTime(DateTime dateTime)
{
return (dateTime - Epoch).Ticks / ToTimeSpanTicks;
}

public static DateTime ToDateTime(long timestamp)
{
return Epoch + new TimeSpan(timestamp * ToTimeSpanTicks));
}

public static long Now()
{
return FromDateTime(DateTime.UtcNow);
}
}
}
using System;

namespace Zhuli
{
public static class ZhuliTimestamp
{
public static readonly DateTime Epoch = new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
public const long TicksPerSecond = 1 << 16;
private const long ToTimeSpanTicks = TimeSpan.TicksPerSecond / TicksPerSecond;

public static long FromDateTime(DateTime dateTime)
{
return (dateTime - Epoch).Ticks / ToTimeSpanTicks;
}

public static DateTime ToDateTime(long timestamp)
{
return Epoch + new TimeSpan(timestamp * ToTimeSpanTicks));
}

public static long Now()
{
return FromDateTime(DateTime.UtcNow);
}
}
}