Page 1 of 1
Time conversion problem
Posted: Tue Sep 28, 2021 9:12 am
by trevix
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
Re: Time conversion problem
Posted: Tue Sep 28, 2021 10:25 am
by LCMark
@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.
Re: Time conversion problem
Posted: Tue Sep 28, 2021 10:53 am
by Klaus
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
Re: Time conversion problem
Posted: Tue Sep 28, 2021 5:44 pm
by trevix
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
Re: Time conversion problem
Posted: Tue Sep 28, 2021 6:07 pm
by dunbarx
Finally, what is the spidier way to get, as a Dateitems, the local day and time?
Try:
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
Re: Time conversion problem
Posted: Tue Sep 28, 2021 6:30 pm
by bn
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
Re: Time conversion problem
Posted: Wed Sep 29, 2021 9:08 am
by trevix
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:
"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.
Re: Time conversion problem
Posted: Wed Sep 29, 2021 9:28 am
by trevix
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
Re: Time conversion problem
Posted: Wed Sep 29, 2021 9:34 am
by LCMark
@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:
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.