Day 4: Date calculator shell (App::datecalc)

About the series: perlancar's 2014 Advent Calendar: Introduction to a selection of 24 modules which I published in 2014. Table of contents.

My habit for the past decade or so, back when I started learning Ruby, is to use irb (the interactive Ruby shell) as a calculator. I guess one can also use python, but irb was the one that stuck in my muscle memory. perl does have the interactive debugger perl -dee since forever, but by default it does not print the result of expressions. I am recently trying to change that habit though and use Reply instead. It's nice, with colors and all (especially with the DataDumpColor plugin).

Sometimes though, instead of numeric arithmetics or string operations, I want to do some date calculations. For example, what is the date (and day of week) 20 days from today? What's 2013-09-01 plus 40 weeks?And so on. Having to write a short script e.g. loading DateTime and creating DateTime object and all that seem so cumbersome.

Thus, I created datecalc (distributed in App-datecalc), a shell dedicated to do date calculations. You can write unquoted date literals like 2014-12-04 or today or yesterday as well as date interval literals like 1 day, 40 weeks 3 days, or P40W3D. You can add/subtract dates, multiply date intervals, and so on. Examples:

% datecalc
> today
> today + 21 day
> month(tomorrow)
> days(P4D)

1 Comment

Messing with it now. Very cool!

Leave a comment

About perlancar

user-pic #perl #indonesia