Is there a way to convert the number of seconds that have elapsed since an initial known starting time/date, to a real time and date (UTC)?
Announcement
Collapse
No announcement yet.
Real time and date from elapsed seconds
Collapse
X
-
You could start with the DATE$ and TIME$ function. That tells you what date and time it is now, in LOCAL date/time.
But.... Since there are no PB-intrinsic functions to convert to UTC, instead of DATE$ and TIME$ you can use the Windows API function "GetSystemTime()" to get the current time in UTC.
You can take your known "start date and time" (which I assume from the question you have in UTC) and just do "date and time arithmetic."
Search here (Source Code Forum) for "Time" and "Date" functions to get the difference in many units of measure. There are multiple contributions from which to choose.
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
mmatti[email protected]
http://www.talsystems.com
-
Code:#COMPILE EXE #DIM ALL #INCLUDE "win32api.inc" FUNCTION PBMAIN () AS LONG LOCAL st AS SYSTEMTIME LOCAL ft AS FILETIME LOCAL quadtime AS QUAD LOCAL elapsedseconds AS LONG 'starting date/time st.wYear = 2009 st.wMonth = 7 st.wDay = 24 st.wHour = 17 st.wMinute = 20 st.wSecond = 30 'number of seconds to add elapsedseconds = 5000 'convert to filetime and then convert to quad value so we can do math SystemTimeToFileTime(st, ft) quadtime = MAK(QUAD, ft.dwLowDateTime, ft.dwHighDateTime) 'add the seconds (expressed as the number of 10ns intervals) quadtime = quadtime + elapsedseconds * 10000000 'multiple seconds by 10,000,000 to convert to amount of 10ns chunks 'convert back into filetime and then system time to get the resulting date/time ft.dwLowDateTime = LO(DWORD, quadtime) ft.dwHighDateTime = HI(DWORD, quadtime) FileTimeToSystemTime(ft, st) ? FORMAT$(st.wYear) & "/" & FORMAT$(st.wMonth) & "/" & FORMAT$(st.wDay) & " - " & FORMAT$(st.wHour) & ":" & FORMAT$(st.wMinute) & ":" & FORMAT$(st.wSecond) END FUNCTION
Comment
-
Code:#Include "Win32Api.Inc" Type QuadFILETIME nS As Quad End Type '================================================================================================== ' AddSeconds. On entry ST holds time to process, on exit ST holds modified time ' (SecsToAdd can also be negative to subtract a value...) '-------------------------------------------------------------------------------------------------- Sub AddSeconds Alias "AddSeconds"(ByRef ST As SYSTEMTIME, ByVal SecsToAdd As Long) Export Local QFT As QuadFileTime SystemTimeToFileTime ST, QFT QFT.nS = QFT.nS + (SecsToAdd * 10000000) FileTimeToSystemTime QFT, ST End Sub Function PBMain() Local lST As SYSTEMTIME lST.wDay = 4 lST.wMonth = 3 lST.wYear = 2009 lST.wHour = 01 lST.wMinute = 02 lST.wSecond = 03 MsgBox Format$(lST.wMonth, "00") & "/" & _ Format$(lST.wDay, "00") & "/" & _ Format$(lST.wYear, "0000") & " - " & _ Format$(lST.wHour, "00") & ":" & _ Format$(lST.wMinute, "00") & ":" & _ Format$(lST.wSecond, "00"),, "Before:" AddSeconds lSt, 12354 MsgBox Format$(lST.wMonth, "00") & "/" & _ Format$(lST.wDay, "00") & "/" & _ Format$(lST.wYear, "0000") & " - " & _ Format$(lST.wHour, "00") & ":" & _ Format$(lST.wMinute, "00") & ":" & _ Format$(lST.wSecond, "00"),, "After:" End Function
Regards,
Peter
"Simplicity is a prerequisite for reliability"
Comment
-
for example, I know 12354 seconds have elapsed from 3/4/2009 1:2:3. What is the time and date?Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
Comment