1

I have to validate different types of dates in ballerina. I have been trying deal with it using regex, however there are many ways to write a date (eg. 25/05/2020, 05/25/2020, 25-May-2020 etc.).

It is hard to predict all the types. What's more, it would be nice to validate whether the received input is really a date - that includes different number of days in different months or leap years. Generally such regex would be monstrous (anyway really big). Are you aware of any existing library that would provide a shortcut or have date validation?

Is there any way to facilitate such task?

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
GregSim
  • 31
  • 3

1 Answers1

1

Validating all the date formats at a single go is a challenge, not only in Ballerina, but in the other languages too. Ballerina do not have a single method to validate all the date formats, but you can use ballerina/time library to parse date strings to Time records.

import ballerina/time;

public function main() {
    string timeString = "<your time string>";
    string timeFormat = "<your time format>";
    time:Time|time:Error time = time:parse(timeString, timeFormat);
}

In the sample, the time:parse() will return a valid Time record, if the provided is a valid time, according to the provided time format.

I know this is not the exact answer you want, but this is the way to parse a time in Ballerina.

Alternatively, there are some Java answers which can be used (I am not sure whether they fulfil your requirement though) with Ballerina - Java interoperability.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • Thank you! That would be some improvement. – GregSim May 25 '20 at 14:55
  • 1
    Hi, it seems there is a ballierinian library to validate regex. It is in central balerina and its name is samjs/jregex – GregSim Jun 04 '20 at 14:07
  • Nice, but to validate Regex, you can use [`ballarina/stringutils`](https://ballerina.io/learn/api-docs/ballerina/stringutils/index.html) module too. But this will too fix only half of the problem, right? – ThisaruG Jun 04 '20 at 14:30