FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour convert this "1458480306" Into 20160320142506
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM

convert this "1458480306" Into 20160320142506

Posted: Sun Mar 20, 2016 04:29 PM
Hi,
"1458480306.jpg" is a name of a pictures in my smartphone.
I have to rename it with this name 20160320142506.jpg


http://www.timestampconvert.com/?go2=true&offset=-1&timestamp=1458480306063&Submit=++++++Convert+to+Date++++++


YYYYMMDDHHMMSS.


Does exist a function?


Many thanks

Marco
Code (fw): Select all Collapse
FUNCTION MAIN
cName := "1458480306"

? MyConvert( cName ) 

RETURN NIL 

FUNCTION Myconvert( cName ) 
LOCAL cReturn 

cReturn := convert( cName ) 


RETURN cReturn
Marco Boschi
info@marcoboschi.it
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM

Re: convert this "1458480306" Into 20160320142506

Posted: Sun Mar 20, 2016 04:48 PM
Try

Code (fw): Select all Collapse
? hb_Ttos( hb_DateTime() )
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: 1091
Joined: Thu Nov 17, 2005 11:08 AM

Re: convert this "1458480306" Into 20160320142506

Posted: Sun Mar 20, 2016 06:26 PM
Many thanks
but I do not explay very wel the problem

This is the string to convert "1458480306"

Unix time, the number of seconds since 00:00:00 UTC on January 1, 1970

into this 20160320142506
or simply into

Date in your timezone*: 20/3/2016, 14:25:06

mucias gracias
Marco Boschi
info@marcoboschi.it
Posts: 1516
Joined: Thu May 27, 2010 02:06 PM

Re: convert this "1458480306" Into 20160320142506

Posted: Sun Mar 20, 2016 09:36 PM

function Main()

local nSeconds:= 1458480306
local nfecha := secondstotime( nSeconds )
msginfo(nFecha)

return nil

//----------------------------------------------------------------//

function secondstotime( nSeconds )

local nDias:= int( nSeconds /( 606024) )
local nResto := nseconds - nDias606024
local nNewDate := ctod("1/1/1970") + nDias
local nHoras := int(nResto/(60
60) )
local nMinutos

nResto := nResto - nHoras 60 60
nHoras := nHoras + 1 // For UTC
nMinutos := int ( nResto / 60 )
nResto := nResto - nMinutos *60
nNewdate := dtos( nNewdate ) + strzero(nHoras,2,0)+ strzero(nMinutos,2,0) + strZero( nResto,2,0)
Return nNewDate

Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM

Re: convert this "1458480306" Into 20160320142506

Posted: Mon Mar 21, 2016 09:31 AM

Many Thanks!

marco

Marco Boschi
info@marcoboschi.it
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

Re: convert this "1458480306" Into 20160320142506

Posted: Thu Mar 24, 2016 09:34 AM
Here is another approach, utilizing (x)Harbour's capabilities of datetime computations and also incorporating the timezone conversion implications.

Code (fw): Select all Collapse
function MyConvert( cUnixName )
return cFileSetExt( Left( TTOS( STOT( "19700101000000" ) + Val( cUnixName ) / ( 24 * 3600 ) + 1/24 ), 14 ), cFileExt( cUnixName ) )


? MyConvert( "1458480306.jpg" ) --> "20160320142506.jpg"

It may be worth examining each calculation involved in the above single line formula. Here it is:



Now, a discussion about TimeZone adjustment and DST. In the above function I just added 1/24 which is +01:00, being the timezone of Italy when DST is not in use. When DST is in use it is going to be +02:00, i.e. 2/24 of a day. So, this forumla should also consider DST.

We need to use the TimeZone offset applicable to the converted UTC date but not the date when the function is called. That means we need to have a look up table.

Here is the revised function taking into account Italy's DST and TimeZone:
Code (fw): Select all Collapse
function MyConvertDST( cUnixName )

   local aDST := { ;
      { {^ 2014/03/30 01:00 }, {^ 2014/10/26 01:00 } }, ;
      { {^ 2015/03/29 01:00 }, {^ 2015/10/25 01:00 } }, ;
      { {^ 2016/03/27 01:00 }, {^ 2016/10/30 01:00 } }, ;
      { {^ 2017/03/26 01:00 }, {^ 2017/10/29 01:00 } }, ;
      { {^ 2018/03/31 01:00 }, {^ 2018/10/28 01:00 } }  }

   local tRet  := STOT( "19700101000000" ) + Val( cUnixName ) / ( 24 * 3600 ) // converted to UTC DateTime

   if AScan( aDST, { |a| tRet >= a[ 1 ] .and. tRet <= a[ 2 ] } ) > 0
      tRet  += 1/24   // Additional for DST
   endif

return cFileSetExt( Left( TTOS( tRet + 1/24 ), 14 ), cFileExt( cUnixName ) )

In above example, if 10 days are added, the result should not be "20160330142506.jpg" but should be "20160330152506.jpg"

We can test the revised function with this:
? MyConvert( "1459344306.jpg" ) --> "20160330142506.jpg"
? MyConvert( "1459344306.jpg" ) --> "20160330152506.jpg"
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM

Re: convert this &quot;1458480306&quot; Into 20160320142506

Posted: Thu Mar 24, 2016 09:38 AM

Day Light Savings:

I am from a tropical country India and we do not have Day Light savings adjustments. So I am not aware of the complexities that arise from this. I have some doubts and seek clarification / guidance from friends who are used to DLS

If an event is said to have happened on 25 Oct 2015 at 02:35 AM in Rome, Italy, how do we know if it happened before the clock was turned back or after? To my mind, this time refers to two different points of time separated by one hour. How do we distinctly identify which one it is?
In other words, if we want to convert this time into UTC, do we covert it as 25 Oct 2015 00:35 or 25 Oct 2015 01:35?

Regards



G. N. Rao.

Hyderabad, India
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM

Re: convert this &quot;1458480306&quot; Into 20160320142506

Posted: Thu Mar 24, 2016 01:16 PM
nageswaragunupudi wrote:Here is another approach, utilizing (x)Harbour's capabilities of datetime computations and also incorporating the timezone conversion implications.

Code (fw): Select all Collapse
function MyConvert( cUnixName )
return cFileSetExt( Left( TTOS( STOT( "19700101000000" ) + Val( cUnixName ) / ( 24 * 3600 ) + 1/24 ), 14 ), cFileExt( cUnixName ) )


Very nice!

EMG
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM

Re: convert this &quot;1458480306&quot; Into 20160320142506

Posted: Thu Mar 24, 2016 03:03 PM

Many Thanks and Happy Easter dear Friends

Marco Boschi
info@marcoboschi.it
Posts: 159
Joined: Wed Mar 28, 2007 01:19 PM

Re: convert this &quot;1458480306&quot; Into 20160320142506

Posted: Thu Mar 24, 2016 04:13 PM

Thanks Rao, for the hint and explanation.
Regards, Euclides.

Continue the discussion