//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// 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.
//
// You should have received a copy of the 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.
//-----------------------------------------------------------------------------
// package	libh/http
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	phil at ipal dot org
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 112 columns wide.
//-----------------------------------------------------------------------------

#include "http_lib.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	http_date_now
//
// purpose	Put the current date as an HTTP date header in the queue.
//
// arguments	1 (int) priority (lowest first)
//		2 (const char *) header prefix to use
//
// returns	(int) -1 : error
//		(int)  0 : OK
//-----------------------------------------------------------------------------
#define http_date_now() http_date(0,NULL,0)

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	http_date
//
// purpose	Put an HTTP date header in the queue.
//
// arguments	1 (int) priority (lowest first)
//		2 (const char *) header prefix to use
//		3 (time_t) time value to convert, or 0 for current time
//
// returns	(int) -1 : error
//		(int)  0 : OK
//-----------------------------------------------------------------------------
#define http_date_now() http_date(0,NULL,0)
int
http_date (
    int			arg_priority
    ,
    const char *	arg_head
    ,
    time_t		arg_time
    )
__PROTO_END__
{
    struct tm *		time_gmt		;
    char		date_str	[40]	;


    //-- If HTTP is already done, return an error.
    if ( http_done ) return -1;

    //-- If no header specified, pick the default.
    if ( ! arg_head ) arg_head = "Date";

    //-- Get current time if given value is 0 or 1.
    if ( ( (int) arg_time ) <= 1 ) arg_time = time( NULL );

    //-- Get time and format it.
    time_gmt = gmtime( & arg_time );
    strftime( date_str, sizeof (date_str), "%a, %d %b %Y %H:%M:%S", time_gmt );

    //-- Put the HTTP header with the time.
    return http_header( arg_priority, arg_head, date_str, "GMT", NULL );
}

