Tcl tips: timestamps

When is it time to pull Tcl out of the tool chest? Time calculations make for one compelling answer.

I don't write production code in Tcl now. I will again, doubtless, some day; although it's the single language in which I'm probably now most expert, I simply don't happen to have any deliveries contracted in it at the moment. Over the last week, I've written a lot of Python, and modest amounts of Java, C, JavaScript, /bin/sh, and Fortran for applications that customers see. Maybe there were others, too; I haven't bothered to check my records deeply.

I still program often in Tcl, though. Lately, I've been looking at a lot of logs. While Cody Powell is prone to exaggerate a bit, it's not more than a bit: applications that end up in users' hands truly do benefit from "... thousands of hacked-up log lines for the most trivial operations ..." Logs are fascinating, 'cause they reflect real end-user behavior, and that has an inexhaustible capacity to surprise.

What does this have to do with Tcl? Tcl, Perl, Ruby, Python, Lua, and so on are all essentially the same language from an only slightly-elevated perspective: they're all succinct, procedural, more-or-less adequate at co-operation with other languages and systems, relatively light-weight, and quick to install. I already mentioned earlier that timestamping, in particular, is something that all languages need to and can do. Why reach for Tcl when Perl or Python have perhaps an order-of-magnitude more popularity?

Leaving aside domain-specific showcases like Expect, one of Tcl's gems is its time arithmetic. Long-time Tcl wizard Kevin Kenny rewrote the internals a few years ago, while preserving its backward compatibility; Tcl reliably calculates, "with accuracy in the tens of microseconds", such arcana as "what is the second Saturday after a particular timestamp?". Yesterday's date is this terse:

clock format [clock scan yesterday]]
knows the Japanese calendar, and even correctly handles Argentina's notoriously confusing timezones (which turn up in Powell's article, too, in no surprise to those who've spent quality time unraveling international error reports).

When I'm studying gigabytes of logfiles, I need small slices of "exploratory" programming to help confirm or reject hypotheses about what I'm seeing. These scripts are largely disposable. I could write them in almost any language, because essentially all modern languages have basic facilities for I/O, textual pattern-matching, arithmetic, and so on. I usually prefer Tcl, though, and its

is one of the reasons.

I'll return next week with code that shows another Tcl strength: concurrency.

ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon