       1 package TimeUtil;
       2 #
       3 # $RCSfile: TimeUtil.pm,v $
       4 # $Date: 2015/02/28 20:47:30 $
       5 # $Revision: 1.9 $
       6 #
       7 # Author: Manish Sud <msud@san.rr.com>
       8 #
       9 # Copyright (C) 2015 Manish Sud. All rights reserved.
      10 #
      11 # This file is part of MayaChemTools.
      12 #
      13 # MayaChemTools is free software; you can redistribute it and/or modify it under
      14 # the terms of the GNU Lesser General Public License as published by the Free
      15 # Software Foundation; either version 3 of the License, or (at your option) any
      16 # later version.
      17 #
      18 # MayaChemTools is distributed in the hope that it will be useful, but without
      19 # any warranty; without even the implied warranty of merchantability of fitness
      20 # for a particular purpose.  See the GNU Lesser General Public License for more
      21 # details.
      22 #
      23 # You should have received a copy of the GNU Lesser General Public License
      24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
      25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
      26 # Boston, MA, 02111-1307, USA.
      27 #
      28
      29 use strict;
      30 use Exporter;
      31 use Time::localtime ();
      32
      33 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
      34
      35 @ISA = qw(Exporter);
      36 @EXPORT = qw(CTimeStamp FPFileTimeStamp ISO8601Date ISO8601Time ISO8601TimeStamp PDBFileTimeStamp SDFileTimeStamp TimeStamp MonthNameToNumber MonthNumberToFullName MonthNumberToAbbreviatedName WeekDayNameToNumber WeekDayNumberToFullName WeekDayNumberToAbbreviatedName);
      37 @EXPORT_OK = qw();
      38 %EXPORT_TAGS = (all  => [@EXPORT, @EXPORT_OK]);
      39
      40 #
      41 # Initialize package data...
      42 #
      43 my(%MonthNameToNumber, %MonthNumberToFullNameName, %MonthNumberToAbbreviatedName, %WeekDayNameToNumber, %WeekDayNumberToFullName, %WeekDayNumberToAbbreviatedName);
      44 _InitializeData();
      45
      46 # Return CTime as default time stamp for MayaChemTools...
      47 #
      48 sub TimeStamp {
      49   return CTimeStamp();
      50 }
      51
      52 # Generate ctime time stamp...
      53 #
      54 # Format: WDay Mon MDay HH:MM:SS YYYY
      55 #
      56 sub CTimeStamp {
      57   my($CTimeStamp);
      58
      59   # Take out an extra space inserted between month name and day by ctime...
      60   $CTimeStamp = Time::localtime::ctime();
      61   $CTimeStamp =~ s/[ ]+/ /g;
      62
      63   return $CTimeStamp;
      64 }
      65
      66 # Generate ISO 8601 timestamp in extended format...
      67 #
      68 # Format: [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]
      69 #
      70 sub ISO8601TimeStamp {
      71   my($TimeStamp, $Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst);
      72
      73   ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst) = _LocalTime();
      74
      75   $TimeStamp = sprintf "%4i-%02i-%02iT%02i:%02i:%02i", $Year, $Mon, $MDay, $Hour, $Min, $Sec;
      76
      77   return $TimeStamp;
      78 }
      79
      80 # Generate ISO 8601 date...
      81 #
      82 # Format: [YYYY]-[MM]-[DD]
      83 #
      84 sub ISO8601Date {
      85   my($Date, $Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst);
      86
      87   ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst) = _LocalTime();
      88
      89   $Date = sprintf "%4i-%02i-%02i", $Year, $Mon, $MDay;
      90
      91   return $Date;
      92 }
      93
      94 # Generate ISO 8601 time in extended format...
      95 #
      96 # Format: [hh]:[mm]:[ss]
      97 #
      98 sub ISO8601Time {
      99   my($Time, $Sec, $Min, $Hour);
     100
     101   ($Sec, $Min, $Hour) = _LocalTime();
     102
     103   $Time = sprintf "%02i:%02i:%02i", $Hour, $Min, $Sec;
     104
     105   return $Time;
     106 }
     107
     108 # Generate MayaChemTools' FP file timestamp...
     109 #
     110 sub FPFileTimeStamp {
     111   return CTimeStamp();
     112 }
     113
     114 # Generate PDB file timestamp...
     115 #
     116 sub PDBFileTimeStamp {
     117   my($TimeStamp, $Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst, $MonthName);
     118
     119   ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst) = _LocalTime();
     120
     121   $MonthName = uc MonthNumberToAbbreviatedName($Mon);
     122   $Year = substr($Year, -2, 2);
     123
     124   $TimeStamp = sprintf "%02i-%3s-%2i", $MDay, $MonthName, $Year;
     125
     126   return $TimeStamp;
     127 }
     128
     129 # Generate SD file timestamp...
     130 #
     131 sub SDFileTimeStamp {
     132   my($TimeStamp, $Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst);
     133
     134   ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst) = _LocalTime();
     135
     136   $Year = substr($Year, -2, 2);
     137
     138   $TimeStamp = sprintf "%02i%02i%02i%02i%02i", $Mon, $MDay, $Year, $Hour, $Min;
     139
     140   return $TimeStamp;
     141 }
     142
     143 # Get local time with modifications to data returned by native localtime function...
     144 #
     145 sub _LocalTime {
     146   my($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst);
     147
     148   ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst) = localtime;
     149
     150   $Mon += 1;
     151   $Year += 1900;
     152
     153   return ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDst);
     154 }
     155
     156 # Return month number from full or three letter abbreviated month name...
     157 sub MonthNameToNumber {
     158   my($Name) = @_;
     159
     160   return (exists $MonthNameToNumber{lc $Name}) ? $MonthNameToNumber{lc $Name} : '';
     161 }
     162
     163 # Return full month name from month number...
     164 sub MonthNumberToFullName {
     165   my($Number) = @_;
     166
     167   return (exists $MonthNumberToFullNameName{$Number}) ? $MonthNumberToFullNameName{$Number} : '';
     168 }
     169
     170 # Return three letter abbreviated month name from month number...
     171 sub MonthNumberToAbbreviatedName {
     172   my($Number) = @_;
     173
     174   return (exists $MonthNumberToAbbreviatedName{$Number}) ? $MonthNumberToAbbreviatedName{$Number} : '';
     175 }
     176
     177 # Return week daty number from full or three letter abbreviated week day name...
     178 sub WeekDayNameToNumber {
     179   my($Name) = @_;
     180
     181   return (exists $WeekDayNameToNumber{lc $Name}) ? $WeekDayNameToNumber{lc $Name} : '';
     182 }
     183
     184 # Return full week day name from week day number...
     185 sub WeekDayNumberToFullName {
     186   my($Number) = @_;
     187
     188   return (exists $WeekDayNumberToFullName{$Number}) ? $WeekDayNumberToFullName{$Number} : '';
     189 }
     190
     191 # Return three letter abbreviated week day name from week day number...
     192 sub WeekDayNumberToAbbreviatedName {
     193   my($Number) = @_;
     194
     195   return (exists $WeekDayNumberToAbbreviatedName{$Number}) ? $WeekDayNumberToAbbreviatedName{$Number} : '';
     196 }
     197
     198 # Initialize week/month day/name data...
     199 #
     200 sub _InitializeData {
     201
     202   %MonthNameToNumber = ('january' => 1, 'february' => 2, 'march' => 3, 'april' => 4,
     203                         'may' => 5, 'june' => 6, 'july' => 7, 'august' => 8,
     204                         'september' => 9, 'october' => 10, 'november' => 11, 'december' => 12,
     205                         'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
     206                         'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
     207                         'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12);
     208
     209   %MonthNumberToFullNameName = (1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
     210                                 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',
     211                                 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');
     212
     213   %MonthNumberToAbbreviatedName = (1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr',
     214                                    5 => 'May', 6 => 'Jun', 7 => 'Jul', 8 => 'Aug',
     215                                    9 => 'Sep', 10 => 'Oct', 11 => 'Nov', 12 => 'Dec');
     216
     217   %WeekDayNameToNumber = ('sunday' => 1, 'monday' => 2, 'tuesday' => 3, 'wednesday' => 4,
     218                           'thursday' => 5, 'friday' => 6, 'saturday' => 7,
     219                           'sun' => 1, 'mon' => 2, 'tue' => 3, 'wed' => 4,
     220                           'thu' => 5, 'fri' => 6, 'sat' => 7);
     221
     222   %WeekDayNumberToFullName = (1 => 'Sunday', 2 => 'Monday', 3 => 'Tuesday',
     223                               4 => 'Wednesday', 5 => 'Thursday', 6 => 'Friday', 7 => 'Saturday');
     224
     225   %WeekDayNumberToAbbreviatedName = (1 => 'Sun', 2 => 'Mon', 3 => 'Tue',
     226                                      4 => 'Wed', 5 => 'Thu', 6 => 'Fri', 7 => 'Sat');
     227 }
     228