Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, May 2, 2016

Finding weekend days based on culture

Finding weekend days based on culture


Is there a way to find the days that constitute a weekend or workweek based on different cultures using the .NET framework? For example, some Muslim countries have a workweek from Sunday through Thursday.

Answer by Mawg for Finding weekend days based on culture


I won't give you a .NET answer, but I will say that you won't base it on "culture", but rather on country.

You can get country from IP with a fir degree of accuracy (but it will never be 100%). After that, I would suggest a lot of googling, because I doubt that you are going to find the code already written.

(you might also look into some open source calendar/appointment programs, especially widely-used ones, like on Linux, or maybe Lightning, the Thunderbird plug-in. If you wade through their code, you might find the data for this)

Fortunately, though, you just face a grind, rather than something difficult to implement.

Good luck!

Answer by martin for Finding weekend days based on culture


The only thing i know is how to get the day the week starts. Perhaps this can help:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek  

from System.Globalization, perhaps you find in this package something.

There are several Calendar-Classses like JulianCalendar, HebrewCalendar and so on. It could be possible to find there what you want.

Answer by Anders for Finding weekend days based on culture


found the question interesting - did not have an answer myself.. but located an interesting resource - also discussing calendars.

Calendar converter

Answer by Amit Tonk for Finding weekend days based on culture


The below code will work till the time last 2 days are considered as weekends in cultures.

:)

///   /// Returns true if the specified date is weekend in given culture  /// is in.   ///   public static bool IsItWeekend(DateTime currentDay, CultureInfo cultureInfo)  {      bool isItWeekend = false;        DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;        DayOfWeek currentDayInProvidedDatetime = currentDay.DayOfWeek;        DayOfWeek lastDayOfWeek = firstDay + 4;        if (currentDayInProvidedDatetime == lastDayOfWeek + 1 || currentDayInProvidedDatetime == lastDayOfWeek + 2)          isItWeekend = true;        return isItWeekend;           }  

Amit Tonk

Answer by David Thielen for Finding weekend days based on culture


No one had a solution for this so I wrote one. This uses the country to determine if a day is a workday, weekend, or 1/2 workday (Saturday in some countries). There is some ambiguity in this as in Mexico a 1/2 day on Saturday is "customary" but not official. For cases like this, I set it as work time.

This covers everything except 3 provinces in Malaysia, which are different from the rest of Malaysia. AFAIK, CultureInfo.Name does not have a distinct value for those 3 provinces. Most interesting country, Brunei where the weekend is Friday & Sunday, with Saturday a workday.

Code is downloadable as a project at Is it the weekend? Main code below:

using System;  using System.Globalization;    namespace windward  {      ///       /// Extensions for the CultureInfo class.      ///       public static class CultureInfoExtensions      {          ///           /// The weekday/weekend state for a given day.          ///           public enum WeekdayState          {              ///               /// A work day.              ///               Workday,              ///               /// A weekend.              ///               Weekend,              ///               /// Morning is a workday, afternoon is the start of the weekend.              ///               WorkdayMorning          }            ///           /// Returns the English version of the country name. Extracted from the CultureInfo.EnglishName.          ///           /// The CultureInfo this object.          /// The English version of the country name.          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();          }            ///           /// Returns the English version of the language name. Extracted from the CultureInfo.EnglishName.          ///           /// The CultureInfo this object.          /// The English version of the language name.          public static string GetLanguageEnglishName(this CultureInfo ci)          {              string[] parts = ci.EnglishName.Split(new[] {'('}, StringSplitOptions.RemoveEmptyEntries);              return parts[0].Trim();          }            ///           /// Return if the passed in day of the week is a weekend.          ///           /// note: state pulled from http://en.wikipedia.org/wiki/Workweek_and_weekend          ///           /// The CultureInfo this object.          /// The Day of the week to return the stat of.          /// The weekday/weekend state of the passed in day of the week.          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;          }      }  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.