本文介绍了以文化求周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出构成一个周末或每周工作时间根据使用.NET Framework不同文化的日子吗?例如,一些穆斯林国家,到周四已经从周日的工作周。

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.

推荐答案

没有人有一个解决方案,所以我写的。本品采用国家确定一个天是工作日,周末或工作日的1/2(星期六在一些国家)。有一个在这方面的一些模糊性在墨西哥半天上周六是习惯,而不是官员。对于这样的情况下,我将其设置为工作时间。

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.

这涵盖了除3个省份在马来西亚,这是来自马来西亚的与众不同。 AFAIK,CultureInfo.Name不具备这3个省份不同的值。最有趣的国家,文莱那里的周末是周五和放大器;周日与周六工作日。

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是下载于的?下面主要code:

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

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;
        }
    }
}

这篇关于以文化求周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:35