Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/Excel/Functions/DateTime/Weeknum.cs @ 12074

Last change on this file since 12074 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using OfficeOpenXml.FormulaParsing.ExpressionGraph;
7
8namespace OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime
9{
10    public class Weeknum : ExcelFunction
11    {
12        public override ExpressionGraph.CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
13        {
14            ValidateArguments(arguments, 1, eErrorType.Value);
15            var dateSerial = ArgToDecimal(arguments, 0);
16            var date = System.DateTime.FromOADate(dateSerial);
17            var startDay = DayOfWeek.Sunday;
18            if (arguments.Count() > 1)
19            {
20                var argStartDay = ArgToInt(arguments, 1);
21                switch (argStartDay)
22                {
23                    case 1:
24                        startDay = DayOfWeek.Sunday;
25                        break;
26                    case 2:
27                    case 11:
28                        startDay = DayOfWeek.Monday;
29                        break;
30                    case 12:
31                        startDay = DayOfWeek.Tuesday;
32                        break;
33                    case 13:
34                        startDay = DayOfWeek.Wednesday;
35                        break;
36                    case 14:
37                        startDay = DayOfWeek.Thursday;
38                        break;
39                    case 15:
40                        startDay = DayOfWeek.Friday;
41                        break;
42                    case 16:
43                        startDay = DayOfWeek.Saturday;
44                        break;
45                    default:
46                        // Not supported
47                        ThrowExcelErrorValueException(eErrorType.Num);
48                        break;
49                }
50            }
51            if (DateTimeFormatInfo.CurrentInfo == null)
52            {
53                throw new InvalidOperationException(
54                    "Could not execute Weeknum function because DateTimeFormatInfo.CurrentInfo was null");
55            }
56            var week = DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay,
57                                                                             startDay);
58            return CreateResult(week, DataType.Integer);
59        }
60       
61       
62    }
63}
Note: See TracBrowser for help on using the repository browser.