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