开发者

Date Time Encoding

开发者 https://www.devze.com 2023-04-02 21:35 出处:网络
Any ideas or implementations floating about for encoding the current date including the milliseconds into the shortest possible string length?

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 th开发者_开发百科e 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.


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! :-)


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).


No can do: The total ms in an year (365 days) is 31,536,000,000 (=365*24*60*60*1000). You need 34.87628063 bits of information to store that value (log2 31,536,000,000). You probably meant "printable characters" BUT you would need 7 bits/character to store 35 bits in 5 characters. As an example base64 is 6 bits/character of information, so 6 characters. Ascii85 would be a little better, but still you would need around 5.5 characters, so 6 characters.

Clearly if you meant 5 BYTES, everything changes. You can store 34.84 years (in ms) in that space.

And if you meant 5 C# PRINTABLE AND UNPRINTABLE CHARACTERS (each C# character is 16 bits), then it's even better. 10 bytes! DateTime in C# is only 8 bytes and it uses ticks (they are a VERY VERY VERY small part of a second)!

BUT if you meant 5 C# PRINTABLE CHARACTERS characters, then use Serge's response. It's very good and show us that the world is a big place (and show us that why good questions are so much important: they let us see the world in new ways).


You can use ASCII characters to represent the numbers and drop the formatting, for example:

31/10/2011 10:41:45

*/*/** *:*:*

*******

That's 7, you can drop 2 if you don't want to include the full year. Obviously the * are actual characters relating to a number, A could be 1 etc, or even use the proper ASCII codes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号