Convert a Time into and int of seconds

dayv2005

Member
Ok to convert ints into the time i found pretty easy. But now im wondering is there a way that i can take a time like 2:00 PM and convert it into the a int?
 

doreynie

Member
The time function gives you the seconds elapsed since midnight.

You should decompose your 'time'-string, into hours minutes seconds.

If you put those three into variables you can do something like this :

DEFINE VARIABLE cTime AS CHARACTER NO-UNDO.
DEFINE VARIABLE iTime AS INTEGER NO-UNDO.
ASSIGN
iTime = TIME
cTime = STRING(iTime,"hh:mm:ss":u)
.
MESSAGE "iTime : " iTime SKIP "cTime : " cTime
VIEW-AS ALERT-BOX INFO BUTTONS OK.
ASSIGN
iTime = 0.
MESSAGE "We've putted iTime to null, to prove our function is working" SKIP
"iTime : " iTime
VIEW-AS ALERT-BOX INFO BUTTONS OK.
FUNCTION fGetIntOfTimeString RETURNS INTEGER
(INPUT ip_cTimeString AS CHARACTER) FORWARD.
ASSIGN
iTime = fGetIntOfTimeString(INPUT cTime).
MESSAGE "iTime : " iTime SKIP "cTime : " cTime
VIEW-AS ALERT-BOX INFO BUTTONS OK.
FUNCTION fGetIntOfTimeString RETURNS INTEGER
(INPUT ip_cTimeString AS CHARACTER):
DEFINE VARIABLE iHours AS INTEGER NO-UNDO.
DEFINE VARIABLE iMinutes AS INTEGER NO-UNDO.
DEFINE VARIABLE iSeconds AS INTEGER NO-UNDO.
DEFINE VARIABLE iTime AS INTEGER NO-UNDO.
ASSIGN
iTime = 0
iHours = INT(SUBSTRING(ip_cTimeString,1,2))
iMinutes = INT(SUBSTRING(ip_cTimeString,4,2))
iSeconds = INT(SUBSTRING(ip_cTimeString,7,2))
iTime = iSeconds * 1
+ (iMinutes * 60)
+ (iHours * 3600)
.
RETURN iTime.
END FUNCTION.
 

schaapie

Member
In OpenEdge I think it can be done simpeler.
You have to convert PM into + 12 Hours, but this seems to work:
Code:
def var dt as datetime no-undo.
dt = DATETIME("01-01-0001 00:01:03").
disp mtime(dt) / 1000.
 

dayv2005

Member
Thanks for this info... I was just about to reply telling you thanks a lil while ago, but my company lost power and im at home now enjoying the day off. Thanks.
 

dayv2005

Member
Here's more or so what i was looking for the other day. I figured i'd post it in case someone needed something similar.



RETURNS INTEGER
(INPUT ip_cTimeString AS CHARACTER):

/* This function takes a string time in the HH:MM AM fomrat like '2:30 PM' and
converts it into the total seconds after midnight. Such as 52200 secs as an Int */

DEFINE VARIABLE iHours AS INTEGER NO-UNDO.
DEFINE VARIABLE iMins AS INTEGER NO-UNDO.
DEFINE VARIABLE iTime AS INTEGER NO-UNDO.
DEFINE VARIABLE strAM_PM AS CHARACTER NO-UNDO.
DEFINE VARIABLE strHour AS CHARACTER NO-UNDO.
DEFINE VARIABLE strMin AS CHARACTER NO-UNDO.

iTime = 0.

ASSIGN
strMin = TRIM(SUBSTRING(ip_cTimeString, index(ip_cTimeString,":") + 1, 2))
strAM_PM = SUBSTRING(ip_cTimeString, index(ip_cTimeString,"M") - 1, 2).


strHour = TRIM(SUBSTRING(ip_cTimeString, index(ip_cTimeString,":") - 2, 2)) NO-ERROR.
IF ERROR-STATUS:NUM-MESSAGES > 0 THEN
strHour = TRIM(SUBSTRING(ip_cTimeString, index(ip_cTimeString,":") - 1, 1)).

ASSIGN
iHours = INTEGER(strHour)
iMins = INTEGER(strMin).

IF strAM_PM = "PM" THEN
iHours = iHours + 12.

iTime = (iHours * 60 * 60) + (iMins * 60).

RETURN iTime.
END FUNCTION.
 
Top