FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour UTC to Local
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
UTC to Local
Posted: Mon Apr 04, 2016 06:16 PM

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!

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: UTC to Local
Posted: Mon Apr 04, 2016 06:40 PM
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: UTC to Local
Posted: Tue Apr 05, 2016 02:38 PM
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: UTC to Local
Posted: Wed Apr 06, 2016 04:48 AM

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.

Regards



G. N. Rao.

Hyderabad, India
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: UTC to Local
Posted: Thu Apr 07, 2016 03:06 AM

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,

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: UTC to Local
Posted: Thu Apr 07, 2016 03:09 AM

Dear Rao,

Where can I get details about UTCtoLocalWithDST and STOT?

Thank you,

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: UTC to Local
Posted: Thu Apr 07, 2016 05:07 PM
This simplified program should be enough for your requirements.
Code (fw): Select all Collapse
#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

//----------------------------------------------------------------------------//
Regards



G. N. Rao.

Hyderabad, India
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: UTC to Local
Posted: Mon Apr 11, 2016 01:29 PM

Dear Rao,

Thank you. I will be implementing and testing today.

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: UTC to Local
Posted: Mon Apr 11, 2016 07:58 PM

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!

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 708
Joined: Fri Oct 28, 2005 09:53 AM
Re: UTC to Local
Posted: Mon Apr 11, 2016 09:14 PM

Hi Rao,

I used HB_DateTime to resolve this issue.

Sincerely,

*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: UTC to Local
Posted: Mon Apr 11, 2016 10:18 PM

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')

Regards



G. N. Rao.

Hyderabad, India
Posts: 153
Joined: Tue Aug 05, 2014 09:48 AM
Re: UTC to Local
Posted: Tue Apr 12, 2016 04:16 AM

Dear Rao,
When we use year 2009 as per example above we receive error as "Date out of range"
Kindly verify

Regards, Greetings



Try FWH. You will enjoy it's simplicity and power.!
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: UTC to Local
Posted: Tue Apr 12, 2016 04:20 AM
bpd2000 wrote:Dear Rao,
When we use year 2009 as per example above we receive error as "Date out of range"
Kindly verify

Yes.
For the sample, I entered table of DST for years from 2015 to 2019. This table works for USA for these years. If they want for earlier years, they need to enter information for earlier years too.
If you want for other time zones, you need to enter this information for other timezones.

This sample provides the logic to form the basis for individual implementations.
Regards



G. N. Rao.

Hyderabad, India
Posts: 153
Joined: Tue Aug 05, 2014 09:48 AM
Re: UTC to Local
Posted: Tue Apr 12, 2016 07:41 AM

Thank you for more info

Regards, Greetings



Try FWH. You will enjoy it's simplicity and power.!

Continue the discussion