nTimestamp to cDate and cTime

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

nTimestamp to cDate and cTime

Post 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 := ??
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: nTimestamp to cDate and cTime

Post 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.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: nTimestamp to cDate and cTime

Post 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
Last edited by jairpinho on Mon Mar 13, 2017 5:57 pm, edited 1 time in total.
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: nTimestamp to cDate and cTime

Post 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
Last edited by jairpinho on Tue Mar 14, 2017 4:46 pm, edited 1 time in total.
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: nTimestamp to cDate and cTime

Post 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.
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: nTimestamp to cDate and cTime

Post 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
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: nTimestamp to cDate and cTime

Post 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
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: nTimestamp to cDate and cTime

Post 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
Attachments
get_data.gif
get_data.gif (51.08 KiB) Viewed 4080 times
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: nTimestamp to cDate and cTime

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

Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: nTimestamp to cDate and cTime

Post 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
Post Reply