Timer.pm 2.2 KB

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