Posted 15 October 2018, 9:58 pm EST
Deepak,
here is the code. There is also a ‘Easter’ function.
Public Class WeekOfYearFunctionInfo
Inherits FarPoint.CalcEngine.FunctionInfo
Sub New()
End Sub
Public Overrides ReadOnly Property Name() As String
Get
Return "WeekOfYear"
End Get
End Property
Public Overrides ReadOnly Property MinArgs() As Integer
Get
Return 1
End Get
End Property
Public Overrides ReadOnly Property MaxArgs() As Integer
Get
Return 1
End Get
End Property
Public Overrides Function Evaluate(ByVal args() As Object) As Object
Dim num As Integer = FarPoint.CalcEngine.CalcConvert.ToInt(args(0))
Return GetIso8601WeekOfYear(System.DateTime.FromOADate(num))
End Function
End Class
Public Function GetIso8601WeekOfYear(ByVal xtime As DateTime) As Integer
Dim day As DayOfWeek = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(xtime)
If (day >= System.DayOfWeek.Monday And day <= System.DayOfWeek.Wednesday) Then
xtime = xtime.AddDays(3)
End If
'// Return the week of our adjusted day
Return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(xtime, CalendarWeekRule.FirstFourDayWeek, System.DayOfWeek.Monday)
End Function
Public Class EasterFunction
Inherits FarPoint.CalcEngine.FunctionInfo
Public Overrides ReadOnly Property Name() As String
Get
Return "Easter"
End Get
End Property
Public Overrides ReadOnly Property MinArgs() As Integer
Get
Return 1
End Get
End Property
Public Overrides ReadOnly Property MaxArgs() As Integer
Get
Return 1
End Get
End Property
Public Overrides Function Evaluate(ByVal args() As Object) As Object
'Gauss's algorithm
'This algorithm for calculating the date of Easter Sunday was first presented by the mathematician
'Carl Friedrich Gauss.
'The number of the year is denoted by Y; mod denotes the remainder of integer division
'(e.g. 13 mod 5 = 3; see modular arithmetic). Calculate first a, b and c:
' A = y Mod 19
' B = y Mod 4
' C = y Mod 7
'Then calculate
'd = (19a + M) mod 30
'e = (2b + 4c + 6d + N) mod 7
'For the Julian calendar (used in eastern churches) M = 15 and N = 6,
'and for the Gregorian calendar (used in western churches) M and N are from the following table:
' Years M N
'1583-1699 22 2
'1700-1799 23 3
'1800-1899 23 4
'1900-2099 24 5
'2100-2199 24 6
'2200-2299 25 0
'If d + e < 10 then Easter is on the (d + e + 22)th of March, and is otherwise on the (d + e - 9)th of April.
'The following exceptions must be taken into account:
'If the date given by the formula is the 26th of April, Easter is on the 19th of April.
'If the date given by the formula is the 25th of April, with d = 28, e = 6,and a > 10, Easter is on the 18th of April.
Dim l As Integer = args.Length
Dim lookupValue As String = args(0).ToString()
Try
Dim lv As FarPoint.CalcEngine.CalcReference = args(0)
lookupValue = lv.GetValue(lv.Row, lv.Column)
Catch
lookupValue = args(0).ToString()
End Try
Dim A, B, C, D, e, TagesZahl As Integer
Dim Jahreszahl As Integer = CInt(lookupValue)
A = Jahreszahl Mod 19
B = Jahreszahl Mod 4
C = Jahreszahl Mod 7
D = (19 * A + 24) Mod 30
e = (2 * B + 4 * C + 6 * D + 5) Mod 7
TagesZahl = 22 + D + e
If TagesZahl > 31 Then
TagesZahl = TagesZahl - 31
If TagesZahl = 26 Then TagesZahl = 19
If (TagesZahl = 25 And D = 28 And A > 10) Then TagesZahl = 18
Return CDate(TagesZahl.ToString + ".04." + Jahreszahl.ToString) ' DateSerial(JahresZahl, 4, TagesZahl)
Else
Return CDate(TagesZahl.ToString + ".03." + Jahreszahl.ToString) ' DateSerial(JahresZahl, 3, TagesZahl)
End If
Return 0 'FarPoint.CalcEngine.CalcError.NotAvailable
End Function
Public Overrides Function AcceptsReference(ByVal i As Integer) As Boolean
Return True
End Function
End Class