SroT/gods/
/***************************************************************************

    Time Zone Code (c) by Ryan Jennings (Markanth)
        email: r-jenn@shaw.ca (markanth@dlmud.com)
        
 **************************************************************************/       
#include <sys/types.h>
#include <stdio.h>
#include <time.h>
#include "merc.h"

#define MAX_TZONE   25

struct tzone_type
{
    const char *name; /* Name of the time zone */
    const char *zone; /* Cities or Zones in zone crossing */
    int gmt_offset; /* Difference in hours from Greenwich Mean Time */
    int dst_offset; /* Day Light Savings Time offset,
                       Not used but left it in anyway */
};

const struct tzone_type tzone_table[MAX_TZONE] =
{
    {"GMT-12", "Eniwetok", -12, 0},
    {"GMT-11", "Samoa", -11, 0},
    {"GMT-10", "Hawaii", -10, 0},
    {"GMT-9", "Alaska", -9, 0},
    {"GMT-8", "PST, Pacific US", -8, -7},
    {"GMT-7", "MST, Mountain US", -7, -6},
    {"GMT-6", "CST, Central US", -6, -5},
    {"GMT-5", "EST, Eastern US", -5, -4},
    {"GMT-4", "Atlantic, Canada", -4, 0},
    {"GMT-3", "Brazilia, Buenos Aries", -3, 0},
    {"GMT-2", "Mid-Atlantic", -2, 0},
    {"GMT-1", "Cape Verdes", -1, 0},
    {"GMT", "Greenwich Mean Time, Greenwich", 0, 0},
    {"GMT+1", "Berlin, Rome", 1, 0},
    {"GMT+2", "Israel, Cairo", 2, 0},
    {"GMT+3", "Moscow, Kuwait", 3, 0},
    {"GMT+4", "Abu Dhabi, Muscat", 4, 0},
    {"GMT+5", "Islamabad, Karachi", 5, 0},
    {"GMT+6", "Almaty, Dhaka", 6, 0},
    {"GMT+7", "Bangkok, Jakarta", 7, 0},
    {"GMT+8", "Hong Kong, Beijing", 8, 0},
    {"GMT+9", "Tokyo, Osaka", 9, 0},
    {"GMT+10", "Sydney, Melbourne, Guam", 10, 0},
    {"GMT+11", "Magadan, Soloman Is.", 11, 0},
    {"GMT+12", "Fiji, Wellington, Auckland", 12, 0},
};

int tzone_lookup(const char *arg)
{
    int i;

    for(i = 0; i < MAX_TZONE; i++)
    {
        if(!str_cmp(arg, tzone_table[i].name))
            return i;
    }

    for(i = 0; i < MAX_TZONE; i++)
    {
        if(is_name(arg, tzone_table[i].zone))
            return i;
    }
    return -1;
}

/* Some examples:
    str_time(current_time, ch->pcdata->timezone, "%I:%M:%S");
    str_time(-1, -1, NULL);
*/    
char *str_time(time_t timet, int tz, const char *format)
{
	static char buf_new[5][100];
	static int i;
    char *result;

	// rotate buffers
    i++;
	i %= 5;
	result = buf_new[i];

    if (timet <= 0)
    {
        timet = current_time;
    }
    if(tz > -1 && tz < MAX_TZONE)
    {
        timet += timezone; /* timezone external variable in time.h holds the 
                            difference in seconds to GMT. */
        timet += (60 * 60 * tzone_table[tz].gmt_offset);  /* Add the offset hours */
    }
    strftime(result, 100, format != NULL ? format : "%a %b %d %I:%M:%S %p %Y", 
        localtime(&timet));

	return result;
}

void do_timezone(CHAR_DATA *ch, char *argument)
{
    int i;
    char buf[MAX_STRING_LENGTH];

    if(IS_NPC(ch))
        return;

    if(argument[0] == '\0')
    {
        sprintf(buf, "%-6s %-30s (%s)\n\r", "Name", "City/Zone Crosses", "Time");
        send_to_char(buf, ch);
        send_to_char("-------------------------------------------------------------------------\n\r", ch);
        for(i = 0; i < MAX_TZONE; i++)
        {
            sprintf(buf, "%-6s %-30s (%s)\n\r", tzone_table[i].name,
                tzone_table[i].zone, str_time(current_time, i, NULL));
            send_to_char(buf, ch);
        }
        send_to_char("-------------------------------------------------------------------------\n\r", ch);
        return;
    }

    i = tzone_lookup(argument);

    if(i == -1)
    {
        send_to_char("That time zone does not exists.\n\r", ch);
        return;
    }

    ch->pcdata->timezone = i;
    sprintf(buf, "Your time zone is now %s %s (%s)\n\r", tzone_table[i].name,
                tzone_table[i].zone, str_time(current_time, i, NULL));
    send_to_char(buf, ch);
}