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