[1578] | 1 | #!/usr/bin/perl |
---|
| 2 | # ------------------------------------------------------------------------------ |
---|
| 3 | # NAME |
---|
| 4 | # Fcm::Timer |
---|
| 5 | # |
---|
| 6 | # DESCRIPTION |
---|
| 7 | # This is a package of timer utility used by the FCM command. |
---|
| 8 | # |
---|
| 9 | # COPYRIGHT |
---|
| 10 | # (C) Crown copyright Met Office. All rights reserved. |
---|
| 11 | # For further details please refer to the file COPYRIGHT.txt |
---|
| 12 | # which you should have received as part of this distribution. |
---|
| 13 | # ------------------------------------------------------------------------------ |
---|
| 14 | |
---|
| 15 | package Fcm::Timer; |
---|
| 16 | |
---|
| 17 | # Standard pragma |
---|
| 18 | use warnings; |
---|
| 19 | use strict; |
---|
| 20 | |
---|
| 21 | # Exports |
---|
| 22 | our (@ISA, @EXPORT, @EXPORT_OK); |
---|
| 23 | |
---|
| 24 | sub timestamp_command; |
---|
| 25 | |
---|
| 26 | require Exporter; |
---|
| 27 | @ISA = qw(Exporter); |
---|
| 28 | @EXPORT = qw(timestamp_command); |
---|
| 29 | |
---|
| 30 | # ------------------------------------------------------------------------------ |
---|
| 31 | |
---|
| 32 | # Module level variables |
---|
| 33 | my %cmd_start_time = (); # Command start time, (key = command, value = time) |
---|
| 34 | |
---|
| 35 | # ------------------------------------------------------------------------------ |
---|
| 36 | # SYNOPSIS |
---|
| 37 | # $string = &Fcm::Timer::timestamp_command ($command[, $status]); |
---|
| 38 | # |
---|
| 39 | # DESCRIPTION |
---|
| 40 | # This function returns a string adding to $command a prefix according the |
---|
| 41 | # value of $status. If $status is not specified or does not match the word |
---|
| 42 | # "end", the status is assumed to be "start". At "start", the prefix will |
---|
| 43 | # contain the current timestamp. If $status is the word "end", the prefix |
---|
| 44 | # will contain the total time taken since this function was called with the |
---|
| 45 | # same $command at the "start" status. |
---|
| 46 | # ------------------------------------------------------------------------------ |
---|
| 47 | |
---|
| 48 | sub timestamp_command { |
---|
| 49 | (my $command, my $status) = @_; |
---|
| 50 | |
---|
| 51 | my $prefix; |
---|
| 52 | if ($status and $status =~ /end/i) { |
---|
| 53 | # Status is "end", insert time taken |
---|
| 54 | my $lapse = time () - $cmd_start_time{$command}; |
---|
| 55 | $prefix = sprintf "# Time taken: %12d s=> ", $lapse; |
---|
| 56 | |
---|
| 57 | } else { |
---|
| 58 | # Status is "start", insert time stamp |
---|
| 59 | $cmd_start_time{$command} = time; |
---|
| 60 | |
---|
| 61 | (my $sec, my $min, my $hour, my $mday, my $mon, my $year) = localtime; |
---|
| 62 | $prefix = sprintf "# Start: %04d-%02d-%02d %02d:%02d:%02d=> ", |
---|
| 63 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | return $prefix . $command . "\n"; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | # ------------------------------------------------------------------------------ |
---|
| 70 | |
---|
| 71 | 1; |
---|
| 72 | |
---|
| 73 | __END__ |
---|