//
// aegis - project change supervisor
// Copyright (C) 2014 Peter Miller
//
// 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 3 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.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see .
//
#include
#include
static int
this_year(void)
{
time_t now = 0;
time(&now);
struct tm *tmp = localtime(&now);
return (tmp->tm_year + 1900);
}
static inline int
year_diff(int a, int b)
{
int d = a - b;
if (d < 0)
d = -d;
return d;
}
int
fix_tm_year(int n)
{
if (n >= 1900)
return (n - 1900);
if (n >= 0 && n < 100)
{
int cccc = this_year();
int this_century = (cccc / 100) * 100;
int last_century = this_century - 100;
int next_century = this_century + 100;
int last_ccnn = last_century + n;
int this_ccnn = this_century + n;
int next_ccnn = next_century + n;
// assume cvlosest century first
int ccnn = this_ccnn;
int d = year_diff(cccc, this_ccnn);
// look for closer century
int d2 = year_diff(cccc, last_ccnn);
if (d2 < d)
{
ccnn = last_ccnn;
d = d2;
}
// look for closer century
int d3 = year_diff(cccc, next_ccnn);
if (d3 < d)
{
ccnn = next_ccnn;
d = d3;
}
return (ccnn - 1900);
}
return n;
}
// vim: set ts=8 sw=4 et :