nips icon indicating copy to clipboard operation
nips copied to clipboard

Why is the protocol using unix timestamps?

Open TiMESPLiNTER opened this issue 5 months ago • 3 comments

I just read through the different NIPs and it seems that dates are often (always?) represented as unix timestamps? What's. the reason for this choice? Wouldn't it be nicer to have a RFC-3339 formatted string as date(/time)? That could have some more context like timezone, etc. and would be better readable by human eyes as well. (Not that this is something very important but an additional benefit.)

TiMESPLiNTER avatar Feb 17 '24 13:02 TiMESPLiNTER

UTC keeps logic much simpler, timezones are a nightmare. Human readable would be nice, but an int is more compact, and can be used without a library. I think it's a good choice.

staab avatar Feb 17 '24 14:02 staab

Unix timestamps are far easier. Clients can convert these into RFC-3339 to show to the user.

Plus, we cannot change that part of the protocol this late in the game even if we wanted to.

But while we are on the subject, I'll say that if we ever started over, we should use TAI seconds instead. Because Unix timestamps have no concept of leap seconds. There have actually been 27 more seconds than the unix timestamp indicates (which pretends every day is exactly 86400 seconds), and operating systems have slowed down their clocks to allow an additional second to elapse in order to pretend they didn't happen.

mikedilger avatar Feb 18 '24 20:02 mikedilger

I don't know why exactly it is a unix timestamp, but I am glad that it is. Integers are much easier to parse and work with than strings for dates. Consider this - incomplete - C struct:

struct nostr_event {
  char[] id;
  char[] sig;
  char[] author;
  long int created_at;
};

You can now use the timestamp to work with struct timespec. Those operations are all purely integer-based. Whilst in most cases you wouldn't notice the difference, on a relay, where you are processing thousands of events at any moment, this would add up and actually introduce an overhead - because most databases store times as integers anyway.

So, frankly, whilst it isn't much, it does definitively help. :)

IngwiePhoenix avatar Mar 25 '24 06:03 IngwiePhoenix

if timezone is needed/desired it can just be spec'd as a tag

jb55 avatar Apr 18 '24 18:04 jb55

I think the question was sufficiently answered.

Giszmo avatar Apr 24 '24 15:04 Giszmo