Jump to content

Time Difference Between Date/Time


Recommended Posts

I'm working on writing a function that can take in the difference between two full date/time strings and convert it back to date time.

I imagine someone has written something like, if not I'll create my own.

$start = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC
sleep(1000)
$end = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & @MSEC
$time = $end - $start

I need to take the $time variable and output a clean difference output such as 00:00:01

Example times:

Start: 20100416140654796

End: 20100416232951176

"Ticks": 92296380

Thanks

Link to comment
Share on other sites

Theres a _DateDiff in the user defined functions > Date management, along with other date manipualtion functions.

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

Here. I don't know whether _DateDiff is summertime-aware.

#include<Date.au3>
$now = _NowCalc()
$future = "2011/01/01 00:00:00"
$secondDiff = _DateDiff("s", $now, $future)

Dim $iHour, $iMin, $iSec
_TicksToTime($secondDiff*1000, $iHour, $iMin, $iSec)
MsgBox(0, 'Time to New Year', StringFormat("%i hours, %i minutes, %i seconds", $iHour, $iMin, $iSec))

$aTime = _SecondsToTime($secondDiff)
MsgBox(0, 'Time to New Year', StringFormat("%i weeks, %i days, %i hours, %i minutes, %i seconds", $aTime[0], $aTime[1], $aTime[2], $aTime[3], $aTime[4]))

Func _SecondsToTime($iSeconds)
    ; Author: ProgAndy
    Local $aReturn[5] = [0, 0, 0, 0, 0]
    $aReturn[4] = Mod($iSeconds, 60)
    
    Local $iMinutes = Floor($iSeconds/60) 
    $aReturn[3] = Mod($iMinutes, 60)
    
    Local $iHours = Floor($iMinutes/60) 
    $aReturn[2] = Mod($iHours, 24)
    
    Local $iDays = Floor($iHours/24) 
    $aReturn[1] = Mod($iDays, 7)
    
    Local $iWeeks = Floor($iDays/7)
    $aReturn[0] = $iWeeks
    Return $aReturn
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

kjcdude,

There is a recent thread where you can grab sideways information about your question and related issues with time and time duration.

The main issue with UTC-based time is that it isn't a linear timescale. Hence difference between two points in UTC are essentially meaningless. That is of course if you are talking, like in your post, of a clean difference: there is no such thing with UTC (and even less with most "local time"s on Earth).

OTOH, if the points in UTC time are not too far away from each other and are either UTC or local time from close locations (whatever close means) and you don't mind your result being off a few seconds (leap seconds) or a few hours (DST), you can still approximate "something" that you could consider as being a duration with:

$timestamp1 = _NowCalc()
;   sometime later you run:
$timestamp2 = _NowCalc()
;   then you can compute "something", split in seconds, minutes, hours, days, months, years as:
$duration = _DataAdd('s', _DateDiff('s', $timestamp1, $timestamp2), '1000/01/01 00:00:00')
;   then substract 1000 to year, 1 to month and 1 to day

but understand that $duration is then essentially meaningless: it isn't a measure, it's ... what you decide it is!

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

From the suggestions above here's what I cam up with.

Here's what I cam up with.

I'm going to use _NowCalc() instead of the date variables for the start and end time.

To calculate the time I'm going to use _DateDiff(s, $, $) then take that return and use _TicksToTime($ * 1000, $, $, $) to get a clean hour, min, sec return.

Thanks for the all the help.

Link to comment
Share on other sites

Oops, I missed using that sorry. Your way is better.

Remember: if your PC synchronizes to an NTP clock during the time interval, your result may be 1 second shorter than what a free-running chronometer would show. If you live in an area where DST apply (or applied or will apply) , your result may be 1 to 2 hours shorter or longer, depending on where you live and when it was.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here's another method that came to my mind using the Epoch functions from trancexx.

#include <Date.au3>

Global $Hour, $Mins, $Secs

$start = _Epoch_encrypt(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)
Sleep(3000)
$end = _Epoch_encrypt(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)

_TicksToTime(Int($end * 1000 - $start *1000), $Hour, $Mins, $Secs)
$Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)

MsgBox(0, "Time between Start and End", $Time)


Func _EPOCH_decrypt($iEpochTime)
    ; http://www.autoitscript.com/forum/index.php?showtopic=83667&hl=epoch
    ; by trancexx

    Local $iDayToAdd = Int($iEpochTime / 86400)
    Local $iTimeVal = Mod($iEpochTime, 86400)

    If $iTimeVal < 0 Then
        $iDayToAdd -= 1
        $iTimeVal += 86400
    EndIf

    Local $i_wFactor = Int((573371.75 + $iDayToAdd) / 36524.25)
    Local $i_xFactor = Int($i_wFactor / 4)
    Local $i_bFactor = 2442113 + $iDayToAdd + $i_wFactor - $i_xFactor

    Local $i_cFactor = Int(($i_bFactor - 122.1) / 365.25)
    Local $i_dFactor = Int(365.25 * $i_cFactor)
    Local $i_eFactor = Int(($i_bFactor - $i_dFactor) / 30.6001)

    Local $aDatePart[3]
    $aDatePart[2] = $i_bFactor - $i_dFactor - Int(30.6001 * $i_eFactor)
    $aDatePart[1] = $i_eFactor - 1 - 12 * ($i_eFactor - 2 > 11)
    $aDatePart[0] = $i_cFactor - 4716 + ($aDatePart[1] < 3)

    Local $aTimePart[3]
    $aTimePart[0] = Int($iTimeVal / 3600)
    $iTimeVal = Mod($iTimeVal, 3600)
    $aTimePart[1] = Int($iTimeVal / 60)
    $aTimePart[2] = Mod($iTimeVal, 60)

    Return SetError(0, 0, StringFormat("%.2d/%.2d/%.2d %.2d:%.2d:%.2d", $aDatePart[0], $aDatePart[1], $aDatePart[2], $aTimePart[0], $aTimePart[1], $aTimePart[2]))

EndFunc   ;==>_Epoch_decrypt

Func _Epoch_encrypt($date)

    Local $main_split = StringSplit($date, " ")
    If $main_split[0] - 2 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf

    Local $asDatePart = StringSplit($main_split[1], "/")
    Local $asTimePart = StringSplit($main_split[2], ":")

    If $asDatePart[0] - 3 Or $asTimePart[0] - 3 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf

    If $asDatePart[2] < 3 Then
        $asDatePart[2] += 12
        $asDatePart[1] -= 1
    EndIf

    Local $i_aFactor = Int($asDatePart[1] / 100)
    Local $i_bFactor = Int($i_aFactor / 4)
    Local $i_cFactor = 2 - $i_aFactor + $i_bFactor
    Local $i_eFactor = Int(1461 * ($asDatePart[1] + 4716) / 4)
    Local $i_fFactor = Int(153 * ($asDatePart[2] + 1) / 5)
    Local $aDaysDiff = $i_cFactor + $asDatePart[3] + $i_eFactor + $i_fFactor - 2442112

    Local $iTimeDiff = $asTimePart[1] * 3600 + $asTimePart[2] * 60 + $asTimePart[3]

    Return SetError(0, 0, $aDaysDiff * 86400 + $iTimeDiff)

EndFunc   ;==>_Epoch_encrypt
Link to comment
Share on other sites

Hi Kafu, I knew there was such a posting by trancexx but couldn't find it.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...