根据文化寻找周末

sdu*_*ooy 10 .net datetime cultureinfo

有没有办法根据不同的文化使用.NET框架找到构成周末或工作周的日子?例如,一些穆斯林国家从周日到周四有一个工作周.

Dav*_*len 5

没有人能解决这个问题,所以我写了一个。这使用国家/地区来确定一天是工作日、周末还是 1/2 工作日(某些国家/地区为星期六)。这其中存在一些含糊之处,因为在墨西哥,周六的 1/2 天是“惯例”,但不是官方的。对于这样的情况,我将其设置为工作时间。

这涵盖了马来西亚除 3 个省份之外的所有地区,这三个省份与马来西亚其他地区不同。AFAIK,CultureInfo.Name 对于这 3 个省份没有明显的值。最有趣的国家是文莱,周末是周五和周日,周六是工作日。

代码可以作为项目下载,是周末吗?主要代码如下:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Maw*_*awg 0

我不会给你一个 .NET 答案,但我会说你不会以“文化”为基础,而是以国家为基础。

您可以从 IP 获取国家/地区,准确度较高(但永远不会 100%)。之后,我建议您进行大量谷歌搜索,因为我怀疑您是否会找到已经编写的代码。

(您还可以研究一些开源日历/约会程序,尤其是广泛使用的程序,例如 Linux 上的程序,或者可能是 Lightning、Thunderbird 插件。如果您深入研究他们的代码,您可能会找到这方面的数据)

但幸运的是,你面临的只是磨难,而不是难以实施的事情。

祝你好运!