VBCommons/parsers/ParserDate.cls
2024-06-07 20:46:40 +03:00

152 lines
4.0 KiB
OpenEdge ABL

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "ParserDate"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ========= Date parser ==========
' Shared module version: 20220614
' Tested in: TestCommons
' Depends on: z_ParserRegex
' Required reference: VBScript_RegExp_55
Option Explicit
Private rxDate_ As RegExp
Public day_ As Integer
Public month_ As Integer
Public year_ As Integer
Private Sub Class_Initialize()
Set rxDate_ = New RegExp
rxDate_.Pattern = P_DATE_CAPTURE
rxDate_.Global = False
End Sub
Public Function Init(aDay%, aMonth%, aYear%)
day_ = aDay
month_ = aMonth
year_ = aYear
End Function
Public Function Test(target$) As Boolean
Test = rxDate_.Test(target)
End Function
Public Function Parse(target$) As Boolean
Parse = Test(target)
If Parse Then
Dim matches As Object
Set matches = rxDate_.Execute(target)
If matches.Item(0).SubMatches(0) <> vbNullString Then
day_ = CInt(matches.Item(0).SubMatches(0))
month_ = ConvertDateText2Int(matches.Item(0).SubMatches(1))
year_ = CInt(matches.Item(0).SubMatches(2))
Else
day_ = CInt(matches.Item(0).SubMatches(3))
month_ = CInt(matches.Item(0).SubMatches(4))
year_ = CInt(matches.Item(0).SubMatches(5))
End If
End If
End Function
Public Function GetData() As Collection
Set GetData = New Collection
Call GetData.Add(year_)
Call GetData.Add(month_)
Call GetData.Add(day_)
End Function
Public Function GetDataDescription() As Scripting.Dictionary
Dim iData As New Scripting.Dictionary
Call iData.Add("ãîä", year_)
Call iData.Add("ìåñÿö", month_)
Call iData.Add("äåíü", day_)
Set GetDataDescription = iData
End Function
Public Function Transform(sText$, sParam$) As String
If Not Parse(sText) Then
Transform = sText
ElseIf sParam = "1" Then
Transform = AsTextString
ElseIf sParam = "2" Then
Transform = AsDigitsString
Else
Transform = sText
End If
End Function
Public Property Get IsValidDate() As Boolean
IsValidDate = False
If month_ > 12 Or month_ < 1 Then _
Exit Property
If day_ > 31 Or day_ < 1 Then _
Exit Property
IsValidDate = IsDate(AsDigitsString)
End Property
Public Property Get DDate() As Double
If Not IsValidDate Then _
Exit Function
DDate = DateSerial(year_, month_, day_)
End Property
Public Function AsDigitsString() As String
AsDigitsString = Format(day_, "00") & "." & Format(month_, "00") & "." & Format(year_, IIf(year_ > 100, "0000", "00"))
End Function
Public Function AsTextString() As String
If Not IsValidDate Then
AsTextString = "INVALID_DATE"
Else
AsTextString = Format(day_, "00") & " " & MonthStr(month_) & " " & Year(DateSerial(year_, 1, 1)) & " ãîäà"
End If
End Function
' ============
Private Function ConvertDateText2Int(sMonth$) As Integer
If IsNumeric(sMonth) Then
ConvertDateText2Int = CInt(sMonth)
Exit Function
End If
Select Case sMonth
Case "ÿíâàðÿ": ConvertDateText2Int = 1
Case "ôåâðàëÿ": ConvertDateText2Int = 2
Case "ìàðòà": ConvertDateText2Int = 3
Case "àïðåëÿ": ConvertDateText2Int = 4
Case "ìàÿ": ConvertDateText2Int = 5
Case "èþíÿ": ConvertDateText2Int = 6
Case "èþëÿ": ConvertDateText2Int = 7
Case "àâãóñòà": ConvertDateText2Int = 8
Case "ñåíòÿáðÿ": ConvertDateText2Int = 9
Case "îêòÿáðÿ": ConvertDateText2Int = 10
Case "íîÿáðÿ": ConvertDateText2Int = 11
Case "äåêàáðÿ": ConvertDateText2Int = 12
Case Else: ConvertDateText2Int = 0
End Select
End Function
Private Function MonthStr(nMonth%) As String
Select Case nMonth
Case 1: MonthStr = "ÿíâàðÿ"
Case 2: MonthStr = "ôåâðàëÿ"
Case 3: MonthStr = "ìàðòà"
Case 4: MonthStr = "àïðåëÿ"
Case 5: MonthStr = "ìàÿ"
Case 6: MonthStr = "èþíÿ"
Case 7: MonthStr = "èþëÿ"
Case 8: MonthStr = "àâãóñòà"
Case 9: MonthStr = "ñåíòÿáðÿ"
Case 10: MonthStr = "îêòÿáðÿ"
Case 11: MonthStr = "íîÿáðÿ"
Case 12: MonthStr = "äåêàáðÿ"
Case Else: MonthStr = "INVALID MONTH"
End Select
End Function