Peter’s blog ✴ Week 332 ✴ 28 July 2025

THE WEEKLY CHALLENGE
Base 2 dates and odd words

The Perl Camel

Task 1

Binary date

You are given a date in the format YYYY-MM-DD. Write a script to convert it into binary date.

Examples


Example 1
Input: $date = '2025-07-26'
Output: '11111101001-111-11010'

Example 2
Input: $date = '2000-02-02'
Output: '11111010000-10-10'

Example 3
Input: $date = '2024-12-31'
Output: '11111101000-1100-11111'

Analysis

This is my first solution requiring no variables and no lines other than those printing the input and output.

sprintf is your friend.

Slightly interesting to know that the last yyyy-mm-dd which is all 1s in binary is 8191-07-31, and the last with an alternating 1010... sequence is 2730-02-21.

Perl Weekly’s review

from Perl Weekly issue 732

A strong, idiomatic Perl solution to both problems—optimized, correct and pleasantly readable. This write-up reflects deep Perl familiarity and attention to corner cases.

Try it 

Try running the script with any input:



example: 2026-05-19

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2025-07-28
use utf8;     # Week 332 - task 1 - Binary date
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

binary_date('2025-07-28');
binary_date('1947-11-09');
binary_date('8191-07-31');
binary_date('2730-02-21');

sub binary_date {
    
    say qq[\nInput:  \$date = '$_[0]'];
    say qq[Output: ] . sprintf("'%b-%b-%b'", 
        substr($_[0], 0, 4), substr($_[0], 5, 2), 
        substr($_[0], 8, 2));
}


5 lines of code

Output from script


Input:  $date = '2025-07-28'
Output: '11111101001-111-11100'

Input:  $date = '1947-11-09'
Output: '11110011011-1011-1001'

Input:  $date = '8191-07-31'
Output: '1111111111111-111-11111'

Input:  $date = '2730-02-21'
Output: '101010101010-10-10101'

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain