Date Macro?

Have a problem? A question? This is the place for answers from other Express users.
Post Reply
Mr Tea
Posts: 5
Joined: 2004-11-01 14:50:56

Date Macro?

Post by Mr Tea »

I'd like to be able to insert the current date into a document in this format: November 1st, 2004. How to do this with a macro?

I did take a look at the 'Date Stamp' macro that comes supplied, but I'm ignorant in the ways of Perl and couldn't for the life of me figure out how to adapt it to my needs. Any help?

And while I'm here, is there any way I can put a 'live' date (ie, one that stays current) in a Nisus document, as is possible with AppleWorks?
"Always remember to warm the pot."
midwinter
Posts: 333
Joined: 2004-09-09 18:07:11
Location: Utah
Contact:

Post by midwinter »

I would really like to see how to do this as well.
Mr Tea
Posts: 5
Joined: 2004-11-01 14:50:56

Cracked it!

Post by Mr Tea »

The lack of response here to my original question has been disappointing, considering that macros are a major feature/selling point for NWX.

So I rolled my own date formatting macro to generate a simple date suitable for inserting into letters, etc. Aside from trawling the web for code examples, I've also plunged in and installed an extra Perl Mod into the standard OS X configuration. It's called 'Lingua::EN::Numbers::Ordinate' and it's used here to convert the cardinal 'day of the month' value (eg, 1, 2, 30) to an ordinal (eg, 1st, 2nd, 30th). For a complete Perl novice, the easiest way to install turned out to be through the CPAN function built into Perl, which does most of the dirty work for you. Typing 'cpan install Lingua::EN::Numbers::Ordinate' seemed to do the trick (but be prepared for CPAN to go through a lengthy 'first-run' process). Here's the macro:


#Nisus Macro Block
#destination clipboard
#source none
#Before Execution
#Clipboard 1
#After Execution
#Paste
#Clipboard 0
#End Nisus Macro Block

use strict;
use POSIX; # needed for 'strftime'
use Lingua::EN::Numbers::Ordinate;
my $month_name = strftime( "%B ",localtime(time));
my $year_number = strftime( ", %Y ",localtime(time));
my $day_number = strftime( "%e",localtime(time));
if ($day_number<10) {$day_number = substr($day_number, -1)};
my $day_ord = ordinate($day_number);
my $formal_date = $month_name . $day_ord . $year_number;
print $formal_date;

The date info is obtained, chunk by chunk, using 'strftime', which offers a huge range of options for generating date & time formats. For the day of the month, single digit dates (ie 1st to 9th) are inconveniently padded with a leading zero (using '%d') or a space (using '%e'). When this is the case, the 'if...' line strips the unwanted extra space (or zero). After the ordinate command has processed the day number, the completed date string is finally assembled using the 'dot' concatenator, ready for insertion into a document.

Phew. That was a monstrous ordeal of trial and error and stumbling around in the dark. I don't know if I should be thanking Nisus for forcing me to get to grips with Perl, or cursing them for not including a basic configureable 'insert date' function.

Where is the online repository for useful NWX macros?
"Always remember to warm the pot."
Post Reply