I have a variable type time but sometimes this variable doesn't have anything. I want that when it is initial don't show "000000", I want '' without nothing (zeros). Let's explain my problem with the code:
if lwa_hora is initial.
clear lwa_hora.
ls_entity-hora = lwa_hora. //result 000000 but I want any zero
else.
ls_entity-hora = lwa_hora. // result 000000
endif.
I tried with clear but nothing happens.
I need this is because in the frontend I need that the variable in the XML comes null to checkout this depends of the value:
sap.ui.define([], function () {
"use strict";
return {
getHora: function (hora) {
if (hora) {
console.log(hora);
hora = hora.replace(/^PT/, '').replace(/S$/, '');
hora = hora.replace('H', ':').replace('M', ':');
var multipler = 60 * 60;
var result = 0;
hora.split(':').forEach(function (token) {
result = token * multipler;
multipler = multipler / 60;
});
var timeinmiliseconds = result * 1000;
var timeFormat = sap.ui.core.format.DateFormat.getTimeInstance({
pattern: "KK:mm:ss a"
});
var TZOffsetMs = new Date(0).getTimezoneOffset() * 60 * 1000;
return timeFormat.format(new Date(timeinmiliseconds TZOffsetMs));
}
else {
return 'Don't have';
}
}
};
});
but it always enters in the if
block because the ABAP program returns the value 0, it's like it comes with a value. Is it possible to make something in the backend to clear the variable? thank you
CodePudding user response:
The time data-type in abap (t
) is a value-type. It's internally implemented as an integer counting the seconds since midnight. 0 seconds since midnight is a valid value, so it can't have a null-value.
However, ABAP allows you to create a reference to any value-type:
hora TYPE REF TO t.
That means hora
will be a reference to a variable of TYPE t
. Initially, this reference will be unbound, which is conceptually very similar to a null-reference in other programming languages. You can check that with:
IF ls_entity-hora IS BOUND.
...
IF ls_entity-hora IS NOT BOUND.
You can assign a time value with GET REFERENCE OF lwa_hora INTO ls_entity-hora
. But now you have a reference to an existing variable. Change the value of lwa_hora
, and the value of ls_entity-hora
also changes. That might not always be what you want. So it might be better to create a new piece of data in memory for our reference to point to:
CREATE DATA ls_entity-hora.
Now ls_entity-hora
is no longer unbound (or "null" if you want to call it that), it points to a new time-value of 000000
. If you want to read or change the value of this nameless piece of data this reference points to, then you can do this with the dereferencing-operator ->*
:
ls_entity-hora->* = lwa_hora.
If you intentionally want to set a reference to unbound (or "set the reference to null"), you can do that by clearing the reference:
CLEAR ls_entity-hora.
By the way: Representing a point in time by two separate variables of the types d
and t
fell out of fashion in the past decade. The current best practice for this situation is to use a single variable of type TIMESTAMP
(if you need second precision) or TIMESTAMPL
(if you need microsecond precision). A timestamp of 00000000000000
is obviously an illegal value, so it can be used to represent the absence of a point in time. This type also usually makes it much easier to communicate with a SAPUI5 frontend (like in your case), because many of the technologies for making oData services offer automatic conversion between Javascript Date
and ABAP TIMESTAMP
.