Why do you build the array using "1-6" instead of all the digits "1-9", or "0-9" like you use all the letters of the alphabet "A-Z"

There's really nothing significant about my choice of characters. I needed a set of 32* unique characters that could be guaranteed to sort consistently. There are 26 letters of the alphabet so I needed six more. I couldn't use both uppercase and lowercase letters because file managers will often sort case insensitively. There are a limited number of punctuation characters that are safe to use in filenames so I just went for the digits 1-6.

I could have used 0-9 and A-V, but I preferred to use fewer digits and more letters to reduce the chance of a numeric code being taken as part of the date.

Alternatively, since there are 14 bits of significant data in the code, I could have spread it out over 4 characters rather than three. Then I'd only have needed 16 unique symbols (e.g.: A-P) to represent the 4 bit payload for each character:

sub day_dir { my($day, $month, $year) = (localtime(shift || time))[3,4,5]; my $code = sprintf('%04X', (((138 - $year) & 31) << 9) + (((11 - $month) & 15) << 5) + ((31 - $day) & 31) ); $code =~ tr/0-9A-F/A-P/; return sprintf('%s-%04u-%02u-%02u', $code, $year + 1900, $month + +1, $day); }

* Eagle eyed readers will note that I really only needed 31 unique symbols but I was being conservative.


In reply to Re^2: Reverse Date Sorted Directory Names by grantm
in thread Reverse Date Sorted Directory Names by grantm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.