Acme Comes Through Again

Well was programming away today (after doing 8 hours of unpaid household chores) and I had to do the old place a file in a dir but make sure you do not overwrite the last one trick.

There are of course all sorts of ways to do this but the most common is to use the current time in some way and add it into the file name. The good old Perl 'time' function is the quick and dirty way as it gets a slew of numbers any you can just tack it on the end like this

my $time = time;
my $file ="somefile.".$time;

Of course that will work but does give you rather ugly file extension and it is always a
nasty pain to convert it over to something that is nice to look at and easily readable.

Time in general must be a constant thorn in every Perl programmer side as there are well over 90 'Time' packages on CPAN and who has time to look at them all and figure out which one works best.

However I do remember one that I liked that has nothing to do whit that namespace and that was good old 'ACME::Stardate'

So 5 mins of poking about and looking at the contents of the .pm I had exactly what I wanted

use POSIX 'strftime';
my $file ="somefile".strftime("%Y%m%d.", gmtime). int(time%86400/86400 * 100000)

No need to go to Google and type a search which would most likely lead to the same answer as above but just done on Perl Monks

So it does pay to actually read the code in Acme


1 Comment

Nice idea. As a variant, you could use

my $file ="somefile".strftime("%Y%m%d.%H%M%S", gmtime)
for a more "human readable" time-of-day :)

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations