UTC to Local

Post Reply
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

UTC to Local

Post by cdmmaui »

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
User avatar
cnavarro
Posts: 5792
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: UTC to Local

Post by cnavarro »

C. Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
Si alguien te dice que algo no se puede hacer, recuerda que esta hablando de sus limitaciones, no de las tuyas.
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: UTC to Local

Post by James Bott »

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: UTC to Local

Post by nageswaragunupudi »

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
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

Re: UTC to Local

Post by cdmmaui »

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
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

Re: UTC to Local

Post by cdmmaui »

Dear Rao,

Where can I get details about UTCtoLocalWithDST and STOT?

Thank you,
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: UTC to Local

Post by nageswaragunupudi »

This simplified program should be enough for your requirements.

Code: Select all

#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
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

Re: UTC to Local

Post by cdmmaui »

Dear Rao,

Thank you. I will be implementing and testing today.
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

Re: UTC to Local

Post by cdmmaui »

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
User avatar
cdmmaui
Posts: 653
Joined: Fri Oct 28, 2005 9:53 am
Location: The Woodlands - Dallas - Scottsdale - London
Contact:

Re: UTC to Local

Post by cdmmaui »

Hi Rao,

I used HB_DateTime to resolve this issue.

Sincerely,
*~*~*~*~*~*~*~*~*~*
Darrell Ortiz
CDM Software Solutions, Inc.
https://www.cdmsoft.com
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: UTC to Local

Post by nageswaragunupudi »

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
User avatar
bpd2000
Posts: 153
Joined: Tue Aug 05, 2014 9:48 am
Location: India

Re: UTC to Local

Post by bpd2000 »

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.!
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: UTC to Local

Post by nageswaragunupudi »

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
User avatar
bpd2000
Posts: 153
Joined: Tue Aug 05, 2014 9:48 am
Location: India

Re: UTC to Local

Post by bpd2000 »

Thank you for more info
Regards, Greetings

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