Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Data table column with time in seconds since 1970 or before


3LPs Mar 22, 2022 08:12 PM

Hi,

I am trying to get a timestamp column in my data table that would display time in seconds (with decimals) since 1970. I've read that it is possible to use tablename.timestamp(m,n) to do so but I am struggling to get this to work. Here's my short and simple program that I've been using to test this:

Public PTemp, Batt_volt, Time

'Define Data Tables
DataTable (Test,1,-1) 'Set table size to # of records, or -1 to autoallocate.
DataInterval (0,10,mSec,100)
Sample (1, Time,Long)
Minimum (1,Batt_volt,FP2,False,False)
Sample (1,PTemp,FP2)
EndTable

'Main Program
BeginProg
Scan (10,msec,0,0)
Time=Test.Timestamp(0,0)
PanelTemp (PTemp,15000)
Battery (Batt_volt)

CallTable Test
NextScan
EndProg

Also, could I change the year 1970 by adding the number of seconds corresponding to a defined number of years?

thanks

LP


JDavis Mar 28, 2022 07:18 PM

To get the current time in the scan, use Public.Timestamp.

The seconds and the microseconds have to be read separately. (option 0 and option 7)

They won't both fit into the same Long variable due to range limitation of the variable type.


kanecharles Mar 1, 2024 10:31 AM

Of course, here is a brief response for you:

To create a timestamp column displaying time in seconds (including decimals) since 1970, you can use the "tablename.timestamp(m,n)" function. However, in the code snippet you provided, it seems like calling "Test.Timestamp(0,0)" may not be in the correct syntax. You need to review how to properly use time calculator function in the context of your program.

To change the year 1970 by adding the number of seconds corresponding to a defined number of years, you can calculate the number of seconds equivalent to the number of years and add it to your existing timestamp value.

I hope this helps!


Sam Apr 7, 2024 07:25 PM

This post got bumped by a spam, but I took interest in the responses because it is a good opportunity to highlight other data types. This application should use type LONG or DOUBLE for the Public declared variable Time.

The original post shows the assignment of 

Time=Test.Timestamp(0,0)

where "Time" is (implicitly) define as a FLOAT

Public PTemp, Batt_volt, Time

 This application should instead define and store "Time" as LONG or DOUBLE

Public PTemp, Batt_volt
Public Time as Long
'...
Sample(1,Time,Long)
'...

 or

Public PTemp, Batt_volt
Public Time as Double
'...
Sample(1,Time,IEEE8)
'...

 

Jacob noted the second and microseconds could be store separately, using two LONG.

Public Time As Long
Public FracSec As Long

DataTable (Test,1,-1)
  DataInterval (0,10,mSec,100)
  Sample (1,Time,Long)
  Sample (1,FracSec,Long)
EndTable

BeginProg
  Scan (10,msec,0,0)
    Time=Test.Timestamp(0,1)
    FracSec=Test.Timestamp(7,1)
    CallTable Test
  NextScan
EndProg

 Or you should be able to store in a single DOUBLE.

Public Time As Double
Public FracSec As Double

DataTable (Test,1,-1)
  DataInterval (0,10,mSec,100)
  Sample (1,Time,IEEE8)
EndTable

BeginProg
  Scan (10,msec,0,0)
    Time=Test.Timestamp(0,1)
    FracSec=(Test.Timestamp(7,1))/1000000.0
    Time=Time+FracSec
    CallTable Test
  NextScan
EndProg


peassalad Jun 25, 2024 02:20 AM

It should be noted that you must use long instead of int for the timestamp type if you wish to convert to millseconds for either more scratch games precise timestamps or Javascript Date() object compatibility. DateTime is generally what you should use.To circumvent timezone issues, use UTCNow.

Log in or register to post/reply in the forum.