/*
   <p>Autodate contains date and time display formatting functions.
   The display format is controlled by the parameters retrieved by
   <i>country()</i>.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   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 Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include "environ.h"

#ifdef MSDOS
#include <dos.h>
#endif

struct COUNTRY  _cp;
int             _cpempty = TRUE;
int             _cpmanual = FALSE;

/*
   Formats dates to a display layout, taking national preferences into
   account where necessary. <u>tm</u> is the standard UNIX time structure,
   holding the components of a date and time. This contains the date that
   is to be formatted for display. <u>fmt</u> is one of:
   <ul>
   <li>AUTOFMT - format the date in the numeric format favoured by the
   default country.</li>
   <li>MANUAL - similar to AUTOFMT, but use the format for the country
   whose international dialling code is in the <u>code</u> parameter.</li>
   <li>DDMMMYY - format the date as two days, a three character month, and
   two years, e.g. 06Jun05</li>
   </ul>
   Returns CHARDATE, which points to a static string containing the
   formatted date.

   The default country is defined in <i>/usr/local/etc/country</i> and
   retrieved by a call to <i>country()</i>.
   
   <b>See also</b> <i>country()</i>
*/
CHARDATE autodate(struct tm *tbp, int fmt, int code)
{
    static char *month[] =    {    "Jan", "Feb", "Mar", "Apr", 
                                   "May", "Jun", "Jul", "Aug", 
                                   "Sep", "Oct", "Nov", "Dec",
                                   "???"
                              };
    static char        date[11];
    int                century;
    
    if (fmt == MANUAL)
    {
        country(code,&_cp);
        _cpempty = TRUE;
        _cpmanual = TRUE;
    }
    else
    {
        if (_cpempty)    /* read country parameters if necessary */
        {
            country(0,&_cp);
            _cpempty = FALSE;
            _cpmanual = FALSE;
        }
    }

    tbp->tm_year = abs(tbp->tm_year)%100;
    if (tbp->tm_mon < 0 || tbp->tm_mon > 11)
        tbp->tm_mon = 12;
    tbp->tm_mday = abs(tbp->tm_mday)%100;

    if (fmt == DDMMMYY)
        sprintf(date,"%02d%03s%02d",
                tbp->tm_mday,month[(tbp->tm_mon)],tbp->tm_year);
    else
    {
        if (tbp->tm_year < 80)
            century = 2000;
        else
            century = 1900;
            
        switch (_cp.co_date)
        {
            case 0: sprintf(date,"%02d%1s%02d%1s%04d",
                            tbp->tm_mon+1,_cp.co_dtsep,
                            tbp->tm_mday,_cp.co_dtsep,
                            tbp->tm_year+century);
                    break;
            case 1: sprintf(date,"%02d%1s%02d%1s%04d",
                            tbp->tm_mday,_cp.co_dtsep,
                            tbp->tm_mon+1,_cp.co_dtsep,
                            tbp->tm_year+century);
                    break;
            default:sprintf(date,"%04d%1s%02d%1s%02d",
                            tbp->tm_year+century,_cp.co_dtsep,
                            tbp->tm_mon+1,_cp.co_dtsep,
                            tbp->tm_mday);
        }
    }
    return(date);
}

/*
   Returns CHARTIME, which points to a string containing the time
   formatted for a 12 hour or 24 hour clock to suit the default
   country.

   The default country is defined in <i>/usr/local/etc/country</i> and
   retrieved by a call to <i>country()</i>.
   
   <b>See also</b> <i>country()</i>
*/
CHARTIME autotime(struct tm *tbp)
{
    int           hours,
                  mins,
                  secs,
                  pm;
    static char   *xx[] = { "am", "pm" };
    static char   time[12];

    if (!_cpmanual)
        if (_cpempty)    /* read country parameters if necessary */
        {
            country(0,&_cp);
            _cpempty = FALSE;
        }

    hours = abs(tbp->tm_hour)%100;
    mins = abs(tbp->tm_min)%100;
    secs = abs(tbp->tm_sec)%100;
    
    if (_cp.co_time == 0)
    {
        pm = (hours/12)%2;
        hours = hours%12;
        
        sprintf(time,"%d%1s%02d%1s%02d %02s",
                hours,_cp.co_tmsep,tbp->tm_min,
                _cp.co_tmsep,tbp->tm_sec,xx[pm]);
    }
    else
        sprintf(time,"%02d%1s%02d%1s%02d",
                hours,_cp.co_tmsep,tbp->tm_min,
                _cp.co_tmsep,tbp->tm_sec);
    return(time);
}
