Is it the weekend?

The .NET class CultureInfo.DateTimeFormat (which returns a DateTimeFormatInfo object) has properties for most everything around a date. Except…

There is no way to determine what days are a weekend. Lots of requests out there for this, but no solution. So I wrote an extension method to return, for a given country, what days of the week are workdays and what are weekends.

I also added two extension methods to return: 1) the English name for the language, and 2) the English name for the country. CultureInfo.EnglishName returns “language (country)” but has no way to return them individually.

This is in C# but could easily be converted to most any other language. Full project here.

 

using System;
using System.Globalization;
namespace windward
{
 /// <summary>
 /// Extensions for the CultureInfo class.
 /// </summary>
 public static class CultureInfoExtensions
 {
 /// <summary>
 /// The weekday/weekend state for a given day.
 /// </summary>
 public enum WeekdayState
 {
 /// <summary>
 /// A work day.
 /// </summary>
 Workday,
 /// <summary>
 /// A weekend.
 /// </summary>
 Weekend,
 /// <summary>
 /// Morning is a workday, afternoon is the start of the weekend.
 /// </summary>
 WorkdayMorning
 }
/// <summary>
 /// Returns the English version of the country name. Extracted from the CultureInfo.EnglishName.
 /// </summary>
 /// <param name="ci">The CultureInfo this object.</param>
 /// <returns>The English version of the country name.</returns>
 public static string GetCountryEnglishName(this CultureInfo ci)
 {
 string[] parts = ci.EnglishName.Split(new[] {'(', ')'}, StringSplitOptions.RemoveEmptyEntries);
 if (parts.Length < 2)
 return ci.EnglishName;
 parts = parts[1].Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
 return parts[parts.Length - 1].Trim();
 }
/// <summary>
 /// Returns the English version of the language name. Extracted from the CultureInfo.EnglishName.
 /// </summary>
 /// <param name="ci">The CultureInfo this object.</param>
 /// <returns>The English version of the language name.</returns>
 public static string GetLanguageEnglishName(this CultureInfo ci)
 {
 string[] parts = ci.EnglishName.Split(new[] {'('}, StringSplitOptions.RemoveEmptyEntries);
 return parts[0].Trim();
 }
/// <summary>
 /// Return if the passed in day of the week is a weekend.
 /// 
 /// note: state pulled from http://en.wikipedia.org/wiki/Workweek_and_weekend
 /// </summary>
 /// <param name="ci">The CultureInfo this object.</param>
 /// <param name="day">The Day of the week to return the stat of.</param>
 /// <returns>The weekday/weekend state of the passed in day of the week.</returns>
 public static WeekdayState IsWeekend(this CultureInfo ci, DayOfWeek day)
 {
 string[] items = ci.Name.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries);
 switch (items[items.Length - 1])
 {
 case "DZ": // Algeria
 case "BH": // Bahrain
 case "BD": // Bangladesh
 case "EG": // Egypt
 case "IQ": // Iraq
 case "IL": // Israel
 case "JO": // Jordan
 case "KW": // Kuwait
 case "LY": // Libya
 // Northern Malaysia (only in the states of Kelantan, Terengganu, and Kedah)
 case "MV": // Maldives
 case "MR": // Mauritania
 case "NP": // Nepal
 case "OM": // Oman
 case "QA": // Qatar
 case "SA": // Saudi Arabia
 case "SD": // Sudan
 case "SY": // Syria
 case "AE": // U.A.E.
 case "YE": // Yemen
 return day == DayOfWeek.Thursday || day == DayOfWeek.Friday
 ? WeekdayState.Weekend
 : WeekdayState.Workday;
case "AF": // Afghanistan
 case "IR": // Iran
 if (day == DayOfWeek.Thursday)
 return WeekdayState.WorkdayMorning;
 return day == DayOfWeek.Friday ? WeekdayState.Weekend : WeekdayState.Workday;
case "BN": // Brunei Darussalam
 return day == DayOfWeek.Friday || day == DayOfWeek.Sunday
 ? WeekdayState.Weekend
 : WeekdayState.Workday;
case "MX": // Mexico
 case "TH": // Thailand
 if (day == DayOfWeek.Saturday)
 return WeekdayState.WorkdayMorning;
 return day == DayOfWeek.Saturday || day == DayOfWeek.Sunday
 ? WeekdayState.Weekend
 : WeekdayState.Workday;
}
// most common Saturday/Sunday
 return day == DayOfWeek.Saturday || day == DayOfWeek.Sunday ? WeekdayState.Weekend : WeekdayState.Workday;
 }
 }
}