/*
 * Xailer source code:
 *
 * Datetime.c
 * Funciones de fecha y hora
 *
 * Copyright 2003, 2007 Pedro Gil
 * Copyright 2003, 2007 Xailer.com
 * All rights reserved
 *
 */

#include <windows.h>
#include <xailer.h>


//--------------------------------------------------------------------------

static SYSTEMTIME st;
static LCID id;
static PHB_ITEM pFormats;

static BOOL CALLBACK DateFormatEnumProc( LPTSTR lpszFormatString )
{
   PHB_ITEM pDate = hb_itemNew( NULL );
   char szDate[ 256 ];

   GetDateFormat( id, 0, &st, lpszFormatString, szDate, 256 );

   if( hb_arrayScan( pFormats, hb_itemPutC( pDate, szDate ), NULL, NULL, TRUE ) == 0 )
      hb_arrayAdd( pFormats, pDate );

   hb_itemRelease( pDate );

   return TRUE;
}

//--------------------------------------------------------------------------

static BOOL CALLBACK TimeFormatEnumProc( LPTSTR lpszFormatString )
{
   PHB_ITEM pTime = hb_itemNew( NULL );
   char szTime[ 256 ];

   GetTimeFormat( id, 0, &st, lpszFormatString, szTime, 256 );

   if( hb_arrayScan( pFormats, hb_itemPutC( pTime, szTime ), NULL, NULL, TRUE ) == 0 )
      hb_arrayAdd( pFormats, pTime );

   hb_itemRelease( pTime );

   return TRUE;
}

//--------------------------------------------------------------------------

XA_FUNC( DATETIMEFORMAT )     // DateTimeFormat() --> aFormats
{
   GetLocalTime( &st );
   id = GetUserDefaultLCID();

   pFormats = hb_itemArrayNew( 0 );

   EnumDateFormats( ( DATEFMT_ENUMPROC ) DateFormatEnumProc, id, DATE_SHORTDATE );
   EnumDateFormats( ( DATEFMT_ENUMPROC ) DateFormatEnumProc, id, DATE_LONGDATE );
   EnumTimeFormats( ( DATEFMT_ENUMPROC ) TimeFormatEnumProc, id, 0 );

   hb_itemReturnForward( pFormats );
   hb_itemRelease( pFormats );
}

//--------------------------------------------------------------------------

XA_FUNC( SETDATE )   // SetDate( [<nDay>], [<nMonth>], [<nYear>] ) --> lSuccess
{
   GetLocalTime( &st );

   st.wDay   = HB_ISNUM( 1 ) ? hb_parni( 1 ) : st.wDay;
   st.wMonth = HB_ISNUM( 2 ) ? hb_parni( 2 ) : st.wMonth;
   st.wYear  = HB_ISNUM( 3 ) ? hb_parni( 3 ) : st.wYear;

   hb_retl( SetLocalTime( &st ) );
}

//--------------------------------------------------------------------------

XA_FUNC( SETTIME )   // SetTime( [<nHour>], [<nMinute>], [<nSecond>], [<nMillisecond>] ) --> lSuccess
{
   GetLocalTime( &st );

   st.wHour         = HB_ISNUM( 1 ) ? hb_parni( 1 ) : st.wHour;
   st.wMinute       = HB_ISNUM( 2 ) ? hb_parni( 2 ) : st.wMinute;
   st.wSecond       = HB_ISNUM( 3 ) ? hb_parni( 3 ) : st.wSecond;
   st.wMilliseconds = HB_ISNUM( 4 ) ? hb_parni( 4 ) : st.wMilliseconds;

   hb_retl( SetLocalTime( &st ) );
}

//--------------------------------------------------------------------------

XA_FUNC( GETTIMEZONEINFORMATION ) // GetTimeZoneInformation() --> hash{ Bias, StandardName, StandardDate, StandardBias, DaylightName, DaylightDate, DaylightBias }
{
   TIME_ZONE_INFORMATION tz;
   if( GetTimeZoneInformation( &tz ) )
      {
      PHB_ITEM pItem, pKey, pValue;
      pItem = hb_hashNew( NULL );
      pKey = hb_itemNew( NULL );
      pValue = hb_itemNew( NULL );

      hb_itemPutC( pKey, "Bias" );
      hb_itemPutNL( pValue, tz.Bias );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "StandardName" );
      hb_itemPutC( pValue, (char *) tz.StandardName );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "StandardDate" );
      hb_itemPutD( pValue, tz.StandardDate.wYear, tz.StandardDate.wMonth, tz.StandardDate.wDay );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "StandardBias" );
      hb_itemPutNL( pValue, tz.StandardBias );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "DaylightName" );
      hb_itemPutC( pValue, (char *) tz.DaylightName );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "DaylightDate" );
      hb_itemPutD( pValue, tz.DaylightDate.wYear, tz.DaylightDate.wMonth, tz.DaylightDate.wDay );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemPutC( pKey, "DaylightBias" );
      hb_itemPutNL( pValue, tz.DaylightBias );
      hb_hashAdd( pItem, pKey, pValue );

      hb_itemRelease( pKey );
      hb_itemRelease( pValue );
      hb_itemReturnRelease( pItem );
      }
   else
      hb_ret();
}

//--------------------------------------------------------------------------
