diff --git a/harbour/ChangeLog b/harbour/ChangeLog index bde98625be..b5fc81aa2d 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,18 @@ past entries belonging to author(s): Viktor Szakats. */ +2009-07-29 23:46 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) + * include/hbdate.h + * source/common/hbdate.c + * contrib/hbtip/utils.c + * examples/httpsrv/uhttpdc.c + + Added hb_timeUTCOffset( void ) -> long (in seconds) + % Using hb_timeUTCOffset() in TIP_TIMESTAMP() and HB_UTCOFFSET(). + These functions can now be implemented on .prg level if we + add a .prg level wrapper for hb_timeUTCOffer(), but I have no + good idea which format to use to return this value, so maybe + simple signed number of seconds. + 2009-07-29 22:28 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * contrib/hbtip/utils.c * contrib/hbtip/tests/tiptime.prg diff --git a/harbour/contrib/hbtip/utils.c b/harbour/contrib/hbtip/utils.c index 777d469a6c..e096189b7a 100644 --- a/harbour/contrib/hbtip/utils.c +++ b/harbour/contrib/hbtip/utils.c @@ -63,45 +63,24 @@ * */ -#define HB_OS_WIN_USED - #include "hbapi.h" #include "hbapiitm.h" #include "hbapierr.h" #include "hbapifs.h" #include "hbdate.h" -#if defined( HB_OS_WIN ) - #ifndef TIME_ZONE_ID_INVALID - #define TIME_ZONE_ID_INVALID ( DWORD ) 0xFFFFFFFF - #endif -#else - #include -#endif - /************************************************************ * Useful internet timestamp based on RFC822 */ HB_FUNC( TIP_TIMESTAMP ) { + static const char * s_days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; + static const char * s_months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + char szRet[ 64 ]; - -/* sadly, many strftime windows implementations are broken */ -#if defined( HB_OS_WIN ) - - int iYear, iMonth, iDay, iHour, iMinute, iSecond, iMSec, iTZBias; - - /* Detect TZ bias */ - { - TIME_ZONE_INFORMATION tzInfo; - DWORD retval = GetTimeZoneInformation( &tzInfo ); - - if( retval == TIME_ZONE_ID_INVALID ) - iTZBias = 0; - else - iTZBias = -( tzInfo.Bias + ( retval == TIME_ZONE_ID_STANDARD ? tzInfo.StandardBias : tzInfo.DaylightBias ) ); - } + int iYear, iMonth, iDay, iHour, iMinute, iSecond, iMSec; + long lOffset = hb_timeUTCOffset(); if( HB_ISDATE( 1 ) ) { @@ -123,67 +102,11 @@ HB_FUNC( TIP_TIMESTAMP ) iSecond = ( int ) ( ulHour % 60 ); } - { - static const char * s_days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - static const char * s_months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - - hb_snprintf( szRet, sizeof( szRet ), "%s, %d %s %d %02d:%02d:%02d %+03d%02d", - s_days[ hb_dateDOW( iYear, iMonth, iDay ) - 1 ], iDay, s_months[ iMonth - 1 ], - iYear, iHour, iMinute, iSecond, - ( int ) iTZBias / 60, - ( int ) iTZBias % 60 ); - } - -#else - - struct tm tmTime; - time_t current; - - /* init time structure anyway */ - time( ¤t ); -#if defined( HB_HAS_LOCALTIME_R ) - localtime_r( ¤t, &tmTime ); -#else - tmTime = *localtime( ¤t ); -#endif - - if( HB_ISDATE( 1 ) ) - { - int iYear, iMonth, iDay; - - hb_dateDecode( hb_pardl( 1 ), &iYear, &iMonth, &iDay ); - - tmTime.tm_year = iYear - 1900; - tmTime.tm_mon = iMonth - 1; - tmTime.tm_mday = iDay; - } - else if( HB_ISDATETIME( 1 ) ) - { - int iYear, iMonth, iDay, iHour, iMinute, iSecond, iMSec; - - hb_timeStampUnpack( hb_partd( 1 ), &iYear, &iMonth, &iDay, &iHour, &iMinute, &iSecond, &iMSec ); - - tmTime.tm_year = iYear - 1900; - tmTime.tm_mon = iMonth - 1; - tmTime.tm_mday = iDay; - tmTime.tm_hour = iHour; - tmTime.tm_min = iMinute; - tmTime.tm_sec = iSecond; - } - - /* For compatibility */ - if( HB_ISNUM( 2 ) ) - { - ULONG ulHour = hb_parnl( 2 ); - - tmTime.tm_hour = ulHour / 3600; - tmTime.tm_min = ( ulHour % 3600 ) / 60; - tmTime.tm_sec = ulHour % 60; - } - - strftime( szRet, sizeof( szRet ), "%a, %d %b %Y %H:%M:%S %z", &tmTime ); - -#endif + hb_snprintf( szRet, sizeof( szRet ), "%s, %d %s %d %02d:%02d:%02d %+03d%02d", + s_days[ hb_dateDOW( iYear, iMonth, iDay ) - 1 ], iDay, s_months[ iMonth - 1 ], + iYear, iHour, iMinute, iSecond, + ( int ) lOffset / 3600, + ( int ) ( lOffset % 3600 ) / 60 ); hb_retc( szRet ); } diff --git a/harbour/examples/httpsrv/uhttpdc.c b/harbour/examples/httpsrv/uhttpdc.c index 831304fad0..2fb2e3e6e3 100644 --- a/harbour/examples/httpsrv/uhttpdc.c +++ b/harbour/examples/httpsrv/uhttpdc.c @@ -143,35 +143,11 @@ HB_FUNC( WIN_SYSREFRESH ) HB_FUNC( HB_UTCOFFSET ) { char szRet[ 6 ]; + long offset = hb_timeUTCOffset(); -#if defined( HB_OS_WIN ) - { - TIME_ZONE_INFORMATION tzInfo; - DWORD retval = GetTimeZoneInformation( &tzInfo ); - - if( retval == TIME_ZONE_ID_INVALID ) - tzInfo.Bias = 0; - else - tzInfo.Bias = -( tzInfo.Bias + ( retval == TIME_ZONE_ID_STANDARD ? tzInfo.StandardBias : tzInfo.DaylightBias ) ); - - hb_snprintf( szRet, sizeof( szRet ), "%+03d%02d", - ( int ) ( tzInfo.Bias / 60 ), - ( int ) ( tzInfo.Bias % 60 > 0 ? - tzInfo.Bias % 60 : tzInfo.Bias % 60 ) ); - } -#else - { - struct tm tmTime; - time_t current; - - time( ¤t ); -# if defined( HB_HAS_LOCALTIME_R ) - localtime_r( ¤t, &tmTime ); -# else - tmTime = *localtime( ¤t ); -# endif - strftime( szRet, sizeof( szRet ), "%z", &tmTime ); - } -#endif + hb_snprintf( szRet, sizeof( szRet ), "%+03d%02d", + ( int ) ( offset / 3600 ), + ( int ) ( ( offset % 3600 ) / 60 ) ); hb_retc( szRet ); } diff --git a/harbour/include/hbdate.h b/harbour/include/hbdate.h index 93e15943e6..c4396c9a6a 100644 --- a/harbour/include/hbdate.h +++ b/harbour/include/hbdate.h @@ -104,6 +104,8 @@ extern HB_EXPORT void hb_timeStampGetLocal( int * piYear, int * piMonth, int * p int * piSeconds, int * piMSec ); extern HB_EXPORT void hb_timeStampGet( long * plJulian, long * plMilliSec ); +extern HB_EXPORT long hb_timeUTCOffset( void ); /* in seconds */ + extern HB_EXPORT char * hb_timeStampStrRawPut( char * szDateTime, long lJulian, long lMilliSec ); extern HB_EXPORT void hb_timeStampStrRawGet( const char * szDateTime, long * plJulian, long * plMilliSec ); diff --git a/harbour/source/common/hbdate.c b/harbour/source/common/hbdate.c index 3092c473c7..9bb0c70bfd 100644 --- a/harbour/source/common/hbdate.c +++ b/harbour/source/common/hbdate.c @@ -87,6 +87,9 @@ # define timeb _timeb # define ftime _ftime # endif +# ifndef TIME_ZONE_ID_INVALID +# define TIME_ZONE_ID_INVALID ( DWORD ) 0xFFFFFFFF +# endif #endif @@ -894,3 +897,32 @@ void hb_timeStampUnpackD( double dTimeStamp, if( pdSeconds ) *pdSeconds = ( double ) iSeconds + ( double ) iMSec / 1000; } + +long hb_timeUTCOffset( void ) /* in seconds */ +{ +#if defined( HB_OS_WIN ) + { + TIME_ZONE_INFORMATION tzInfo; + DWORD retval = GetTimeZoneInformation( &tzInfo ); + + if( retval == TIME_ZONE_ID_INVALID ) + return 0; + else + return -( tzInfo.Bias + ( retval == TIME_ZONE_ID_STANDARD ? tzInfo.StandardBias : tzInfo.DaylightBias ) ) * 60; + } +#else + { + struct tm tmTime; + time_t current; + + time( ¤t ); +# if defined( HB_HAS_LOCALTIME_R ) + localtime_r( ¤t, &tmTime ); +# else + tmTime = *localtime( ¤t ); +# endif + + return tmTime.tm_gmtoff; + } +#endif +}