Layer7 API Management

Expand all | Collapse all

context variable: string to date/time conversion and back

Jump to Best Answer
  • 1.  context variable: string to date/time conversion and back

    Posted 05-05-2017 12:58 PM

    Hello,

     

    I will appreciate if someone can help me understand how to convert string context variables into date/time format and vice versa. Let me describe the use case as follows.

     

    The request.http contains a query parameter "from-date" in the format of yyyy-mm-ddThh:mm:ss-hhmm (e.g. 2017-05-01T00:00:00-0400). I need to first convert it from string to date/time and then to get corresponding UTC date/time. From UTC date/time back to string and perform a query on a back end database which stores UTC date/time as string. In the output I need to covert the date/time from UTC back to local time zone as per the from-date request.  

     

    For string to date/time, I tried the following assertion. However, this assertion fails. 

     

     

    Alternative formats available from format: drop down like auto didn't work either. 

    Please share what assertion to use for string -> date and time.

    what assertion to use to convert date and time -> string.

     

    Thank you very much



  • 2.  Re: context variable: string to date/time conversion and back
    Best Answer

    Posted 05-05-2017 02:54 PM
      |   view attached

    Good afternoon,

     

    I've attached a policy that will take the value you provided as the parameter then pull out the offset to be used in the set context variable and the output variable is automatically in UTC format.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support

    Attachment(s)



  • 3.  Re: context variable: string to date/time conversion and back

    Posted 05-05-2017 04:02 PM

    The policy helps to get the offset out. But the date conversion returns in a wrong date. Why would the response be  offset by several days. 

     

    Original Date String: 2017-05-01T00:00:00-0400 Modified Date that is in UTC format: 2017-04-14T08:00:00.000Z


  • 4.  Re: context variable: string to date/time conversion and back

    Posted 05-05-2017 06:12 PM

    I've just reloaded the policy above with some modification to handle the time better.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 5.  Re: context variable: string to date/time conversion and back

    Posted 05-09-2017 11:55 AM

    Thank Stephen.

     

    There is subtle change needed. The most recent version incorrectly deducts the time if negative time offset is provided.

     

    Eastern DST 2017-05-09T11:50:00-0400

    Corresponding UTC 2017-05-09T03:50:00+0000

     

    However, following is the result generated by the policy.

     

    Original Date String: 2017-05-09T11:50:00-0400  Eastern Daylight Saving time Modified Date that is in UTC format: 2017-05-09T07:50:00.000Z




  • 6.  Re: context variable: string to date/time conversion and back

    Posted 05-09-2017 12:14 PM

    Okay one more time. Original updated with the change.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 7.  Re: context variable: string to date/time conversion and back

    Posted 05-09-2017 02:11 PM

    Not sure the "Set Context Variable" does correct computation. Because for the following input, the output should have the time as 18:10:00. Instead it computes the morning 06:10:00 UTC.

     

     

    Original Date String: 2017-05-09T11:10:00-0700 Modified Date that is in UTC format: 2017-05-09 06:10:00




  • 8.  Re: context variable: string to date/time conversion and back

    Posted 05-09-2017 02:23 PM

    I've retest the policy that I uploaded to the community post and it does provide the correct value if you use the string you set it should show 04:10 and if you switch it to +0700 then it shows at 18:10. Please try to download it again and reload the policy.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 9.  Re: context variable: string to date/time conversion and back

    Posted 05-15-2017 06:30 PM

    Good afternoon,

     

    Did the final policy work for you?

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 10.  Re: context variable: string to date/time conversion and back

    Posted 05-16-2017 08:00 AM

    Yes it did. Thank you very much.