Tips

ISO DATE文字列形式で表現された時間の計算

日付2010/12/28
ID76219 (英語原文参照)
バージョンv12.1
プラットフォームMac/Win

次のメソッドは, 文字列で渡された ISO DATEの差を計算するものです。

// Description:
// Calculates the difference between two ISO DATE timestamps values
// (2010-11-22T15:55:22) and returns human readable string value of 
// elapse time in "Days, Hours, Minutes, Seconds"
//
// Parameters:
// $1 = string; start time = ISO DATE timestamp (now if empty string is passed)
// $2 = string; end time = ISO DATE timestamp (now if empty string is passed)
//
// Return Value:
// $0 = string; time difference in "Days, Hours, Minutes, Seconds"
//

C_TEXT($0)

If (Count parameters=2)

C_TEXT($1;$sts) // start timestamp
C_TEXT($2;$ets) // end time stamp


If ((Position("z";$1)=0) & (Position("z";$2)=0))
// "z" is not found in $1 or $2

If($1="")
// start time is empty - so use now
$sts:=String(Current date;ISO Date;Current time)
Else 
// start time is not empty - so confirm the format
// for now just assign it
$sts:=String(Date($1);ISO Date;Time($1))
End if 

If ($2="")
// end time is empty - so use now
$ets:=String(Current date;ISO Date;Current time)
Else 
// start time is not empty - so confirm the format
// for now just assign it
$ets:=String(Date($2);ISO Date;Time($2))
End if 

C_LONGINT($days_l)
C_TIME($hms_t)

If ($ets>$sts)
$days_l:=Date($ets)-Date($sts)
$hms_t:=Time($ets)-Time($sts)
Else 
$days_l:=Date($sts)-Date($ets)
$hms_t:=Time($sts)-Time($ets)
End if 

If ($hms_t<?00:00:00?)
// time is negative so correct the days
$days_l:=$days_l-1
// subtract 1 day from days
$hms_t:=$hms_t+?24:00:00?
// add 24 hours to time
End if 

If ($days_l=0)
// zero days
$0:=String($hms_t;Hour Min Sec)
Else 
// not zero days
$0:=String($days_l;"###,##0 days ")+String($hms_t;Hour Min Sec)
End if 

Else 
// "z" is found in $1 or $2
// this method does not accept ISO DATE GMT - only ISO DATE
$0:="This method requires ISO DATE format parameters only"
End if 

Else 
// incorrect parameters
// this method requires 2 parameters (check the header)
$0:="This method requires 2 parameters"
End if