A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label format. Show all posts
Showing posts with label format. Show all posts

MySQL date display format conversion

MySQL date time conversion functions: 

  • UNIX_TIMESTAMP(date) docs
  • FROM_UNIXTIME(epoch) docs

Examples:

select name, from_unixtime(time_added, '%Y-%m-%d %h:%i')

from company

order by date_added desc

limit 10; -- list the most recently added companies

You may also omit the '%Y-%m-%d %h:%i' format string, to get the default format YYYY-MM-DD HH:MM:SS, e.g. `2023-09-07 09:43:51`


Perltidy docs / example config

Docs

Reference for all the possible perltidy options
More detailed explanation of some the options

Team perltidy options

ruleexplanationnotes
-i=4
4 column indentA lot of the legacy code has 2 column indent.
-pt=2
"horizontal tightness" 2 - no space for parenthesis tokens
 if ( ( my $len_tab = length( $tabstr ) ) > 0 ) {  # -pt=0
 if ( ( my $len_tab = length($tabstr) ) > 0 ) {    # -pt=1 (default)
 if ((my $len_tab = length($tabstr)) > 0) {        # -pt=2
(docs)
-wls='=>'
ensure there's always space to the left of =>
-wrs='=>'
...and always to the right of =>
-vtc=0
always break before a closing token (default)i.e. non-block curly braces, parentheses, and square brackets docs
-l=79
Lines should be no more than 79 characters wide
-nolc
no "outdenting" for long comments - indent them properlyBy default, full-line (block) comments longer than the value maximum-line-length will have their indentation removed ("outdented"). This rule prevents that. docs
-nolq
no "outdenting" for long quotes/stringsdocs
-sot
stack opening tokens - same as -sop -sohb -sosbPut opening parentheses, braces, etc. on the same line docs
-sct
stack closing tokensPut closing parentheses, braces, etc. on the same line docs

Options that were removed

ruleexplanationnotes
-lp
line up parenthesesSee docs
-vt=2
define opening vertical tightnessPut opening parentheses, braces, etc. on the same line. Related to -lp docs
-vt=0
always break a line after opening tokendocs
-st -se
use as a filterInstructions to the parser docs
-nsfs
'for' loop semicolon spaces
The default is to place a space before a semicolon in a for statement, like this:
    for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) {  # -sfs (default)
If you prefer no such space, like this:
    for ( @a = @$ap, $u = shift @a; @a; $u = $v ) {    # -nsfs
then use nsfs.
-nbbc
No blanks before commentsBy default, a blank line will be introduced before a full-line comment. This rule prevents that.

Outlook 2007: Don't remove line breaks

Microsoft :'-(

Outlook 2007:
Tools | Options | Preferences | E-mail Options
Untick "Remove extra line breaks in plain text messages"

(source)

Split one long line into shorter lines

Command:

fold file_with_long_lines.txt

...prints to STDOUT

Don't indent so much

TL;DR: Don't indent over more than one page.

When programming, please factor your subroutines enough. One test for this is the level of indentation. If you find the code is indented for longer than one page (i.e. around 40-50 lines), then split that indented code out into its own subroutine.

This applies to loops, if-then statements, try-catch blocks, and given-when blocks. Especially when any combination of those are nested over several pages, it makes the code very difficult to follow.

Anything that indents the code indicates a logical grouping which you may find fits happily into a new subroutine. You can even give the subroutine a meaningful name which will serve as a signpost to future maintainers (not to mention a useful checkpoint to assert that variable contents are correct by validating the subroutine arguments).

How to paste a table into a thunderbird email with correct formatting

How:
  • Copy table from web page
  • Paste into OpenOffice Calc (Excel)
  • Copy from Calc
  • Paste into email
done.

Easiest way to find yesterday's date in Perl

perl -le'my ($year, $month, $day) = (localtime(time - (1 * 24 * 60 * 60)))[5,4,3]; $year += 1900; $month++; printf("%d-%02d-%02d", $year, $month, $day)'

Text file endings

Mac OS: \r
Unix: \n
Windows: \r\n

Perl sprintf

        # Format number with up to 8 leading zeroes
$result = sprintf("%08d", $number);
        # Round number to 3 digits after decimal point
$rounded = sprintf("%.3f", $number);

Perl's sprintf permits the following universally-known conversions:

   %% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
%g a floating-point number, in %e or %f notation