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.

CRBasic: Converting time


MortenS Jun 16, 2011 12:11 PM

Hi

I need to convert this number: 35523.50000 into a date/time string as 12:00:00 3-April-1997

It is representing days since 00:00:00 1-Jan-1900


I can do it in excel without any problem, but I need to have the date and time in my CRBasic program on a CR1000 datalogger.

Anyone ?

* Last updated by: MortenS on 6/16/2011 @ 6:13 AM *


aps Jun 16, 2011 01:26 PM

Have a look at the help for secssince1990 instruction and in particular the link to the example at the bottom of the page which shows a trick of saving an integer based time into a table and them reading it back out into a string in different formats. In your case you could need to multiply the number you have by 86400 (and store the result in a LONG to hold the number of secs since 1990) and then work through the example storing the value into the special table and then reading it out again.


MortenS Jun 20, 2011 09:31 AM

Thanks aps

I have looked at that one, but couldn't get it to work.

If you could explain it to me I would be glad

Regards

Morten

* Last updated by: MortenS on 6/20/2011 @ 3:53 AM *


aps Jun 20, 2011 05:42 PM

If you use the program below and enter a value for the days since 1990 into the similarly named variable, it will do the conversion of the value into a date and time with four different options. There is no format exactly as you asked for but one of those formats could easily be improved on by reordering the string (option 2 is closest)

One thing it won't do though is "convert this number: 35523.50000 into a date/time string as 12:00:00 3-April-1997" because that number does not convert to that date, if the first really is days since 1990. You can see at first sight that equates to about 100 years not 7.


----------------

Public UTCTime(4) As String * 30
Public dayssince1990
Public TimeLong As Long
Const Daystosec = 86400

'Define a table used for this conversion only (only one record).
'Note the format of the time is NSEC
DataTable(TimeTable,true,1)
Sample(1,TimeLong,NSEC)
EndTable


BeginProg
Scan (1,Sec,0,0)
TimeLong = dayssince1990 * Daystosec
'Unconditionally store the time to the table
CallTable(TimeTable)
'Show the four possible conversion options.
'Retrieve NSEC value from data table & reformat to string
'Format as "mm/dd/yyyy hr:mm:ss
UTCTime(1) = TimeTable.TimeLong(1,1)
'Format as Internet time ddd, DD MM YYYY HH:MM:SS GMT
UTCTime(2) = TimeTable.TimeLong(2,1)
'Format as "ccyy-mm-dd hr:mm:ss (ISO 8601 Intl Date)
UTCTime(3) = TimeTable.TimeLong(3,1)
'Format as "ccyy-mm-dd hr:mm:ss (ISO 8601 Intl Date)
UTCTime(4) = TimeTable.TimeLong(4,1)
NextScan
EndProg


MortenS Jun 20, 2011 07:09 PM

Hi Andrew

35523.50000 should be as written before 12:00:00 3-April-1997.

The time system used is days since year 1900 00:00:00.
I newer claimed it to be 1990 !

Since I ask is that I can't figure it out using the above mentioned example

Regards

Morten


aps Jun 20, 2011 10:53 PM

My apologies, I misread the original date. A revised example is below which has an offset for the days from 1900 to 1990 (the start of the loggers timebase) before converting to secs since 1990 (the loggers serial time). This works by using built in functions in the logger for converting its serial time to ASCII strings similar to that you want (which are unfortunately only accessible via this devious method).


---
Public UTCTime(4) As String * 30
Public dayssince1900
Public TimeLong As Long
Const Daystosec = 86400
Const Daysto1990 =32874

'Define a table used for this conversion only (only one record).
'Note the format of the time is NSEC
'By storing in this format the logger recognises it to be a
'serial time
DataTable(TimeTable,true,1)
Sample(1,TimeLong,NSEC)
EndTable


BeginProg
Scan (1,Sec,0,0)
TimeLong = (dayssince1900 - daysto1990) * Daystosec
'Unconditionally store the time to the table
CallTable(TimeTable)
'Show the four possible conversion options.
'Retrieve NSEC value from data table & reformat to string
'Format as "mm/dd/yyyy hr:mm:ss
UTCTime(1) = TimeTable.TimeLong(1,1)
'Format as Internet time ddd, DD MM YYYY HH:MM:SS GMT
UTCTime(2) = TimeTable.TimeLong(2,1)
'Format as "ccyy-mm-dd hr:mm:ss (ISO 8601 Intl Date)
UTCTime(3) = TimeTable.TimeLong(3,1)
'Format as "ccyy-mm-dd hr:mm:ss (ISO 8601 Intl Date)
UTCTime(4) = TimeTable.TimeLong(4,1)
NextScan
EndProg

* Last updated by: aps on 6/21/2011 @ 3:46 AM *


MortenS Jun 21, 2011 09:11 AM

Thank you very much

That helped a lot

Regards

Morten


aps Jun 21, 2011 09:55 AM

To convert the UTCTime(2) into the exact format you requested you can do this with this code:

---
Public timearray(4) As String
Public Desiredtime As String *30

SplitStr (timearray(),UTCTime(2)," ",4,4)
Desiredtime = timearray(4)&" "&timearray(1)&"-"&timearray(2)&"-"&timearray(3)
---


One word of warning though. When you read the 1900 serial time stamp into a floating point variable in the logger there is a risk of running out of resolution. This is because the logger only works in "single-precision" floating point unlike Excel which is "double". This means if the time has more than about two digits of significant resolution after the decimal point the converted time may appear to be a few seconds out.

There is as simple way around this if the time is initially read into the logger as a string - you can split the number in two and add the components separately to make up the long serial time value.

* Last updated by: aps on 6/21/2011 @ 3:56 AM *


MortenS Jun 21, 2011 10:03 AM

Thanks again Andrew

Nice to here the warning about the floating point. I dont think it would be a problem, since a resolution i minuts is fine for the task

Regars

Morten

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