Is This The Most Important Script You’ll Ever Write?

Is This The Most Important Script You’ll Ever Write?

Since my first IT job ever, I’ve taken part in a tradition started by the guy I was replacing. A tradition known to us as escape.pl, and something people in my local Linux Administrator community have kept with for well over a decade now.

The Most Important Script You’ll Ever Write?

The first time I saw it it was written in Perl, I’ve since written them in Bash, Java and now Javascript. I’ve just seen one written in AWK. Some are cleverer than others, some just do the bare minimum. Some are executed by hand, others are run each time a new terminal is opened thanks to .profile, .bashrc or whatever’s managing your shell environment.

Well What Does It Do?

Quite simply the most important script you’ll ever write calculates the difference between two dates. Today, and your last day of work. The day you escape.. Where to? Who knows, perhaps a new job, a new country, either way somewhere significant!

Mine is currently a node.js app, and it has a bug, it hasn’t taken in to account a bank holiday, but it does the job.

free:~ rus$ node escape.js 
Escape in  18  days.
Working days left:  15

Another takes in to account even minutes so he knows how long he’s waiting!

16:03 < woduf> 004 days, 00 hours, 57 minutes

How complicated you want to make the most important script you’ll ever write is up to you, but the sentiment remains the same!

Examples Of The Most Important Script You’ll Ever Write

Node.JS

function workingDaysBetweenDates(startDate, endDate) {
  
    // Validate input
    if (endDate < startDate)
        return 0;
    
    // Calculate days between dates
    var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
    startDate.setHours(0,0,0,1);  // Start just after midnight
    endDate.setHours(23,59,59,999);  // End just before midnight
    var diff = endDate - startDate;  // Milliseconds between datetime objects    
    var days = Math.ceil(diff / millisecondsPerDay);
    
    // Subtract two weekend days for every week in between
    var weeks = Math.floor(days / 7);
    var days = days - (weeks * 2);

    // Handle special cases
    var startDay = startDate.getDay();
    var endDay = endDate.getDay();
    
    // Remove weekend not previously removed.   
    if (startDay - endDay > 1)         
        days = days - 2;      
    
    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6)
        days = days - 1  
            
    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0)
        days = days - 1  
    
    return days;
}

var date1 = new Date();
var date2 = new Date("08/29/2014");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
console.log("Escape in ", diffDays, " days.");

var workdays = workingDaysBetweenDates(date1, date2);
console.log("Working days left: ", workdays);

AWK

Thanks to woduf for the following awk snippet!

awk 'BEGIN { print strftime("%j days, %H hours, %M minutes", mktime("2014 08 15 17 00 00") - systime() - 89940) }'

HTML/JS

Thanks to xhae for the following HTML/JS combination!

<html>
<head>
<style type="text/css">
body {
    background-color: #8c8;
    font-size: 11em;
    color: firebrick;
}
</style>
<title>^[</title>
<script>
var then = new Date(2013, 6, 5, 17, 0, 0);
(function loop() {
    window.requestAnimationFrame(loop);
    var now = new Date(),
        s = Math.round((then - now)/1000).toString(),
        DAY = 86400,
        HOUR = 3600,
        MINUTE = 60,
        days = Math.floor(s / DAY),
        hours = Math.floor((s % DAY) / HOUR),
        minutes = Math.floor((s % HOUR) / MINUTE),
        seconds = Math.floor(s % MINUTE);
    document.body.innerHTML = "T-" + days + ":" + hours + ":" + minutes + ":" + seconds;
})();
</script>

Perl

Thanks Huw!

#!/usr/bin/perl -w</code>

use strict;
use Date::Manip;

my $leave = "14th June 2013";
my $escape_date = ParseDate($leave);
my $day = ParseDate("today");
my $count = 0;

do {
$count++;
$day = DateCalc($day, "+ 1 day");
} while (Date_Cmp($day, $escape_date) &lt; 0); print "Only $count day",$count&gt;1?"s":""," to go\n\n";

Bash

Thanks woduf. Note, this works fine on Linux/GNU but not so fine on Mac/BSD

d=$(($(date +%s -d '2013/09/27 17:00:00') - $(date +%s))); echo "Escape in $((d / 86400)) days, $((d % 86400 / 3600)) hours"

Have you ever written an escape.pl? Whether you love your job or hate it, changing job can be a significant life decision and it helps to add fun to the seriousness of it! Please send me examples of your most important script and I’ll add it to the list!

Leave a Reply

Your email address will not be published. Required fields are marked *