///<summary>Creates a YearlyZmanimDictionary for the given year.</summary>
        ///<returns>The Zmanim for that year, or null if they could not be loaded.</returns>
        protected override YearlyZmanimDictionary CreateYear(int year)
        {
            var days = new ZmanimInfo[DateTime.IsLeapYear(year) ? 366 : 365];

            for (int i = 0; i < days.Length; i++) {
                var date = new DateTime(year, 1, 1).AddDays(i);
                days[i] = new ZmanimInfo(date, CalculateZmanim(date));
            }
            return new YearlyZmanimDictionary(year, days);
        }
Beispiel #2
0
        ///<summary>Creates a YearlyZmanimDictionary for the given year.</summary>
        ///<returns>The Zmanim for that year, or null if they could not be loaded.</returns>
        protected override YearlyZmanimDictionary CreateYear(int year)
        {
            var days = new ZmanimInfo[DateTime.IsLeapYear(year) ? 366 : 365];

            for (int i = 0; i < days.Length; i++)
            {
                var date = new DateTime(year, 1, 1).AddDays(i);
                days[i] = new ZmanimInfo(date, CalculateZmanim(date));
            }
            return(new YearlyZmanimDictionary(year, days));
        }
        void WriteYear(ZmanimInfo[] year, bool writeDateColumn)
        {
            var columns = year[0].Times.Keys.ToArray();
            using (var writer = File.CreateText(Path.Combine(DataFolder, year[0].Date.Year.ToString(CultureInfo.InvariantCulture) + "." + Extension))) {
                writer.WriteLine(CsvHeader);
                if (writeDateColumn)
                    writer.Write("Date,");

                for (int i = 0; i < columns.Length; i++) {
                    if (i > 0) writer.Write(',');
                    writer.Write(columns[i].Replace(',', '_'));
                }
                writer.WriteLine();
                foreach (var day in year) {
                    if (writeDateColumn)
                        writer.Write("{0:MM/dd/yyyy},", day.Date);

                    for (int i = 0; i < columns.Length; i++) {
                        if (i > 0) writer.Write(',');
                        var time = day[columns[i]];
                        writer.Write("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
                    }
                    writer.WriteLine();
                }
            }
        }
        ///<summary>Creates a YearlyZmanimDictionary for the given year.</summary>
        ///<returns>The Zmanim for that year, or null if they could not be loaded.</returns>
        protected override YearlyZmanimDictionary CreateYear(int year)
        {
            var path = Path.Combine(DataFolder, year.ToString(CultureInfo.InvariantCulture) + "." + Extension);
            if (!File.Exists(path)) return null;
            var values = new ZmanimInfo[DateTime.IsLeapYear(year) ? 366 : 365];
            var dayOfYear = 0;
            string[] columnNames = null;
            bool hasDateColumn = false;
            string line;
            using (var reader = File.OpenText(path)) {
                while (null != (line = reader.ReadLine())) {
                    if (line.Length == 0) continue;
                    if (line[0] == '#') continue;

                    if (columnNames == null) {
                        columnNames = line.Split(Separator);
                        if (columnNames[0].StartsWith("Date", StringComparison.OrdinalIgnoreCase)) {
                            var oldCols = columnNames;
                            columnNames = new string[oldCols.Length - 1];
                            Array.Copy(oldCols, 1, columnNames, 0, oldCols.Length - 1);
                            hasDateColumn = true;
                        }

                        for (int i = 0; i < columnNames.Length; i++) {
                            columnNames[i] = columnNames[i].Trim();
                        }
                    } else {
                        int index = hasDateColumn ? line.IndexOf(Separator) + 1 : 0;
                        if (line.Length != index + 9 * columnNames.Length - 1)	//The last column doesn't have a trailing comma
                            throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path);
                        var times = new Dictionary<string, TimeSpan>(columnNames.Length);
                        for (int i = 0; i < columnNames.Length; i++) {
                            try {
                                times.Add(columnNames[i], new TimeSpan(
                                    int.Parse(line.Substring(index + 0, 2), CultureInfo.InvariantCulture),
                                    int.Parse(line.Substring(index + 3, 2), CultureInfo.InvariantCulture),
                                    int.Parse(line.Substring(index + 6, 2), CultureInfo.InvariantCulture))
                                );
                            } catch (Exception ex) {
                                throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path, ex);
                            }
                            if (i < columnNames.Length - 1 && line[index + 8] != ',')	//The last column doesn't have a trailing comma
                                throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path);
                            index += 9;
                        }
                        values[dayOfYear] = new ZmanimInfo(new DateTime(year, 1, 1).AddDays(dayOfYear), times);
                        dayOfYear++;
                    }
                }
            }
            return new YearlyZmanimDictionary(year, values);
        }
        ///<summary>Creates a YearlyZmanimDictionary for the given year.</summary>
        ///<returns>The Zmanim for that year, or null if they could not be loaded.</returns>
        protected override YearlyZmanimDictionary CreateYear(int year)
        {
            var path = Path.Combine(DataFolder, year.ToString(CultureInfo.InvariantCulture) + "." + Extension);

            if (!File.Exists(path))
            {
                return(null);
            }
            var values    = new ZmanimInfo[DateTime.IsLeapYear(year) ? 366 : 365];
            var dayOfYear = 0;

            string[] columnNames   = null;
            bool     hasDateColumn = false;
            string   line;

            using (var reader = File.OpenText(path)) {
                while (null != (line = reader.ReadLine()))
                {
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    if (line[0] == '#')
                    {
                        continue;
                    }

                    if (columnNames == null)
                    {
                        columnNames = line.Split(Separator);
                        if (columnNames[0].StartsWith("Date", StringComparison.OrdinalIgnoreCase))
                        {
                            var oldCols = columnNames;
                            columnNames = new string[oldCols.Length - 1];
                            Array.Copy(oldCols, 1, columnNames, 0, oldCols.Length - 1);
                            hasDateColumn = true;
                        }

                        for (int i = 0; i < columnNames.Length; i++)
                        {
                            columnNames[i] = columnNames[i].Trim();
                        }
                    }
                    else
                    {
                        int index = hasDateColumn ? line.IndexOf(Separator) + 1 : 0;
                        if (line.Length != index + 9 * columnNames.Length - 1)                          //The last column doesn't have a trailing comma
                        {
                            throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path);
                        }
                        var times = new Dictionary <string, TimeSpan>(columnNames.Length);
                        for (int i = 0; i < columnNames.Length; i++)
                        {
                            try {
                                times.Add(columnNames[i], new TimeSpan(
                                              int.Parse(line.Substring(index + 0, 2), CultureInfo.InvariantCulture),
                                              int.Parse(line.Substring(index + 3, 2), CultureInfo.InvariantCulture),
                                              int.Parse(line.Substring(index + 6, 2), CultureInfo.InvariantCulture))
                                          );
                            } catch (Exception ex) {
                                throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path, ex);
                            }
                            if (i < columnNames.Length - 1 && line[index + 8] != ',')                                   //The last column doesn't have a trailing comma
                            {
                                throw new DataException("Bad line:\r\n" + line + "\r\n in file " + path);
                            }
                            index += 9;
                        }
                        values[dayOfYear] = new ZmanimInfo(new DateTime(year, 1, 1).AddDays(dayOfYear), times);
                        dayOfYear++;
                    }
                }
            }
            return(new YearlyZmanimDictionary(year, values));
        }