Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Fri Nov 24, 2017 12:11 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Time format
PostPosted: Wed Feb 18, 2009 6:37 am 
Offline

Joined: Wed Feb 11, 2009 9:32 am
Posts: 34
Hello,

Is there a way of formatting time based on milliseconds. I see functions to format time but it seems to format the devices time. I'm getting time in milliseconds from an external source and I need to have it formatted and displayed. Is this possible?

Thank you


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Wed Feb 18, 2009 9:45 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
This might get you started

'Time is the number of milliseconds elapsed since midnight
Code:
func FormatMSTime (time as integer) returns string
    dim h, m, s, ms as integer

    ms = time mod 1000
    time = time / 1000

    s = time mod 60
    time = time / 60

    m = time mod 60
    time = time / 60

    h = time

    return cprintf("%02d:", h) + cprintf("%02d:", m) + cprintf("%02d.", s) + cprintf("%03d", ms)
endfunc

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 6:09 am 
Offline

Joined: Wed Feb 11, 2009 9:32 am
Posts: 34
Jeremy,

Thank you for the code. Unfortunately the time I'm getting is unix time which is milliseconds from 1970 and carried in 64bits. Any ideas?

Thanks,

Joe


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 7:55 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Are you trying to get the date out of that value or just the time? If it is just the time, the code I sent you should mostly work -- the key changes would be extracting the low order 32 bits out of the 64 bit value and then performing the modulus division to keep h between 0 and 23.

If you need the date, things are a bit trickier. Let me think on that for a sec.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 9:12 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Ignore my last post, I was obviously incorrect in it. Still trying to see if I can figure out a solution

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 9:53 am 
Offline

Joined: Wed Feb 11, 2009 9:32 am
Posts: 34
Jeremy

My source is feeding me two int's seconds and milliseconds. I have to convert those to a long then do the correct math. But as I think about it, let me see what the seconds mean. It's probably seconds from 1970 but let me make sure. Don't spend too much time at least until I find out a work around. I may have another work around as well.

Joe


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 10:00 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
If you have seconds since 1970 available then I have a (relatively) simple solution for you.

Actually I have just figured out the proper algorithm for dealing with 64 bit integer expressing milliseconds since 1970, I just need to implement it in Qlarity.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Fri Feb 20, 2009 12:14 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Ok, I went ahead and did the full blown solution. I have not tested this at all, so there are, without doubt some bugs, but the general logic should be in the right direction.

First of all, there is an object in QF 2.6 (I don't think it was ever in 2.5) called DateTimeFunctions -- this provides a rich set of methods for manipulating and displaying dates and times -- but they are all based on seconds since Jan 1, 2000. So your problem decomposes into how to convert your 64 bit time (ms since 1970) into seconds since 2000. After that you can use the DateTimeFunctions object. (Specifically the ConvertToRTCTime method will be of interest).

The following block of code should convert your time into seconds since 2000 (as well as return the number of ms seperately)
Code:
func MSTimeToSecAndMS(timeStr[] as byte, secsSince2000 as reference to integer, ms as reference to integer)
    'This assumes that timeStr is a 64 bit integer, that is stored in big endian format.
    'timeStr[] represents the number of milliseconds elapsed since Jan 1, 1970
    dim tmp1 as unibyte
    dim tmp2 as integer
    dim u[4] as integer
    dim i as integer

    'Change this loop if the value is little endian
    for i = 0 to 3
        FromBytes(tmp1, mid(timeStr, 2*i, 2), true)
        u[i] = tmp1
    next

    'u now contans the 64 bit value broken into 16 bit chunks, that we can perform long division on
    tmp2 := 0
    for i = 0 to 3
        u[i] = u[i] + tmp2 * 0x10000
        tmp2 = u[i] mod 1000
        u[i] = u[i] / 1000
    next

    'tmp2 now contains the number of milliseconds.  u now contains the 64 bit value, broken into
    '16 bit chunks and divided by 1000.  In 2038 we could have an overflow problem.
    ms = tmp2

    'Next, lets get the seconds back to an integer
    'The first two 16 bit chunks of u[i] should now be 0 (assuming it is before 2038, possibly before 2106)
    'depending on how the signs fall out
    if (u[0] <> 0) or (u[1] <> 0) then
        throw ("MSTimeToSeconds", "Invalid source time!")
    endif

    tmp2 = 0x10000 * u[2] + u[3]

    'tmp2 is now the seconds elapsed since 1970.  Convert that to seconds since 2000
    tmp2 := tmp2 - 946684800

    secsSince2000 = tmp2
endfunc

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Mon Feb 23, 2009 6:51 am 
Offline

Joined: Wed Feb 11, 2009 9:32 am
Posts: 34
Jeremy,

Thank you for this, but I have Qlarity 2.50. The download section of your site only sends 2.5 not 2.6. How do I get 2.6 so I can use these new functions date and time functions?

Thanks,

Update: I may have what I needed. Thanks.


Last edited by Fonzy on Mon Feb 23, 2009 7:46 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Time format
PostPosted: Mon Feb 23, 2009 7:38 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
2.60 is fully released. Unfortunately, there is lag between the release and when the web team updates the site.

Until then, you can get the 2.50 release from www.qsicorp.com/engfiles.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group