Data::Format::Pretty::Console just got a bit prettier - dates

One of the small annoyances I have when displaying data is with dates (timestamps), which some APIs produce.

$ perl -MData::Format::Pretty::Console=format_pretty \
  -e'print format_pretty([map {{msg=>"msg$_", send_date=>1342610186+$_*12345}} 1..3])'
.-------------------.
| msg  | send_date  |
+------+------------+
| msg1 | 1342622531 |
| msg2 | 1342634876 |
| msg3 | 1342647221 |
'------+------------'

Often I have to copy paste the timestamp to irb and do something like 'Time.at(X)'.

No more. The latest release of Data::Format::Pretty::Console detects column names and automatically format the dates for you.

$ perl -MData::Format::Pretty::Console=format_pretty \
  -e'print format_pretty([map {{msg=>"msg$_", send_date=>1342610186+$_*12345}} 1..3])'
.----------------------------.
| msg  | send_date           |
+------+---------------------+
| msg1 | 2012-07-18 21:42:11 |
| msg2 | 2012-07-19 01:07:56 |
| msg3 | 2012-07-19 04:33:41 |
'------+---------------------'

When column name is not indicative, you can explicitly tell the module to format it as date, either using the formatting option table_column_formats, or environment variable when that is more convenient:

$ FORMAT_PRETTY_TABLE_COLUMN_FORMATS='[{"sent":[["date",{"format":"%Y-%m-%d"}]]}]' \
  perl -MData::Format::Pretty::Console=format_pretty \
  -e'print format_pretty([map {{msg=>"msg$_", sent=>1342610186+$_*12345}} 1..3])'
.-------------------.
| msg  | sent       |
+------+------------+
| msg1 | 2012-07-18 |
| msg2 | 2012-07-19 |
| msg3 | 2012-07-19 |
'------+------------'

Of course, when you need the raw data, you can still get it.

$ perl -MData::Format::Pretty::Console=format_pretty \
-e'print format_pretty([map {{msg=>"msg$_", sent=>1342610186+$_*12345}} 1..3])'|cat
msg1    1342622531
msg2    1342634876
msg3    1342647221

BTW, I'm planning a whole new level of prettiness, from colors (and color themes) to ANSI extended characters, from avoiding table wrapping to various other autoformatting, from locale awareness to indenting/nested tables. Now to find time for it all :)

2 Comments

Is this in Data::Format::Pretty or specifically in ::Console? I.e. will it not apply in ::HTML?

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.