# ---------------------------------------------------------------------------
# 
# File:    GEN_TIME.pm
#
# Copyright (c) Programming Research, 2004.
#
# Version: 1.2
#
# Written: C.Waddingham 20-Jan-05
#
# Purpose: To provide utility functions for time handling.
#
# History  : 
#   Date      Version By   Comment
#   --------  ------- ---- ----------------------------------------------
#   16-03-09  1.2     CW   FORMAT_TIME formatting added.
#   20-02-07  1.1     CW   Milli sec ability added to TIME().
# ---------------------------------------------------------------------------

package GEN_TIME; #==========================================================

require Exporter;
our @ISA = qw( Exporter );

BEGIN
{
   use integer;
   use strict;

   require GEN_STRING;
}

#--------------------------------------------------------- DATE_DD_MMM_YY --

sub DATE_DD_MMM_YY
{
   # Usage: $date_dd_mmm_yy = DATE_DD_MMM_YY();
   #
   # Note this sub is based on localtime.  Other possibility is gmtime.
   # Others?
   #
   # NB sub DATE() deleted from GEN at 23-01-05 as it is identical to this
   # sub except that DATE() was based on gmtime.
   #

   my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;

   my $dd  = GEN_STRING::LPAD_CH( $mday, 2, "0" );

   my $mmm = ( qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) )[ $mon ];

   my $yy  = GEN_STRING::LPAD_CH( ( $year - 100 ), 2, "0" );

   return "$dd-$mmm-$yy";
}

#---------------------------------------------------------- DATE_ISO_8601 --

sub DATE_ISO_8601
{
   # Usage: $$date_yyyy_mm_dd = DATE_ISO_8601()
   #
   # Note this sub is based on localtime.  Other possibility is gmtime.
   # Others?
   #

   my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;

   my $dd   = GEN_STRING::LPAD_CH( $mday, 2, "0" );
   
   my $mm   = GEN_STRING::LPAD_CH( $mon + 1, 2, "0" );

   my $yyyy = GEN_STRING::LPAD_CH( $year + 1900, 4, "0" );

   return "$yyyy-$mm-$dd";
}

#------------------------------------------------------------ FORMAT_TIME --

sub FORMAT_TIME
{
   # Usage: $formatted_time = FORMAT_TIME( $time );
   #
   # Returns the supplied time formatted.
   #
   my $time = shift;

   my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( $time );

   return "$hour:" . GEN_STRING::LPAD_CH( $min, 2, "0" ) . ":" . GEN_STRING::LPAD_CH( $sec, 2, "0" );
}

#---------------------------------------------------------------------------

sub TIME
{
   # Usage: $formatted_time = TIME();
   #
   # Returns the current time formatted as HH:MM:SS
   #
   my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;

###   # Following bodged 12-12-06 for Ionela's timing purposes only.
###   # NB This edit is not configured.
###   #
###
###   require Time::HiRes; 
###   my ( $s, $usec ) = Time::HiRes::gettimeofday();
###   my $msec = int( $usec / 1000 );

   return GEN_STRING::LPAD_CH( $hour, 2, "0" ) . ":" . 
          GEN_STRING::LPAD_CH( $min,  2, "0" ) . ":" . 
          GEN_STRING::LPAD_CH( $sec,  2, "0" );
###	     GEN_STRING::LPAD_CH( $sec,  2, "0" ) . "." .
###          GEN_STRING::LPAD_CH( $msec, 3, "0" );
}

#------------------------------------------------------------------- YEAR --

sub YEAR
{
   # Usage: $year = YEAR();
   #
   # Returns the current year as dddd.
   #
   my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;
   return $year + 1900;
}

#-- eof GEN_TIME.pm --------------------------------------------------------
