#!/bin/perl

# Routine to guess the month and year, given a day. The day is the day of receipt
# of a met. report; we want either the current mon,year or (if day,mon,year is
# after the current date) we want (day,mon-1,year) unless mon-1 == 0 in which case
# we want (day,12,year-1).

# We may wish to pretend that the date is different. If
# so, the global variables PRETEND_DAY_IS, PRETEND_MONTH_IS and
# PRETEND_YEAR_IS should be set.
# Alternatively, read it from ENV

#   Copyright W. M. Connolley 1997, 2003. wmc@bas.ac.uk
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details, available from:
#
#   http://www.gnu.org/licenses/gpl.txt.

sub guess_mon_year {

# Get parameter: day
  local($day)=@_;

# Get the current day, month, year. Note that year is returned as 99,100 for 1999, 2000
  local($sec,$min,$hour,$DAY,$MON,$YEAR,$junk)=localtime(time());
  $YEAR+=1900;
  $MON=$MON+1;

# Maybe pretend...
# First try ENV
  if ($ENV{"PRETEND_YEAR_IS"}) { $YEAR=$ENV{"PRETEND_YEAR_IS"} };
  if ($ENV{"PRETEND_MONTH_IS"}) { $MON=$ENV{"PRETEND_MONTH_IS"} };
  if ($ENV{"PRETEND_DAY_IS"}) { $DAY=$ENV{"PRETEND_DAY_IS"} };
# But override if set
  if ($PRETEND_YEAR_IS) { $YEAR=$PRETEND_YEAR_IS };
  if ($PRETEND_MONTH_IS) { $MON=$PRETEND_MONTH_IS };
  if ($PRETEND_DAY_IS) { $DAY=$PRETEND_DAY_IS };
# Make sure still 4-figure year, if *we* set it to, eg, 98
  if ($YEAR<1900) { $YEAR+=1900 };
# Maybe need previous month, year
  if ($day > $DAY+1) { $MON-- };
  if ($MON ==0) { $MON=12; $YEAR-- };

  return ($MON,$YEAR)

};

1;
