# Copyright 2020 Florida State University, Center for Ocean-Atmospheric Prediction Studies # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. package CoapsTime; use strict; use integer; use DateTime; use Exporter qw(import); our @EXPORT = qw(invtime convtime); sub invtime { # This subroutine takes a timestamp representing the minutes from # 1-1-1980 00:00 and converts it to strings with the human readable time. # # The return value has two parts: (YYYYMMDD, HHMM) # # Example: # my ($yyyymmdd, $hhmm) = invtime(9411643); # # print "$yyyymmdd $hhmm\n"; # # The above example prints "19971122 2043" my $samos_epoch = $_[0]; my $logfilepath = $_[1]; # redirect standard error to a log file if given if ($logfilepath ne "") { *OLD_STDERR = *STDERR; open my $logfile, '>', $logfilepath; *STDERR = $logfile; } my $start = DateTime->new( year => 1980, month => 1, day => 1, )->epoch(); my $unix_epoch = $samos_epoch*60 + $start; my $datetime = DateTime->from_epoch( epoch => $unix_epoch ); my $date = $datetime->ymd(''); my $time = $datetime->hms(''); # restore standard error if ($logfilepath ne "") { *STDERR = *OLD_STDERR; } return ($date, $time); } sub convtime { # this subroutine takes five parameters and # returns an integer which is the number of minutes from 1-1-1980 00:00 # # In particular, this subroutine will convert a given year, month, day, hour, # and minute to a minutes from 1-1-1980 00:00 time stamp. # for example: # print "dfdfdf ",convtime(1980, 1, 1, 0, 0)," dfdfdfd\n"; # # within a Perl script should cause the line: # # dfdfdf 0 dfdfdfd # # to be written out to default output. # # print "dfdfdfdf ", convtime(2019, 12, 13, 0, 0), "\n"; # # yields # # dfdfdfdf 21011040 # # print "dfdfdfdf ", convtime(2019, 2, 29, 0, 0), "\n"; # # yields # # ERROR ---- Feb. day out of range (1-28) at ConvTime.pl line 111. # Originally written by Shawn Smith (smith@coaps.fsu.edu) in FORTRAN # Updated Spring 1999 for Y2K compliance by Anthony Arguez # (anthony@coaps.fsu.edu). # # Perl version created by James Lamm (lamm@coaps.fsu.edu) # # work with integer math rather than (the default) floating point my $year = @_[0]; my $month = @_[1]; my $day = @_[2]; my $hour = @_[3]; my $minute = @_[4]; my $logfilepath = @_[5]; # redirect standard error to a log file if given if ($logfilepath ne "") { *OLD_STDERR = *STDERR; open my $logfile, '>', $logfilepath; *STDERR = $logfile; } # get the unix epoch times for our start date and input dates my $start = DateTime->new( year => 1980, month => 1, day => 1, hour => 0, minute => 0 )->epoch(); my $input = DateTime->new( year => $year, month => $month, day => $day, hour => $hour, minute => $minute )->epoch(); # convert the times to minutes # note that since the resolution of our input time is only to the minute, # the difference will always be a multiple of 60, so we don't need to # worry about rounding my $diff = ($input - $start)/60; if ($diff lt 0) { print STDERR "convtime: Date given is before 1980: $year-$month-$day $hour:$minute:00"; # restore standard error if ($logfilepath ne "") { *STDERR = *OLD_STDERR; } exit(1); } # restore standard error if ($logfilepath ne "") { *STDERR = *OLD_STDERR; } return $diff; }