gritzko / ron

(dated, see the site) Replicated Object Notation, a distributed live data format, golang/ragel lib
http://replicated.cc
Apache License 2.0
360 stars 7 forks source link

Specify calendar format #19

Open cblp opened 6 years ago

cblp commented 6 years ago

Variable-precision timestamps have the MMDHmSssnn format. Ten Base64 chars encode months-since-epoch, days, hours, minutes, seconds, milliseconds and an additional sequence number. The resulting resolution is ~4mln timestamps per second, which is often excessive. It is OK to shorten timestamps by zeroing the tail (sequence number, milliseconds, etc). For example, 1CQAneD is 7 chars and 1CQAn is 5 chars (MMDHm, no seconds - Fri May 27 20:50:00 UTC 2016). We use Gregorian calendar and not milliseconds-since-epoch because Swarm timestamps are hybrid (logical, calendar-aware). Intuitive understanding of timestamps is more important for us than easy calculation of time intervals. The resulting bit loss is tolerable (no month is 64 days long).

значение последних 4 знаков поменялось - в go это сотни наносекунд, для совместимости с RFC4122: MMDhmsnnnn

actual format is specified by the Go implementation:

func DecodeCalendar(v uint64) time.Time {
    var ns100 int = int(v & MASK24)
    v >>= 24
    var secs int = int(v & 63)
    v >>= 6
    var mins int = int(v & 63)
    v >>= 6
    var hours int = int(v & 63)
    v >>= 6
    var days int = int(v & 63)
    v >>= 6
    var months int = int(v & 4095)
    var month = months % 12
    var year = months / 12
    t := time.Date(year+2010, time.Month(month+1), days+1, hours, mins, secs, ns100*100, time.UTC)
    return t
}