/* 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. */ #include int __is_leap_year(int year) { if ((year % 400) == 0) { return 1; } else if ((year % 100) == 0) { return 0; } else if ((year % 4) == 0) { return 1; } return 0; } int __minutes_in_year(int year) { if (__is_leap_year(year)) { return 527040; } else { return 525600; } } int __minutes_in_month(int month, int year) { int minutes_per_month[12] = { 44640,40320,44640,43200,44640,43200, 44640,44640,43200,44640,43200,44640 }; if (__is_leap_year(year)) { minutes_per_month[1] += 1440; } return minutes_per_month[month-1]; } /* Function: convtime Author: Shyam Lakshmin (lakshmin@cs.fsu.edu) Date: July 13, 1999 Revision: For Y2020 Author: Homer McMillan (hmcmillan@coaps.fsu.edu) Date: February 11, 2020 This function will convert a year/month/day/hour/minute time into a minute timestamp measured from 1-1-1980 00:00. The function is passed the year/month/day/hour/minute time and the return value is the minute timestamp. A value of -9999 is returned if an error is encountered. Based upon the subroutine convtime.f */ int convtime(int year, int month, int day, int hour, int minute) { int temp_year = year-1, result = 0, error = 0; int days_per_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (__is_leap_year(year)) { days_per_month[1] += 1; } /* validate the given date-time */ if (year < 1980) { fprintf(stderr, "ERROR: convtime: Given year (%d) was less than 1980\n", year); error = 1; } if ((month < 1) || (12 < month)) { fprintf(stderr, "ERROR: convtime: Out of range month (%d)\n", month); error = 1; } if ((day < 1) || (days_per_month[month-1] < day)) { fprintf(stderr, "ERROR: convtime: Out of range day (%d)\n", day); error = 1; } if ((hour < 0) || (23 < hour)) { fprintf(stderr, "ERROR: convtime: Out of range hour (%d)\n", hour); error = 1; } if ((minute < 0) || (59 < minute)) { fprintf(stderr, "ERROR: convtime: Out of range minute (%d)\n", minute); error = 1; } if (error) return -9999; /* convert years to minutes */ while (temp_year > 1979) { result += __minutes_in_year(temp_year); temp_year -= 1; } month -= 1; /* convert months to minutes */ while (month > 0) { result += __minutes_in_month(month, year); month -= 1; } day -= 1; /* convert days to minutes */ while (day > 0) { result += 1440; day -= 1; } /* convert hours to minutes */ while (hour > 0) { result += 60; hour -= 1; } result += minute; return result; } /* Function: invtime Author: Mylene J. Remigio Date: November 12, 1997 Revision: For Y2K Author: Shyam Lakshmin Date: July 8, 1999 Revision: For Y2020 Author: Homer McMillan Date: February 11, 2020 This procedure converts the minute timestamp for SAMOS ship data back into a standard year/month/day/hour/minute time. Based on the subroutine invtime.f INPUT VALUE: timestamp - Number of minutes after 1980-01-01 00:00:00 UTC. Range is [0,...) **NOTE: year, month, day, hour, and minute are altered by this function** year - Year from timestamp. Range is [1980,...) month - The month from timestamp. Range is [1,12] day - The day from timestamp. Range is [1,31], depending on the month hour - The hour from timestamp. Range is [0,23] minute - integer The minute from timestamp. Range is [0,59] ERROR CONDITIONS: If timestamp < 0 an error is printed to stderr and year is set to -9999. month, day, hour, and minute are set to 0 ***********************************************************************/ void invtime(int timestamp, int* year, int* month, int* day, int* hour, int* minute) { int temp; *year = 1980; *month = 1; *day = 1; *hour = 0; *minute = 0; /* timestamp can't be negative */ if (timestamp < 0) { fprintf(stderr, "ERROR: invtime: timestamp must be > 0 (found %d)\n", timestamp); *year = -9999; *month = 0; *day = 0; return; } /* convert from minutes to years */ temp = __minutes_in_year(*year); while (timestamp >= temp) { *year += 1; timestamp -= temp; temp = __minutes_in_year(*year); } /* convert from minutes to months */ temp = __minutes_in_month(*month, *year); while (timestamp >= temp) { *month += 1; timestamp -= temp; temp = __minutes_in_month(*month, *year); } /* convert from minutes to days */ while (timestamp >= 1440) { *day += 1; timestamp -= 1440; } /* convert from minutes to hours */ while (timestamp >= 60) { *hour += 1; timestamp -= 60; } *minute = timestamp; }