Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Time Encoding

Tags:

c#

.net

Any ideas or implementations floating about for encoding the current date including the milliseconds into the shortest possible string length?

e.g I want 31/10/2011 10:41:45 in the shortest string possible (ideally 5 characters) - obviously decodable.

If it is impossible to get down to 5 characters, then the year is optional.

edit: it doesn't actually need to be decodable. It just needs to be a unique string.

like image 204
user880954 Avatar asked Mar 20 '26 10:03

user880954


2 Answers

An time_t is 31 bits. Add 10 bits for up to 1000 milliseconds: That's 41 bits. You want 5 characters: That's 8 bits for the 1st 4 characters + 9 bits for the last one. Using Chinese ideograms, you should easily be able to find a range of 256 consecutive chars for each of the 1st 4 chars and a range of 512 for the last one.

Needless to say your encoded date will look... chinese! But it should do the trick ;-)

BTW, you don't have to stick to Chinese. You might even want to choose a different Unicode 256 chars range for each character. Of course, you'll want to find sequences of 256/512 printable chars.

Now let's say we skip the year. We're down to 86400 x 366 seconds per year = 31622400 seconds. Including millisecs : 31622400000. That's 35 bits. Great: We're down at 7 bits per character. Easy! :-)

like image 82
Serge Wautier Avatar answered Mar 21 '26 22:03

Serge Wautier


you can use the Ticks:

var ticks = System.DateTime.Now.Ticks;

this is a 64bit number. You get the Time back by calling:

var timeBack = new System.DateTime(ticks);

of course this are 8 bytes but I don't think you can get this more compact (easily).

like image 44
Random Dev Avatar answered Mar 21 '26 22:03

Random Dev