Skip to main content
May 4, 2023 1:08 AM UTC
Solved

Time Parsing Rule

I am trying to write a parser business rule to pull out the middle portion of a date string from a data source.

The file has the date as "1-Jan-23" and I want to pull out the Jan.  On the data source, I have the time dimension assigned to the proper column in the file.  The month is always 3 characters, and the year is always 2 characters. 

I am getting an error that I am unable to execute the BR. Can I not use args.value for the starting point of my parser rule ?

 

Dim filetime As String = args.Value

Dim filetimelen As Integer = filetime.Length
Dim startpos As Integer = filetimelen - 6
Dim month As String = filetime.Substring(startpos, 3)

Return month

RobbSalzmann
Employee - Advisor
May 4, 2023 12:27 PM UTC

Here are a couple ways to do this depending on the requirement:

 

 

'returns period in Mn notation (e.g. M1, M2...)     
Dim month As Integer = DateTime.ParseExact(args.Value, "d-MMM-yy", CultureInfo.InvariantCulture).Month
return $"M{month}"

'returns the Three letter month abbreviation (e.g. Jan, Feb...)     
Dim strMonth As String = DateTime.ParseExact(args.Value, "d-MMM-yy", CultureInfo.InvariantCulture).ToString("MMM")
return strMonth

 

 

4 replies

May 4, 2023 9:50 AM UTC

bennovak  The complex expression looks good to me. It should return Jan as per your example. Can you put a screenshot of the error ? Also, let us know when exactly are you running into the error.

RobbSalzmann
Employee - Advisor
Employee - Advisor
May 4, 2023 12:27 PM UTC

Here are a couple ways to do this depending on the requirement:

 

 

'returns period in Mn notation (e.g. M1, M2...)     
Dim month As Integer = DateTime.ParseExact(args.Value, "d-MMM-yy", CultureInfo.InvariantCulture).Month
return $"M{month}"

'returns the Three letter month abbreviation (e.g. Jan, Feb...)     
Dim strMonth As String = DateTime.ParseExact(args.Value, "d-MMM-yy", CultureInfo.InvariantCulture).ToString("MMM")
return strMonth

 

 

bennovakAuthor
May 4, 2023 3:17 PM UTC

Thank you, RobbSalzmann.  This worked perfectly!

Employee - Sage
May 4, 2023 1:03 PM UTC

Parsing dates is better done the way RobbSalzmann showed, but for the generic case when you want to split a delimited string, don't rely on brittle indexes - you can use StringHelper.SplitString instead:

' split a string separated by '-'
Dim fields As List(Of String) = StringHelper.SplitString( _
    "1-Jan-23", "-", _
    StageConstants.ParserDefaults.DefaultQuoteCharacter)

BRApi.ErrorLog.LogMessage(si, fields(0)) ' will output 1
BRApi.ErrorLog.LogMessage(si, fields(1)) ' will output Jan
BRApi.ErrorLog.LogMessage(si, fields(2)) ' will output 23