Page 1 of 2

nTimestamp to cDate and cTime

Posted: Mon Mar 13, 2017 4:47 pm
by jairpinho
Hi, I need to convert a timestamp value to date and time in harbor unixtime format


1489364562.247 = 2017-03-13 00:22:42.247 converter Converted web site in php

nValue := 1489364562.247

cDate := ??
Ctime := ??

Re: nTimestamp to cDate and cTime

Posted: Mon Mar 13, 2017 5:27 pm
by Rathinagiri
It is number of seconds from 1st of January of 1970.

We can write a small module to divide the above number by 60, 60, 24 taking only quotients.

Reminders can be multiplied by 60, 60, 24 to find out the seconds, minutes and hours respectively.

Re: nTimestamp to cDate and cTime

Posted: Mon Mar 13, 2017 5:56 pm
by jairpinho
There is no function in harbor to do this, could you give an example of your explanation how can I get the date and the time

Re: nTimestamp to cDate and cTime

Posted: Mon Mar 13, 2017 5:57 pm
by jairpinho
Rathinagiri wrote: Mon Mar 13, 2017 5:27 pm It is number of seconds from 1st of January of 1970.

We can write a small module to divide the above number by 60, 60, 24 taking only quotients.

Reminders can be multiplied by 60, 60, 24 to find out the seconds, minutes and hours respectively.

There is no function in harbor to do this, could you give an example of your explanation how can I get the date and the time,
I can not return the date yyyymmdd from seconds

Re: nTimestamp to cDate and cTime

Posted: Mon Mar 13, 2017 7:20 pm
by KDJ
jairpinho

See in PeteWG's "Harbour functions dictionary": https://github.com/Petewg/V-harbour-core/wiki/hb_T
TimeStamp functions: hb_TSToStr, hb_TSToUTC, hb_TtoC, etc.

Re: nTimestamp to cDate and cTime

Posted: Tue Mar 14, 2017 4:45 pm
by jairpinho
KDJ wrote: Mon Mar 13, 2017 7:20 pm jairpinho

See in PeteWG's "Harbour functions dictionary": https://github.com/Petewg/V-harbour-core/wiki/hb_T
TimeStamp functions: hb_TSToStr, hb_TSToUTC, hb_TtoC, etc.

This is for the format = 2017-03-13 00:22:42.247, My format is decimal in seconds = 1489364562.247

Re: nTimestamp to cDate and cTime

Posted: Tue Mar 14, 2017 6:09 pm
by Rathinagiri
Jair,

Check up this:

Code: Select all

#include <hmg.ch>

Function Main
   msginfo( getDateTime( 1489364562.247 ) )
Return

function getDateTime( nTime )
   set cent on
   cMilliSeconds := padl( mod( nTime, 1 ) * 1000, 3, '0' )
   nTime := int( nTime )
   cSeconds := padl( mod( nTime, 60 ), 2, '0' )
   nTime := int( nTime / 60 )
   cMinutes := padl( mod( nTime, 60 ), 2, '0' )
   nTime := int( nTime / 60 )
   cHours := padl( mod( nTime, 24 ), 2, '0' )
   nTime := int( nTime / 24 )
   dFirstDate := stod( '19700101' )
   dDate := dFirstDate + nTime
   cYear := padl( year( dDate ), 4, '0' )
   cMonth := padl( month( dDate ), 2, '0' )
   cDay := padl( day( dDate ), 2, '0' )
    // 2017-03-13 00:22:42.247
   cReturn := cYear + '-' + cMonth + '-' + cDay + ' ' + cHours + ':' + cMinutes + ':' + cSeconds + '.' + cMilliSeconds
   return cReturn

Re: nTimestamp to cDate and cTime

Posted: Tue Mar 14, 2017 8:52 pm
by jairpinho
Rathinagiri wrote: Tue Mar 14, 2017 6:09 pm Jair,

Check up this:

Code: Select all

#include <hmg.ch>

Function Main
   msginfo( getDateTime( 1489364562.247 ) )
Return

function getDateTime( nTime )
   set cent on
   cMilliSeconds := padl( mod( nTime, 1 ) * 1000, 3, '0' )
   nTime := int( nTime )
   cSeconds := padl( mod( nTime, 60 ), 2, '0' )
   nTime := int( nTime / 60 )
   cMinutes := padl( mod( nTime, 60 ), 2, '0' )
   nTime := int( nTime / 60 )
   cHours := padl( mod( nTime, 24 ), 2, '0' )
   nTime := int( nTime / 24 )
   dFirstDate := stod( '19700101' )
   dDate := dFirstDate + nTime
   cYear := padl( year( dDate ), 4, '0' )
   cMonth := padl( month( dDate ), 2, '0' )
   cDay := padl( day( dDate ), 2, '0' )
    // 2017-03-13 00:22:42.247
   cReturn := cYear + '-' + cMonth + '-' + cDay + ' ' + cHours + ':' + cMinutes + ':' + cSeconds + '.' + cMilliSeconds
   return cReturn

Thank you worked just one detail at the time(cHours), maybe I have to add timezone, so returned
2017-03-13 0.:22:42.247

Re: nTimestamp to cDate and cTime

Posted: Wed Mar 15, 2017 1:13 am
by jairpinho
Thank you all with the help of the Rathinagiri I made correction changes and it worked perfect

Code: Select all

Function getDateTime( nTime )
   set cent on
   cMilliSeconds := padl( STRZERO( mod( nTime, 1 )*1000  ,3) , 3, '0' )
   nTime := int( nTime )
   cSeconds := padl( STRZERO( mod( nTime, 60 ), 2 ), 2, '0' )
   
   nTime := int( nTime / 60 )
   cMinutes := padl( STRZERO( mod( nTime, 60 ), 2 ), 2, '0' )
   
   nTime := int( nTime / 60 )
   cHours :=  padl( STRZERO( mod( nTime, 24 ), 2 ) , 2, '0' ) 
   
   nTime := int( nTime / 24 )
   dFirstDate := stod( '19700101' )
   dDate := dFirstDate + nTime
   cYear := padl( year( dDate ), 4, '0' )
   cMonth := padl( month( dDate ), 2, '0' )
   cDay := padl( day( dDate ), 2, '0' )
   cReturn := cYear + '-' + cMonth + '-' + cDay + ' ' + cHours + ':' + cMinutes + ':' + cSeconds + '.' + cMilliSeconds
Return(cReturn)
   


Re: nTimestamp to cDate and cTime

Posted: Wed Mar 15, 2017 6:07 am
by KDJ
This code can be a little shortened:

Code: Select all

FUNCTION Main()
  LOCAL nSeconds := 1489364562.247

  MsgBox(HB_TStoStr(t"1970-01-01" + nSeconds /60 /60 /24))

RETURN NIL