Camel
Peter
Peter Campbell Smith

Base 2 dates and odd words

Weekly challenge 332 — 28 July 2025

Week 332: 28 Jul 2025

Task 1

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

Try it 

Try running the script with any input:



example: 2025-08-10

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));
}


Output


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