Page 1 of 1 1
Topic Options
#197298 - 2010-01-06 02:54 PM TimeDiff() not working as it should, paging Glenn
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Guys,

I'm using Glenn's TimeDiff() function to calculate the difference between a timestamp and today’s timestamp like shown below.
The function comments say:
 Quote:

When a format value is specified, it returns the fractional part (ie 0.5 days for 12 hours).


so if I do something like below I would expect it to return a fractional value because it is more than 1 year ago but less than two years ago. All I get is the full years and no decimal characters. Me and my colleague have been staring at this for some time and cannot see anything wrong. Am I doing something wrong or is the UDF not ok?

 Code:
Break on

$date = "2008/05/02 10:20:12"

TimeDiff($date, "now", "y")

Sleep 5


;FUNCTION       TimeDiff() 
; 
;AUTHOR         Glenn Barnas 
; 
;VERSION        2.2 / 2007/10/14 
;		 Modified to increase accuracy, permit fracional second calculations 
;		 2.1 / 2007/03/17 
;               added "now" and "today" options for both start and end times 
;               2.0 / 2006/11/20 
;               Changes for code efficiency; added defaults for midnight  
; 
;ACTION         Calculates the time difference between two given date/time strings 
; 
;SYNTAX         TimeDiff(Start [, End] [, Format] [, MSec]) 
; 
;PARAMETERS     Start  - REQUIRED, String value representing the start timestamp 
;                 Format yyyy/mm/dd hh:mm:ss 
; 
;               End    - OPTIONAL, Defaults to "now" 
;		   String value representing the ending time 
;                 Format yyyy/mm/dd hh:mm:ss 
;		   Can be the special value "now" for the current date/time, or "today" 
;                 for midnight of the current day. 
; 
;                 When the time value is not specified, it defaults to 00:00:00.000 (midnight) 
; 
;		 Format - OPTIONAL, one of: 
;		  "m" - return minutes 
;		  "h" - return hours 
;		  "d" - return days 
;		  "y" - return years 
;		 When a format value is specified, it returns the fractional part (ie 0.5 days for 12 hours). 
; 
;		 MSec	- OPTIONAL, True if the fractional seconds should be returned. Default 
;		  is false, returning whole seconds, to maintain compatibility with earlier versions. 
;		  MSec only affects the return of fractional seconds, not fractional parts of other time formats. 
; 
;REMARKS        Returns a value representing the difference in time between two date/time 
;		 strings. Assumes that "Start" is in the past, but will properly return a 
;		 negative value if it is in the future. 
; 
;RETURNS        Double - difference between Start and End timestamps in seconds 
; 
;DEPENDENCIES   None 
; 
;TESTED WITH    Kix 4.2+, NT4, W2K, WXP, W2K3 
; 
;EXAMPLES       If TimeDiff(GetFileTime('SomeFile.txt'),  'now', 'h') > 48 
;		   "File is more than 2 days old!" ? 
;		 EndIf 
; 
Function TimeDiff($_Start, OPTIONAL $_End, OPTIONAL $_Fmt, OPTIONAL $_MSec)
	 
	Dim $_, $_SDate, $a_Start, $_EDate, $a_End, $_Duration
	 
	; Check for special START parameters 
	Select
		Case $_Start = 'now'
			$_Start = @DATE + ' ' + @TIME + '.' + @MSECS
		Case $_START = 'today'
			$_Start = @DATE + ' 00:00:00.000'
	EndSelect
	 
	; Check for special END parameters 
	Select
		Case $_End = 'now' Or $_End = '' 
			$_End = @DATE + ' ' + @TIME + '.' + @MSECS
		Case $_End = 'today'
			$_End = @DATE + ' 00:00:00.000'
	EndSelect
	 
	; Validate parameters 
	; Parameters passed are "yyyy/mm/dd hh:mm:ss[.sss]" - make sure the default time is added 
	$a_Start = Split(Join(Split(Join(Split($_Start + ' 00:00:00.000', '/'), ' '), ':'), ' '), ' ', 6)
	If UBound($a_Start) <> 5 Exit 87 EndIf 	; bad start time parameter 
	For $_ = 0 to 5
		$a_Start[$_] = CDbl($a_Start[$_]) 	; convert to numeric values 
	Next
	 
	$a_End = Split(Join(Split(Join(Split($_End + ' 00:00:00.000', '/'), ' '), ':'), ' '), ' ', 6)
	If UBound($a_End) <> 5 Exit 87 EndIf 	; bad start time parameter 
	For $_ = 0 to 5
		$a_End[$_] = CDbl($a_End[$_]) 	; convert to numeric values 
	Next
	 
	; Convert dates to Days, then convert to seconds and add the time value 
	If $a_Start[1] < 3
		$a_Start[1] = $a_Start[1] +12
		$a_Start[0] = $a_Start[0] -1
	EndIf
	$_SDate = $a_Start[2] + (153 * $a_Start[1] -457) /5 + 365 * $a_Start[0] + $a_Start[0] /4 - $a_Start[0] /100 + $a_Start[0] /400 - 306
	$_SDate = CDbl($_SDate) * 86400.0
	$_SDate = $_SDate + $a_Start[3] * 3600 + $a_Start[4] * 60 + $a_Start[5]
	 
	If $a_End[1] < 3
		$a_End[1] = $a_End[1] +12
		$a_End[0] = $a_End[0] -1
	EndIf
	$_EDate = $a_End[2] + (153 * $a_End[1] -457) /5 + 365 * $a_End[0] + $a_End[0] /4 - $a_End[0] /100 + $a_End[0] /400 - 306
	$_EDate = CDbl($_EDate) * 86400.0
	$_EDate = $_EDate + $a_End[3] * 3600 + $a_End[4] * 60 + $a_End[5]
	 
	; Get the duration between the timestamps 
	$_Duration = CDbl($_EDate - $_SDate)
	 
	; Trim fractional seconds if the MSec flag wasn't set 
	; Value returned is whole seconds 
	If Not $_MSec
		$_Duration = CInt($_Duration)
	EndIf
	 
	; Return data as a Double - seconds (default), hours, minutes, days, or years 
	Select
		Case $_Fmt = 'm' ; minutes 
			$TimeDiff = $_Duration /60.0
		Case $_Fmt = 'h' ; hours 
			$TimeDiff = $_Duration /3600.0
		Case $_Fmt = 'd' ; days 
			$TimeDiff = $_Duration /86400.0
		Case $_Fmt = 'y' ; years 
			$TimeDiff = $_Duration /31536000.0
		Case 1
			$TimeDiff = $_Duration
	EndSelect
	 
	Exit 0
	 
EndFunction


Edited by Mart (2010-01-07 08:53 AM)
Edit Reason: D#mn those typos.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#197299 - 2010-01-06 05:15 PM Re: TimeDiff() not working as it should, paging Glenn [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I'll take a look shortly..
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197300 - 2010-01-06 05:35 PM Re: TimeDiff() not working as it should, paging Glenn [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Quick-fix - enable the MSEC flag
 Code:
TimeDiff($date, "now", "y", 1)
Alternatively, download version 2.2a from my web site. This bug was found and corrected within a few days of original publication, but I guess I never updated the copy on KORG.

FYI - the latest versions are always available on the UDF library in the Resources section of my web site, as my entire UDF library is postprepped daily for publication. New (otherwise unpublished) UDFs are also available.

I'll update the KORG posting this evening.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197301 - 2010-01-07 08:43 AM Re: TimeDiff() not working as it should, paging Glenn [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Enabling the MSecs works but I do not like to enable things that I do not need to get other things to work that should work without the unneeded stuff. I downloaded v2.2a from your website and all is working fine now. Thanks

An update of the TimeDiff UDF on KORG might be nice ;\)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#197304 - 2010-01-07 12:20 PM Re: TimeDiff() not working as it should, paging Glenn [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
 Originally Posted By: Mart
An update of the TimeDiff UDF on KORG might be nice ;\)
Well I guess I was nice, then. ;\)

KORG is updated. Sadly, the posting seems to have a million lines of whitespace after it - I can't understand why. The UDF code is the same size, just had a block of code relocated. Maybe one of the admins could look at the UBB DB and fix it. It doesn't affect the UDF, though.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197309 - 2010-01-07 02:23 PM Re: TimeDiff() not working as it should, paging Glenn [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
When you say "million" do you mean 4 or 5, 'cause that's all I could see in my browser.

There was a small area of whitespace both at the start and end of the prepped script caused by newlines in the <PRE> </PRE> section.

I've chopped them out, let me know if it looks any better to you.

Top
#197315 - 2010-01-07 03:24 PM Re: TimeDiff() not working as it should, paging Glenn [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Strange.. after I posted the edited update, there were what seemed like 150 blank lines (ok - a fraction of a million, but it felt MUCH larger) after the EndFunction statement. Looking at the actual HTML, there was only the 1 leading and trailing blank line.

It displayed properly on my web site, so I just copied & pasted the HTM source file from my site. (my source file is PPCL raw output - the ASP script to display it wraps it in a proper HTML page, so there should have been no extraneous HTML.)

Thanks - it looks fine now.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 675 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.133 seconds in which 0.102 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org