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!
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!
The question is something like this:
? UTCtoLocalWithDST( STOT( "20150310103000" ), "PT" ) --> 10/03/2015 03:30:00
? UTCtoLocalWithDST( STOT( "20160310103000" ), "PT" ) --> 10/02/2015 02:30:00
For this function to return correct results we need to keep tables of DST for all the zones and years we are interested in.
I do not know if there is any existing database of these dates on internet which can be queried and obtainted. If not, an organisation has to maintain its own tables.
Dear Rao,
Thank is exactly what I needed. Thank you!
We are going to build a table and add DST for the coming years and add more years as we get them.
We are tracking in real time so we have time to update tables.
Thanks again for your help!
Sincerely,
Dear Rao,
Where can I get details about UTCtoLocalWithDST and STOT?
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
//----------------------------------------------------------------------------//Dear Rao,
Thank you. I will be implementing and testing today.
Dear Rao or Anyone That Can Help,
I see the sample of tUTC := DateTime() which works fine.
However, I am reading date and time from a DBF and assigning values dTxDate and cTxtime.
How do I get the dTxdate and cTxTime in the correct format returned by DateTime(), can I pass parameters to DateTime(), i.e., tUTC := DateTime( dTxDate, cTxTime)?
I appreciate anyone's assistance!
Hi Rao,
I used HB_DateTime to resolve this issue.
Sincerely,
Better we stop using date (valtyle 'D') and time (valtype 'C') separately and start dealing with date and time as DateTime varialbles (valtype 'T').
For getting current datetime value we use DateTime() in xHarbour and HB_DateTime() in Harbour.
We can store and retrieve DateTime values in DBF by creating Field with Type 'T'
Eg: { "START", 'T', 8, 0 } // Spec for creating table
When we have Date (valtype 'D') and Time (valtype 'C') separately and want to combine the two into a single DateTime variable, we can use the function FW_ADDTIME( dDate, cTime ).
Eg:
dDate := CTOD( "10/10/2009" )
cTime := "11:12:15"
tDateTime := FW_ADDTIME( dDate, cTime ) // --> 10/10/2009 11:12:15 (valtype 'T')
Dear Rao,
When we use year 2009 as per example above we receive error as "Date out of range"
Kindly verify
bpd2000 wrote:Dear Rao,
When we use year 2009 as per example above we receive error as "Date out of range"
Kindly verify
Thank you for more info