OSB 12c Xquery function to convert the date of credit card expiry date to yyyy-MM-dd

I had a requirement to convert the credit card expiration date in the form of “201506” to “yyyy-MM-dd”.
The challenge here is adding the day value, we have to add the last day of the respective month.
For example, if we get 201502, the converted date should be “2015-02-28”

Here is the function to convert this date format.

Xquery:

xquery version "1.0" encoding "utf-8";

(:: OracleAnnotationVersion "1.0" ::)

declare namespace functx = "http://www.functx.com";

declare variable $inputDate as xs:string external;

declare function functx:days-in-month
  ( $date as xs:anyAtomicType? )  as xs:integer? {

   if (month-from-date(xs:date($date)) = 2 and
       functx:is-leap-year($date))
   then 29
   else
   (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    [month-from-date(xs:date($date))]
} ;
declare function functx:is-leap-year
  ( $date as xs:anyAtomicType? )  as xs:boolean {
      
    for $year in xs:integer(substring(string($date),1,4))
    return ($year mod 4 = 0 and
            $year mod 100 != 0) or
            $year mod 400 = 0
} ;

declare function local:func($inputDate as xs:string) as xs:string {
    concat((fn:substring(fn:data($inputDate),1,4)),'-',
(fn:substring(fn:data($inputDate),5,2)),'-',(functx:days-in-month(concat(fn:substring(fn:data($inputDate),1,4),'-',
fn:substring(fn:data($inputDate),5,2),'-',15))))
};

local:func($inputDate)


Hope it helps.

Comments

  1. The requirement to handle expiration dates in the form "yyyyMM" is clear, but it would be helpful to elaborate on scenarios where this conversion is typically used.
    pulse jet bag filter in delhi
    Rotary Air Locks in delhi

    ReplyDelete
  2. It's great that the function considers leap years. Have you tested edge cases, such as invalid dates like "202113"?
    Dust Collector Manufacturer India
    Axial Flow Fans India

    ReplyDelete
  3. The function names like functx:days-in-month and functx:is-leap-year are self-explanatory, but a brief comment explaining their purpose within the code would improve readability.
    Dust Collector Manufacturer
    Air Pollution Control System manufacturer

    ReplyDelete
  4. Adding a few more examples of input values (e.g., "201401", "201911") and their expected outputs would make it easier for readers to grasp the function's behavior.
    Air Ventilation System Manufacturer
    Dust collector manufacturer

    ReplyDelete
  5. The functx namespace is used for utility functions. Including a brief note on what http://www.functx.com provides would be insightful for readers unfamiliar with it.
    paint booth
    checkered sheet dealer in delhi

    ReplyDelete
  6. The use of $inputDate as an external variable is interesting. Could you show how this variable is passed in a real-world application?
    structural-steel-tubes in gwalior
    pulse jet bag filter manufacturer

    ReplyDelete

Post a Comment

Popular posts from this blog

OSB actions in proxy service

Difference between Route, Service Callout, Publish

XQuery functions that supported by OSB