/// <summary> /// Returns the daylight changes for UTC (never). /// </summary> public override DaylightTime GetDaylightChanges(int year) { // We need a start and stop time DateTime start, end; Calendar cal = new GregorianCalendar(); // 2007+ at this point has a slightly different start/stop if (year >= 2007) { start = new DateTime(year, 4, 1, 2, 0, 0); end = new DateTime(year, 11, 1, 2, 0, 0); } else { start = new DateTime(year, 3, 1, 2, 0, 0); end = new DateTime(year, 12, 1, 2, 0, 0); } // Move forward for the start date until we have a sunday while (cal.GetDayOfWeek(start) != DayOfWeek.Sunday) start = cal.AddDays(start, 1); // Move backwards from the end date to the last sunday while (cal.GetDayOfWeek(end) != DayOfWeek.Sunday) start = cal.AddDays(end, 1); // Create it return new DaylightTime(start, end, new TimeSpan(stOffset - dtOffset, 0, 0)); }
public List<List<CalendarDay>> Create(IQueryable<Episode> episodeCollection, DateTime currentDate) { List<List<CalendarDay>> model = this.ConstructModel(); Calendar calendar = new GregorianCalendar(); const int CalendarDays = 42; DateTime startDate = new DateTime(currentDate.Year, currentDate.Month, 1); startDate = startDate.Subtract(new TimeSpan((int)calendar.GetDayOfWeek(startDate) - (int)DayOfWeek.Monday, 0, 0, 0)); DateTime endDate = startDate.Add(new TimeSpan(CalendarDays, 0, 0, 0)); List<CalendarEpisode> episodes = this.GetEpisodes(episodeCollection, startDate, endDate); int weekIndex = 0; for (int i = 0; i < CalendarDays; i++) { DayOfWeek dayOfWeek = calendar.GetDayOfWeek(startDate); this.AddEpisodes(model, episodes, startDate, weekIndex); startDate = startDate.AddDays(1); if (dayOfWeek == DayOfWeek.Sunday) { weekIndex++; } } return model; }
public void PosTest3() { System.Globalization.Calendar kC = new KoreanCalendar(); System.Globalization.Calendar gC = new GregorianCalendar(); DateTime dateTime = new GregorianCalendar().ToDateTime(2000, 2, 29, 0, 0, 0, 0); DayOfWeek expectedValue; DayOfWeek actualValue; expectedValue = gC.GetDayOfWeek(dateTime); actualValue = kC.GetDayOfWeek(dateTime); Assert.Equal(expectedValue, actualValue); }
public void PosTest4() { System.Globalization.Calendar kC = new KoreanCalendar(); System.Globalization.Calendar gC = new GregorianCalendar(); DateTime dateTime = new DateTime(_generator.GetInt64(-55) % (DateTime.MaxValue.Ticks + 1)); dateTime = new GregorianCalendar().ToDateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0); DayOfWeek expectedValue; DayOfWeek actualValue; expectedValue = gC.GetDayOfWeek(dateTime); actualValue = kC.GetDayOfWeek(dateTime); Assert.Equal(expectedValue, actualValue); }
public WorkDay(int year, int month, int day, GermanSpecialDays specialDays) : this() { this.year = year; this.month = month; this.day = day; var dt = new DateTime(year, month, day); var cal = new GregorianCalendar(); this.DayOfWeek = cal.GetDayOfWeek(dt); GermanSpecialDay specialDay; this.DayType = this.calculateDayType(dt, this.DayOfWeek, specialDays, out specialDay); this.SpecialDay = specialDay; }
public static List<ActivityDay> GetCalendarDaysFor(int? period/*0=yearly, 1=monthly and 2=weekly*/) { var days = new List<ActivityDay>(); //var calendar = new ActivityCalendar(); var calendarYear = new GregorianCalendar(); switch (period) { case 0: var dayNumber=(int)DateTime.Now.DayOfWeek; var weekDay = DateTime.Now.AddDays(-dayNumber); for (int i=0;i<7;i++) { days.Add(new ActivityDay { Date = weekDay, Name = calendarYear.GetDayOfWeek(weekDay).ToString(), Status=true, ActivityStatus = calendarYear.GetDayOfWeek(weekDay) == DayOfWeek.Friday ? DayStatus.Holiday :DayStatus.Active}); weekDay=weekDay.AddDays(1); } break; case 1: var daysInMonth=calendarYear.GetDaysInMonth(DateTime.Now.Year, DateTime.Now.Month); for (int day = 1; day <= daysInMonth;day++ ) { var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, day); days.Add(new ActivityDay { Date = date, Name = calendarYear.GetDayOfWeek(date).ToString(), Status = true, ActivityStatus = calendarYear.GetDayOfWeek(date) == DayOfWeek.Friday ? DayStatus.Holiday : DayStatus.Active }); } break; default: var daysInYr=calendarYear.GetDaysInYear(DateTime.Now.Year); var startOfYr = new DateTime(DateTime.Now.Year, 1, 1); for (int day = 0; day < daysInYr; day++) { var dateOfYr = new ActivityDay {Date=startOfYr.AddDays(day) }; dateOfYr.Name = calendarYear.GetDayOfWeek(dateOfYr.Date).ToString(); dateOfYr.Status = true; dateOfYr.ActivityStatus = calendarYear.GetDayOfWeek(dateOfYr.Date) == DayOfWeek.Friday ? DayStatus.Holiday : DayStatus.Active; days.Add(dateOfYr); } break; } return days; }
public static long problem19_1() { long sum = 0; GregorianCalendar calendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish); for (int year = 1901; year <= 2000; year++) { for (int month = 1; month <= 12; month++) { if (calendar.GetDayOfWeek(new DateTime(year, month, 1)) == DayOfWeek.Sunday) sum++; } } return sum; }
public static string GregToHijriCompleteWithDayEng(DateTime dtTime) { if (dtTime == DateTime.MinValue) return ""; UmAlQuraCalendar calHijri = new UmAlQuraCalendar(); GregorianCalendar calEng = new GregorianCalendar(); CultureInfo info = new CultureInfo("ar-sa"); info.DateTimeFormat.Calendar = calHijri; string weekday = Convert.ToString(calEng.GetDayOfWeek(dtTime)); int CurrDayOfMonth = calHijri.GetDayOfMonth(dtTime); int CurrMonth = calHijri.GetMonth(dtTime); int Curryear = calHijri.GetYear(dtTime); return string.Format("{3}/{2}/{1} h ", weekday, CurrDayOfMonth.ToString(), CurrMonth.ToString(), Curryear.ToString()); }
internal int GetWeekOfYearFullDays(DateTime time, CalendarWeekRule rule, int firstDayOfWeek, int fullDays) { int dayForJan1; int offset; int day; System.Globalization.Calendar gc = new GregorianCalendar(); int dayOfYear = gc.GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0. // // Calculate the number of days between the first day of year (1/1) and the first day of the week. // This value will be a positive value from 0 ~ 6. We call this value as "offset". // // If offset is 0, it means that the 1/1 is the start of the first week. // Assume the first day of the week is Monday, it will look like this: // Sun Mon Tue Wed Thu Fri Sat // 12/31 1/1 1/2 1/3 1/4 1/5 1/6 // +--> First week starts here. // // If offset is 1, it means that the first day of the week is 1 day ahead of 1/1. // Assume the first day of the week is Monday, it will look like this: // Sun Mon Tue Wed Thu Fri Sat // 1/1 1/2 1/3 1/4 1/5 1/6 1/7 // +--> First week starts here. // // If offset is 2, it means that the first day of the week is 2 days ahead of 1/1. // Assume the first day of the week is Monday, it will look like this: // Sat Sun Mon Tue Wed Thu Fri Sat // 1/1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 // +--> First week starts here. // Day of week is 0-based. // Get the day of week for 1/1. This can be derived from the day of week of the target day. // Note that we can get a negative value. It's ok since we are going to make it a positive value when calculating the offset. dayForJan1 = (int)gc.GetDayOfWeek(time) - (dayOfYear % 7); // Now, calucalte the offset. Substract the first day of week from the dayForJan1. And make it a positive value. offset = (firstDayOfWeek - dayForJan1 + 14) % 7; if (offset != 0 && offset >= fullDays) { // // If the offset is greater than the value of fullDays, it means that // the first week of the year starts on the week where Jan/1 falls on. // offset -= 7; } // // Calculate the day of year for specified time by taking offset into account. // day = dayOfYear - offset; if (day >= 0) { // // If the day of year value is greater than zero, get the week of year. // return (day / 7 + 1); } // // Otherwise, the specified time falls on the week of previous year. // Call this method again by passing the last day of previous year. // return (GetWeekOfYearFullDays(time.AddDays(-(dayOfYear + 1)), rule, firstDayOfWeek, fullDays)); }
internal int GetFirstDayWeekOfYear(DateTime time, int firstDayOfWeek) { System.Globalization.Calendar gc = new GregorianCalendar(); int dayOfYear = gc.GetDayOfYear(time) - 1; // Make the day of year to be 0-based, so that 1/1 is day 0. // Calculate the day of week for the first day of the year. // dayOfWeek - (dayOfYear % 7) is the day of week for the first day of this year. Note that // this value can be less than 0. It's fine since we are making it positive again in calculating offset. int dayForJan1 = (int)gc.GetDayOfWeek(time) - (dayOfYear % 7); int offset = (dayForJan1 - firstDayOfWeek + 14) % 7; //BCLDebug.Assert(offset >= 0, "Calendar.GetFirstDayWeekOfYear(): offset >= 0"); return ((dayOfYear + offset) / 7 + 1); }
public static string GetShortManReadable(DateTime datetime) { DateTime now = DateTime.Now; if (now.Year == datetime.Year)//以下的前提是两时间都为同一年 { TimeSpan span = now.Date - datetime.Date; int days = span.Days; switch (days) { case 1: return "昨天"; case 0: return "今天"; case -1: return "明天"; default: break; } if (days >= -14 || days <= 14) { GregorianCalendar gc = new GregorianCalendar(); int dateWeekofYear = gc.GetWeekOfYear(datetime, CalendarWeekRule.FirstDay, DayOfWeek.Monday); int nowWeekofYear = gc.GetWeekOfYear(now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); string dateDayofWeek = gc.GetDayOfWeek(datetime).ToString(); int weeks = nowWeekofYear - dateWeekofYear; switch (weeks) { case 1: return string.Format("上周{0}", WhichDay(dateDayofWeek)); case 0: return string.Format("本周{0}", WhichDay(dateDayofWeek)); case -1: return string.Format("下周{0}", WhichDay(dateDayofWeek)); default: break; } } if (days >= -62 || days <= 62) { int months = now.Month - datetime.Month; int dayofMonth = datetime.Day; switch (months) { case 1: return string.Format("上月{0}号", dayofMonth); case 0: return string.Format("本月{0}号", dayofMonth); case -1: return string.Format("下月{0}号", dayofMonth); default: break; } } } else//以下的前提是两时间不同年 { } return datetime.ToShortDateString(); }