Handling date and time in POST requests in Odata
Just thought I would share my recent thoughts and findings on a well debated area of Odata Development in ABAP.
There are numerous blogs about the edm.dateTime object, but not really many solutions on how to ACTUALLY handle it. So I thought I would share my easy way with you all.
First rule of odata dateTime club, do NOT just use string for convenience. I know it can be a pain, but it leads to bad design. Secondly don’t just use a date AND a time property, you can actually quite easily handle this properly.
Instead assign your edm.dateTime or edm.dateTimeOffset to your property. If you haven’t assigned a structure to your entity, the gateway builder will automatically propose the timestamp type.
I’ve had so much trouble with this and can’t really get it through all the automatic mapping in the gateway.
However if you change your type to timestampl like below.
This will allow your odata calls to pass through all the automatic mapping without worry.
Now in your update, create etc method you can get the values you need from this with a simple code:
data: lv_local_date type sy-datum, lv_local_time type sy-uzeit. CONVERT TIME STAMP <property-with-timestampl> TIME ZONE sy-zonlo INTO DATE lv_local_date TIME lv_local_time.
With the above you will get the date and time in the servers timezone, since a odata call is always transferred to UTC.
Thats pretty much it. Easy peasy.