nTimestamp to cDate and cTime

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

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

nTimestamp to cDate and cTime

Post by jairpinho » Mon Mar 13, 2017 4:47 pm

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: 5096
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 93 times
Been thanked: 107 times
Contact:

Post by Rathinagiri » 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.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

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

Post by jairpinho » Mon Mar 13, 2017 5:56 pm

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: 160
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Post by jairpinho » Mon Mar 13, 2017 5:57 pm

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: 154
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 120 times

Post by KDJ » 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.

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

Post by jairpinho » Tue Mar 14, 2017 4:45 pm

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: 5096
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 93 times
Been thanked: 107 times
Contact:

Post by Rathinagiri » 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
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

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

Post by jairpinho » Tue Mar 14, 2017 8:52 pm

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 257 times
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br

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

Post by jairpinho » Wed Mar 15, 2017 1:13 am

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: 154
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland
Been thanked: 120 times

Post by KDJ » Wed Mar 15, 2017 6:07 am

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

Who is online

Users browsing this forum: No registered users and 2 guests