Mercurial > repos > deepakjadmin > mayatool3_test2
comparison lib/TimeUtil.pm @ 0:4816e4a8ae95 draft default tip
Uploaded
| author | deepakjadmin |
|---|---|
| date | Wed, 20 Jan 2016 09:23:18 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4816e4a8ae95 |
|---|---|
| 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 | |
| 229 1; | |
| 230 | |
| 231 __END__ | |
| 232 | |
| 233 =head1 NAME | |
| 234 | |
| 235 TimeUtil | |
| 236 | |
| 237 =head1 SYNOPSIS | |
| 238 | |
| 239 use TimeUtil; | |
| 240 | |
| 241 use TimeUtil qw(:all); | |
| 242 | |
| 243 =head1 DESCRIPTION | |
| 244 | |
| 245 B<TimeUtil> module provides the following functions: | |
| 246 | |
| 247 CTimeStamp, FPFileTimeStamp, ISO8601Date, ISO8601Time, ISO8601TimeStamp, | |
| 248 MonthNameToNumber, MonthNumberToAbbreviatedName, MonthNumberToFullName, | |
| 249 PDBFileTimeStamp, SDFileTimeStamp, TimeStamp, WeekDayNameToNumber, | |
| 250 WeekDayNumberToAbbreviatedName, WeekDayNumberToFullName | |
| 251 | |
| 252 =head1 FUNCTIONS | |
| 253 | |
| 254 =over 4 | |
| 255 | |
| 256 =item B<CTimeStamp> | |
| 257 | |
| 258 $CTimeStamp = CTimeStamp(); | |
| 259 | |
| 260 Returns B<CTimeStamp> string using the following format: WDay Mon MDay HH:MM:SS YYYY | |
| 261 | |
| 262 =item B<FPFileTimeStamp> | |
| 263 | |
| 264 $FPFileTimeStamp = FPFileTimeStamp(); | |
| 265 | |
| 266 Returns fingerints B<FP> file time stamp string for MayaChemTools package. It corresponds to | |
| 267 B<CTimeStamp>. | |
| 268 | |
| 269 =item B<ISO8601Date> | |
| 270 | |
| 271 $Date = ISO8601Date(); | |
| 272 | |
| 273 Returns ISO8601 B<Date> string using the following format: [YYYY]-[MM]-[DD] | |
| 274 | |
| 275 =item B<ISO8601Time> | |
| 276 | |
| 277 $Time = ISO8601Time(); | |
| 278 | |
| 279 Returns ISO8601 B<Time> string using the following extended format: [hh]:[mm]:[ss] | |
| 280 | |
| 281 =item B<ISO8601TimeStamp> | |
| 282 | |
| 283 $TimeStamp = ISO8601TimeStamp(); | |
| 284 | |
| 285 Returns ISO8601 B<TimeStamp> string using the following extended format: [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss] | |
| 286 | |
| 287 =item B<MonthNameToNumber> | |
| 288 | |
| 289 $Number = MonthNameToNumber($Name); | |
| 290 | |
| 291 Return month B<Number> for full month I<Name> or three letter abbreviated month I<Name>. | |
| 292 | |
| 293 =item B<MonthNumberToAbbreviatedName> | |
| 294 | |
| 295 $AbbrevMonthName = MonthNumberToAbbreviatedName($Number); | |
| 296 | |
| 297 Returns three letter B<AbbrevMonthName> for month I<Number>. | |
| 298 | |
| 299 =item B<MonthNumberToFullName> | |
| 300 | |
| 301 $Name = MonthNumberToFullName($Number); | |
| 302 | |
| 303 Returns full month B<Name> for month I<Number>. | |
| 304 | |
| 305 =item B<PDBFileTimeStamp> | |
| 306 | |
| 307 $TimeStamp = PDBFileTimeStamp(); | |
| 308 | |
| 309 Returns PDB file B<TimeStamp> using the following format: DD-MMM-YY | |
| 310 | |
| 311 =item B<SDFileTimeStamp> | |
| 312 | |
| 313 $TimeStamp = SDFileTimeStamp(); | |
| 314 | |
| 315 Returns SD file B<TimeStamp> using the following format: MMDDYYHHMM | |
| 316 | |
| 317 =item B<TimeStamp> | |
| 318 | |
| 319 $TimeStamp = TimeStamp(); | |
| 320 | |
| 321 Returns deafult I<TimeStamp> for MayaChemTools. It corresponds to B<CTimeStamp>. | |
| 322 | |
| 323 =item B<WeekDayNameToNumber> | |
| 324 | |
| 325 $Number = WeekDayNameToNumber($Name); | |
| 326 | |
| 327 Returns week day B<Number> from full week day I<Name> or three letter abbreviated week | |
| 328 day I<Name>. | |
| 329 | |
| 330 =item B<WeekDayNumberToAbbreviatedName> | |
| 331 | |
| 332 $Name = WeekDayNumberToAbbreviatedName($Number); | |
| 333 | |
| 334 Returns three letter abbreviates week day B<Name> for week day I<Number>. | |
| 335 | |
| 336 =item B<WeekDayNumberToFullName> | |
| 337 | |
| 338 $Name = WeekDayNumberToFullName($Number); | |
| 339 | |
| 340 Returns full week day B<Name> for week day I<Number>. | |
| 341 | |
| 342 =back | |
| 343 | |
| 344 =head1 AUTHOR | |
| 345 | |
| 346 Manish Sud <msud@san.rr.com> | |
| 347 | |
| 348 =head1 SEE ALSO | |
| 349 | |
| 350 FileUtil.pm, TextUtil.pm | |
| 351 | |
| 352 =head1 COPYRIGHT | |
| 353 | |
| 354 Copyright (C) 2015 Manish Sud. All rights reserved. | |
| 355 | |
| 356 This file is part of MayaChemTools. | |
| 357 | |
| 358 MayaChemTools is free software; you can redistribute it and/or modify it under | |
| 359 the terms of the GNU Lesser General Public License as published by the Free | |
| 360 Software Foundation; either version 3 of the License, or (at your option) | |
| 361 any later version. | |
| 362 | |
| 363 =cut |
