Of Dates, and Sigs, and Shiny Things (and cabbages and kings)

You know, I’ve been trying to analyze my working patterns lately, and I think I’ve hit on something.  Looking back over the past few years, it seems like I get obsessed with one particular project, work frantically on it and produce lots of great stuff, then I get distracted and next thing I know I’m obsessing over an entirely different project.  And then sometimes I circle back around to the first project, but it’s usually a long time later.  I’m actually starting to wonder if maybe I have some undiagnosed ADHD (not only because of the easily distracted, but also because of the hyperfocus, and SQUIRREL!).  Anyways, that appears to be the pattern of my life, so I think at this point I’m just going to have to learn to roll with it.

Now, why would you care about all this?  Well, most probably you don’t.  And that’s fine.  But insofar as you might care, you might care because some of those projects are Perl projects, and many of those Perl projects are CPAN modules (or will be someday (assuming I don’t get distracted (again (sort of like this sentence)))).  For instance, many of you first heard of me in connection with my work on Method::Signatures, which there was quite a lot of, back in the day.  In that particular case, I was aided not only by becoming obsessed with making signatures work in Perl, but also because I could spend $work time on it, which radically increases the amount of code I can crank out.  So I was able to do a lot with MSig, over the course of about a year, before I got distracted by other shiny things, and that was nice, but now it’s feeling a bit neglected and needs some love.1  For instance, I just now noticed that I released a trial version over six months ago that I just plain forgot to promote to a full release.  So ... sorry about that (and it should be there now).  Not much new there except a few compatibility features with native signatures, but still.

Of course, not all my passion projects are Perl.  The past several weeks I’ve been working on a Google Sheets (with Google App Script) app which you can read all about on my Original Blog, if you’re so inclined.  The short answer is that it’s gaming related (specifically D&D), but still a programming project.  Not all my projects involve coding, of course.  I also do a fair amount of writing, editing, making music mixes, gaming of one sort or another, etc.  But a lot of the time when I disappear into a temporary obsession, it’s programming, and nearly always, when it’s programming, it’s Perl.

Last year I disappeared for several months into a nifty idea: Date::Easy.  Well, at least I thought it was a nifty idea.  And I was fortunate enough to hear from several of you folks at last year’s YAPC, and most of those who had an opinion seemed to think it was a nify idea too.  Now, I haven’t had time to work on Date::Easy very much in the past few months—too much distracted by other projects—but I have tried to use it here and there, mainly to see where it worked well, and where it had gaps that needed filling.

So, just to let you know that I’ve only been taking a brief break and not abandoning the concept entirely, today I have for you two things.

The first is a small update: Date::Easy version 0.02_01-TRIAL is on CPAN now and getting very good preliminary results from CPAN Testers, so I hope to promote it to 0.03 within the next few days.  It doesn’t add very much to Date::Easy’s repertoire, but those few things it does add are among those I found most useful to add for my own purposes:

  • The one bit of math you couldn’t do fairly easily (that is, by adding appropriate numbers of days or seconds) was to add one or more months to a date or datetime.  Of course, the underlying Time::Piece can do that, so just passing the add_months method call through was trivial enough.
  • If you wanted an ISO 8601 formatted date (and you couldn’t remember the exact strftime format), you formerly had to do this: say now->as('Time::Piece')->datetime.  Well, that’s just silly.  Now you can do this: say now->iso8601 (or even this: say now->iso, if you value brevity over clarity2).
  • I rapidly got tired of writing say $date->strftime('%Y/%m/%d').  So now you can just write: say $date->as('/Ymd').  Or $date->as('-Ymd'), or $datetime->as(':HMS'), or what-have-you.

The second thing I have for you is a few examples of how Date::Easy has made my life easier, and how I hope it could make yours easier as well.

First, here’s some code I wrote a long time ago to do some basic date wrangling from a command-line argument:

use Date::Format;
use Time::ParseDate qw<parsedate>;
my $today = time2str('%Y/%m/%d', scalar parsedate($arg // time()));

(The actual code is a bit more spread out than that, but that’s the executive precis.)

This code is a bit chatty, and also it’s not entirely clear how or why it works, unless you know a bit about the various time and date modules and what those function calls are actually doing.  Now contrast that with what I was able to replace that with:

use Date::Easy;
$today = date($arg // today)->as('/Ymd');

Now, this code is simple, concise, and it’s pretty clear exactly what it does, even if you don’t know everything about how Date::Easy works.  Okay, sure: the new trick with as might not be immediately obvious, but it’s not too unlikely that you could work it out with a bit of thinking.

Next, here’s a little trick I ran across a couple months ago.  I was poking around in the Xorg.0.log file.  It’s full of lines that look something like this:

[2253463.887] reporting 7 7 37 280

Now, you may be wondering (as I was), what in the world “2253463.887” is.  As it turns out, it’s the number of seconds since the last time my laptop was rebooted.  (Thank you, Google.)  Which is a pretty insane way to express time, if you ask me.  But it is what it is.  The big question is, how do we turn it into something useful?

Well, with Date::Easy, it wasn’t actually that hard ...

perl -MDate::Easy -E 'say datetime(`uptime -s`) + shift' 2253463.887
Sat Feb 11 00:44:14 2017

Pretty nifty, eh?


Finally, here’s my favorite.  This takes a bit of setup, but bear with me.

A while back, I set up a playlist of songs that were exactly 5 minutes long each.3  I use it to wake up by, instead of an annoying alarm clock, and it’s long enough that I let it play the whole time I’m getting ready for work.  That way, I always know what time it is just by which song is currently playing.  Except that I’ve now gotten to the point where the songs correspond more to abstract times, like “time for me to be getting in the shower” or “time for me to be heading out the door!” and not so much to real clock times.  I often found myself rifling through my playlist and counting on my fingers to figure out what time it was (or will be) when such-and-such song was playing.

But then it occurred to me: this is the sort of thing that Date::Easy can do for me very easily.  All I have to do is start with a datetime corresponding to five minutes before the tracklist starts playing—i.e. 10:10am4then just add 5 times 60 seconds for each song.  The songs, of course, are already in a playlist file, so all I need to do is loop through that file and add a time at the beginning of each line.  I can also remove the directories of the full paths to the MP3 files, and their extensions, since that’s not really adding any information here, and I can actually do all this with a one-liner:

perl -MDate::Easy -pe 'BEGIN { $d = datetime("10:10") } s|.*/| : |; s/.mp3//; s/^/($d += 5*60)->as(":HM")/e' tracklists/misc/5minutes.m3u
10:15 : The Police - King of Pain
10:20 : Nine Inch Nails - Head Like a Hole
10:25 : Spin Doctors - How Could You Want Him (When You Know You Could Have Me?)
10:30 : Tori Amos - Crucify
10:35 : KT Tunstall - Hallowed Ground
10:40 : The The - This Is the Day
10:45 : Styx - Snowblind
10:50 : Simple Minds - Sanctify Yourself
10:55 : Berlin - When We Make Love
11:00 : Billy Idol - Eyes Without a Face
11:05 : Dave Matthews Band - Tripping Billies
11:10 : The Dukes of Stratosphear - What in the World??...
11:15 : The Wallflowers - Three Marlenas
11:20 : Pearl Jam - Garden

Which is not too shoddy, if I do say so myself.5


Anyway, that’s what I have for you today.  I’m not sure what next big project will steal my focus, but I’ll still keep chipping away at Date::Easy, and other things as well.  And I’ll keep checking in with you guys to see if you’re finding this stuff as useful as I am.

(Next up in the Date::Easy series: finally getting around to stealing the last of the Date::Piece interface.)



__________

1 For anyone who’s interested in volunteering, the main thing it needs right now is a bunch of tests around Moose, Mouse, and Moo typing, to make sure that just dropping in Type::Tiny isn’t going to break anything.


2 Or if you just can’t remember the exact digits of the ISO specification.  Like I usually can’t.


3 Or at least within a second of that time.  As it turns out, finding those songs amidst my jumbled masses of MP3s was its own programming project, which is probably worthy of its own blog post.  Some day.


4 Yes, I’m very much a night person, and also I’m very thankful to my wonderful employers, who allow me to set my own hours and sleep in until that time.


5 Also somewhat revealing of my musical tastes, I suppose.  But I’m okay with that.


Leave a comment

About Buddy Burden

user-pic 14 years in California, 25 years in Perl, 34 years in computers, 55 years in bare feet.