Hello,
Is there a function available to convert UTC YYYY/MM/DD MM:SS:SS to local date and time if I pass a parameter such as ET, CT, MT or PT for USA time zones with consideration of DST?
Thank you!
#include "fivewin.ch"
static atzones := { ;
{ "ET", -5, -4 }, ; // Eastern
{ "CT", -6, -5 }, ; // Central
{ "MT", -7, -6 }, ; // Mountain
{ "PT", -8, -7 }, ; // Pacific
{ "AT", -9, -8 }, ; // Alaska
{ "HT", -10, -10 }, ; // Hawaii
{ "AR", -7, -7 }, ; // Arizona
{ "SGT", 8, 8 }, ; // Singapore
{ "HKT", 8, 8 } } // Hongkong
static aDsTable := { ;
{ 2015, {^ 2015/03/08 02:00:00 }, {^ 2015/11/01 02:00:00 } }, ;
{ 2016, {^ 2016/03/13 02:00:00 }, {^ 2016/11/06 02:00:00 } }, ;
{ 2017, {^ 2017/03/12 02:00:00 }, {^ 2017/11/05 02:00:00 } }, ;
{ 2018, {^ 2018/03/11 02:00:00 }, {^ 2018/11/04 02:00:00 } }, ;
{ 2019, {^ 2019/03/10 02:00:00 }, {^ 2019/11/03 02:00:00 } } }
//----------------------------------------------------------------------------//
function USATimeTest() // this function is for testing only
local tUTC := DateTime()
local tLocal
SET DATE AMERICAN
SET CENTURY ON
SET TIME FORMAT TO "HH:MM"
// change the test as you like
do while msgget( "USA UTC/Local Conversion", "Enter UTC Date Time", @tUTC )
? tUtc, tLocal := USA_UTCtoLocal( tUtc, "CT" ), USA_LocalToUTC( tLocal, "CT" )
enddo
return nil
//----------------------------------------------------------------------------//
function USA_UTCtoLocal( tUtc, cTimeZone )
local nAt, tLocal
local aInfo := Array( 4 )
DEFAULT cTimeZone := "PT"
cTimeZone := Upper( cTimeZone )
nAt := AScan( aTzones, { |a| a[ 1 ] == cTimeZone } )
if nAt == 0
MsgAlert( "Invalid TimeZone " + cTimeZone )
else
if aTZones[ nAt, 2 ] == aTZones[ nAt, 3 ]
// no daylight savings
tLocal := tUtc + ( aTZones / 24.0 )
else
aInfo[ 1 ] := ATZones[ nAt, 2 ] / 24.0
aInfo[ 2 ] := ATZones[ nAt, 3 ] / 24.0
nAt := AScan( aDsTable, { |a| a[ 1 ] == Year( tUtc ) } )
if nAt == 0
MsgAlert( "Date out of range" )
else
aInfo[ 3 ] := ADsTable[ nAt, 2 ] - aInfo[ 1 ]
aInfo[ 4 ] := ADsTable[ nAt, 3 ] - aInfo[ 2 ]
if tUtc >= aInfo[ 3 ] .and. tUtc <= aInfo[ 4 ]
tLocal := tUtc + aInfo[ 2 ]
else
tLocal := tUtc + aInfo[ 1 ]
endif
endif
endif
endif
return tLocal
//----------------------------------------------------------------------------//
function USA_LocalToUTC( tLocal, cTimeZone )
local nAt, tUtc
local aInfo := Array( 4 )
DEFAULT cTimeZone := "PT"
cTimeZone := Upper( cTimeZone )
nAt := AScan( aTzones, { |a| a[ 1 ] == cTimeZone } )
if nAt == 0
MsgAlert( "Invalid TimeZone " + cTimeZone )
else
if aTZones[ nAt, 2 ] == aTZones[ nAt, 3 ]
// no daylight savings
tUTc := tLocal - ( aTZones / 24.0 )
else
aInfo[ 1 ] := ATZones[ nAt, 2 ] / 24.0
aInfo[ 2 ] := ATZones[ nAt, 3 ] / 24.0
nAt := AScan( aDsTable, { |a| a[ 1 ] == Year( tLocal ) } )
if nAt == 0
MsgAlert( "Date out of range" )
else
aInfo[ 3 ] := ADsTable[ nAt, 2 ]
aInfo[ 4 ] := ADsTable[ nAt, 3 ]
if tLocal >= aInfo[ 3 ] .and. tLocal <= aInfo[ 4 ]
tUTC := tLocal - aInfo[ 2 ]
else
tUTC := tLocal - aInfo[ 1 ]
endif
endif
endif
endif
return tUTC
//----------------------------------------------------------------------------//
bpd2000 wrote:Dear Rao,
When we use year 2009 as per example above we receive error as "Date out of range"
Kindly verify
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot] and 105 guests