Time conversion problem

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Time conversion problem

Post by trevix » Tue Sep 28, 2021 9:12 am

Code: Select all

put 300 into tTime --5 minutes
convert tTime from seconds to short time
put tTime --return "01:05" ?
Why is there the extra hour? What am I missing?
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Time conversion problem

Post by LCMark » Tue Sep 28, 2021 10:25 am

@trevix: 'the seconds' is not a time of day measurement (i.e. on an arbitrary day since midnight) - it is a measure of point in time, i.e. the number of seconds since 1970-01-01 0:00 in UTC.

All other time/date formats in LC are local time so what you are seeing is the fact that "1970-01-01 00:05 UTC" is actually "1:05" in your current system timezone.

Presumably you want to format a time since midnight to the user's local system time settings (and so you have useSystemDate set to true) - I think this should work:

Code: Select all

convert the date to dateItems -- 'gregorian' Y,M,D,0,0,0 form (local time)
put tSeconds div 3600 into item 4 of it
put (tSeconds div 60) mod 60 into item 5 of it
put tSeconds mod 60 into item 6 of it
convert it from dateItems to short time
Here this turns the current date into a (in local time) decomposition - which is easy to substitute a new time into - it then converts this to the (local) short time format (which will be system short time if useSystemDate is used).

The reason this may seem overly complex is just a reflection on how different locales around the world *could* handle dates and times. In full generality, there is 'no such thing' as an isolated time of day - how any given locale displays a time or date (isolated or not) depends on the actual point in time, not (what is essentially) an arbitrary offset from midnight.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Time conversion problem

Post by Klaus » Tue Sep 28, 2021 10:53 am

Hi Trevix,

for an SMPTE-like display of seconds I use this little function:

Code: Select all

function smptelite tSecs
   return format("%02d:%02d:%02d", tSecs div 3600, (tSecs mod 3600) div 60, tSecs mod 60)
end smptelite
put smptelite(300) -> 00:05:00

Best

Klaus

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Time conversion problem

Post by trevix » Tue Sep 28, 2021 5:44 pm

Blame on me. I forgot Greenwich.
Should it be a difference between "the seconds" and "the "system seconds" (which is accepted by the code editor) ?

Also why this

Code: Select all

put the  seconds into tNow
convert tNow to dateitems
put tNow
return the exact time and date of my Mac clock (Rome, I am into a +02 zone)
SHouldn't it be 2 hours before (or later...hu!)

Finally, what is the spidier way to get, as a Dateitems, the local day and time?
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Time conversion problem

Post by dunbarx » Tue Sep 28, 2021 6:07 pm

Finally, what is the spidier way to get, as a Dateitems, the local day and time?
Try:

Code: Select all

 convert the seconds to dateItems
Now, in the local variable 'it", you have the year as item 1, the month as item 2, the date as item 3, the hour as item 4, the minutes as item 5, the seconds as item 6 and the day of the week as item 7.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Time conversion problem

Post by bn » Tue Sep 28, 2021 6:30 pm

trevix wrote:
Tue Sep 28, 2021 5:44 pm
Finally, what is the spidier way to get, as a Dateitems, the local day and time?

Code: Select all

   convert the date && the long time to dateItems
   put it into tDateTimeItems
or

Code: Select all

   put the internet date into tDate2
   convert tDate2 to dateItems
Kind regards
Bernd

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Time conversion problem

Post by trevix » Wed Sep 29, 2021 9:08 am

Dunbarx & Bernd:
As Mark said
@trevix: 'the seconds' is not a time of day measurement (i.e. on an arbitrary day since midnight) - it is a measure of point in time, i.e. the number of seconds since 1970-01-01 0:00 in UTC.
For what I understand (I have to do some testing, changing localization on the Mac, wich is a drag), in order to quickly obtain a DateItems that correctly reflects the Time in a device, not all the proposed solutions should work:

Code: Select all

convert the seconds to dateItems
"The seconds" is UTC, so it doesn't know about my DayLight saving time or my TimeZone. So "convert the seconds to dateItems" will not always reflect the Time of my device.

Code: Select all

 put the internet date into tDate2
convert tDate2 to dateItems
I assume this solution takes in account the time zone so it should be correct. But what aboutDayLight saving???

Code: Select all

convert the date && the long time to dateItems
put it into tDateTimeItems
This is it. No place for mistake since it is the device date and time.
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Time conversion problem

Post by trevix » Wed Sep 29, 2021 9:28 am

Ops, I was wrong:

Code: Select all

put the seconds into tNow
convert tNow to dateItems
always reflect the correct time, no matter the TimeZone or LightSaving.
Sorry
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1209
Joined: Thu Apr 11, 2013 11:27 am

Re: Time conversion problem

Post by LCMark » Wed Sep 29, 2021 9:34 am

@trevix: That is what you should find - yes! Just to clarify...

LC computes all date/time stuff using the same underlying mechanism - everything goes through 'the seconds'. All computers internally store their notion of 'current time' in UTC as the number of seconds since a certain point in time - due to its UNIX heritage, LC chooses 1970-01-01 for this point.

The key difference between `the seconds` and *all* other date formats (including dateItems) is that everything but `the seconds` is in local time - i.e. they are assumed to include the user's local time settings.

So if you do:

Code: Select all

convert the seconds to dateItems
Then the engine knows the input is in UTC seconds, so then adjusts that internally by the local time difference (which includes DST), and then decomposes that into the 6 items Y,M,D,H,M,S.

If you do something like:

Code: Select all

convert the date && the long time to dateItems
Then the engine actually does:

Code: Select all

convert the date && the long time to seconds
convert it to dateItems
Basically, if you convert anything to dateItems, then the dateItems should reflect the local time/date of your device - same for all other (non-seconds) formats.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”