Search code examples
c#c#-4.0compressioncode-golf

Convert DateTime to shortest possible version number (for url)


Challenge: convert a 'modified date' DateTime of an image file to a version number / string suitable for maintaining uniqueness in a url, so each modification of the image generates a unique url, the version number/string to be as short as possible.

The shortness of code is secondary to the shortness of number/string Apologies if this does not really qualify for code-golf status :-)

Requirements

  • C#, .Net framework v4
  • output must be valid characters for a folder-name in a url.
  • DateTime precision can be reduced to nearest minute.

EDIT: this is not entirely theoretical / Puzzle, so I guess I'd rather keep it here than on code-golf stack exchange?


Solution

  • Use the DateTime.Ticks property and then convert it to a base 36 number. It'll be very short and usable on a URL.

    Here's a class for converting to/from Base 36:

    http://www.codeproject.com/KB/cs/base36.aspx

    You could also use base 62, but not base64 since one of the extra digits in base 64 beyond numbers and letters is + which needs to be url encoded and you said you wanted to avoid that.