Tech Tip: Extracting hours, minutes, and seconds from a Time value
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: January 8, 2010
When the need arises to extract one of the three parts of a time value (hours, minutes, or seconds), native commands do not exist in 4D for the task. Below are three 4D methods; each will retrieve the different parts to a Time value.
If (True) If (False) `***************************************************************************** `// `// TIME_HourOf `// `// Purpose: Return the hour of a time value `// `// $0 - LONGINT - Hour value `// $1 - TIME - Time value `// `***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name `===================== Declare Variables ================================== C_LONGINT($0;$Hr) C_TIME($Time;$1) End if `====================== Initialize and Setup ================================ $Time:=$1 `======================== Method Actions ================================== $TiStr:=String($Time;HH MM SS ) $Hr:=Num(Substring($TiStr;1;2)) `======================== Clean up and Exit ================================= $0:=$Hr |
If (True) If (False) `***************************************************************************** `// `// TIME_MinuteOf `// `// Purpose: Return the Minute of a time value `// `// $0 - LONGINT - Minute value `// $1 - TIME - Time value `// `***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name `===================== Declare Variables ================================== `method_parameters_declarations C_LONGINT($0;$Min) C_TIME($Time;$1) C_STRING(15;$TiStr) End if `====================== Initialize and Setup ================================ $Time:=$1 `======================== Method Actions ================================== $TiStr:=String($Time;HH MM SS ) $Min:=Num(Substring($TiStr;4;2)) `======================== Clean up and Exit ================================= $0:=$Min |
If (True) If (False) `***************************************************************************** `// `// TIME_SecondOf `// `// Purpose: Return the second of a time value `// `// $0 - LONGINT - Second value `// $1 - TIME - Time value `// `***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name `===================== Declare Variables ================================== `method_parameters_declarations C_LONGINT($0;$Sec) C_TIME($Time;$1) C_STRING(15;$TiStr) End if `====================== Initialize and Setup ================================ $Time:=$1 `======================== Method Actions ================================== $TiStr:=String($Time;HH MM SS ) $Sec:=Num(Substring($TiStr;7;2)) `======================== Clean up and Exit ================================= $0:=$Sec |
Here are some modifications to these 3 methods so that they handle all 4D time values and also all 4D language versions.
TIME_HourOf Method Modifications:
Before line 27, add this line: GET SYSTEM FORMAT(Time separator ;$timeSeparator_t)
Change line 28 to this line: $Hr:=Num(Substring($TiStr;1;(Position($timeSeparator_t;$TiStr)-1)))
TIME_MinuteOf Method Modifications
Before line 29, add this line: GET SYSTEM FORMAT(Time separator ;$timeSeparator_t)
Change line 30 to this line: $Min:=Num(Substring($TiStr;(Position($timeSeparator_t;$TiStr)+1);2))
TIME_SecondOf Method Modifications
Change line 28 to this line: $Sec:=Num(Substring($TiStr;(Length($TiStr)-1);2))
With these modifications the three methods will now return proper results for 4D time values greater than 99:59:59.