根据文化寻找周末

发布于 2024-08-16 17:50:43 字数 64 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凶凌 2024-08-23 17:50:43

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

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

代码可以作为项目下载,网址为 现在是周末吗< /a>?主要代码如下:

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

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
{
    /// <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;
        }
    }
}
日暮斜阳 2024-08-23 17:50:43

我唯一知道的是如何获得一周的开始日期。也许这会有所帮助:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

System.Globalization,也许您会在这个包中找到一些东西。

有几个日历类,例如 JulianCalendar、HebrewCalendar 等。在那里也许能找到你想要的东西。

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.

野鹿林 2024-08-23 17:50:43

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

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

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

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

祝你好运!

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!

倾`听者〃 2024-08-23 17:50:43

发现这个问题很有趣 - 我自己没有答案..但找到了一个有趣的资源 - 也讨论了日历。

日历转换器

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

Calendar converter

撑一把青伞 2024-08-23 17:50:43

以下代码将一直有效,直到最后 2 天在文化中被视为周末。

:)

/// <summary>
/// Returns true if the specified date is weekend in given culture
/// is in. 
/// </summary>
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;         
}

阿米特·唐克

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

:)

/// <summary>
/// Returns true if the specified date is weekend in given culture
/// is in. 
/// </summary>
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文