久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  1. <tfoot id='s4XYW'></tfoot>
  2. <small id='s4XYW'></small><noframes id='s4XYW'>

    1. <legend id='s4XYW'><style id='s4XYW'><dir id='s4XYW'><q id='s4XYW'></q></dir></style></legend>
    2. <i id='s4XYW'><tr id='s4XYW'><dt id='s4XYW'><q id='s4XYW'><span id='s4XYW'><b id='s4XYW'><form id='s4XYW'><ins id='s4XYW'></ins><ul id='s4XYW'></ul><sub id='s4XYW'></sub></form><legend id='s4XYW'></legend><bdo id='s4XYW'><pre id='s4XYW'><center id='s4XYW'></center></pre></bdo></b><th id='s4XYW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='s4XYW'><tfoot id='s4XYW'></tfoot><dl id='s4XYW'><fieldset id='s4XYW'></fieldset></dl></div>
      • <bdo id='s4XYW'></bdo><ul id='s4XYW'></ul>

    3. .NET Date to string 在 Vista 偽文化中提供無(wú)效字符串

      .NET Date to string gives invalid strings in Vista Pseudo-cultures(.NET Date to string 在 Vista 偽文化中提供無(wú)效字符串)
        <tfoot id='qkcH4'></tfoot>
          <tbody id='qkcH4'></tbody>
        <legend id='qkcH4'><style id='qkcH4'><dir id='qkcH4'><q id='qkcH4'></q></dir></style></legend>
            <bdo id='qkcH4'></bdo><ul id='qkcH4'></ul>

            <small id='qkcH4'></small><noframes id='qkcH4'>

                <i id='qkcH4'><tr id='qkcH4'><dt id='qkcH4'><q id='qkcH4'><span id='qkcH4'><b id='qkcH4'><form id='qkcH4'><ins id='qkcH4'></ins><ul id='qkcH4'></ul><sub id='qkcH4'></sub></form><legend id='qkcH4'></legend><bdo id='qkcH4'><pre id='qkcH4'><center id='qkcH4'></center></pre></bdo></b><th id='qkcH4'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qkcH4'><tfoot id='qkcH4'></tfoot><dl id='qkcH4'><fieldset id='qkcH4'></fieldset></dl></div>

              • 本文介紹了.NET Date to string 在 Vista 偽文化中提供無(wú)效字符串的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                問(wèn)題描述

                我的計(jì)算機(jī)配置了一種文化 .這樣的建議對(duì)我來(lái)說(shuō)毫無(wú)意義.

                我會(huì)避免與想要通過(guò)更改標(biāo)題來(lái)改變我的問(wèn)題含義的人發(fā)生爭(zhēng)執(zhí).但問(wèn)題不僅限于偽語(yǔ)言環(huán)境,特別是旨在發(fā)現(xiàn)應(yīng)用程序中的錯(cuò)誤.

                獎(jiǎng)金聊天

                這是來(lái)自世界各地的唯一日期格式列表:

                • 11.11.25
                • 2011 年 11 月 25 日
                • 2011 年 11 月 25 日
                • 2011.11.25
                • 2011.11.25.
                • 2011/11/25
                • 2011-11-25
                    1. 2011

                • 25.11.11
                • 25.11.2011
                • 25.11.2011 г.
                • 2011 年 11 月 25 日.
                • 25//11//2011
                • 2011 年 11 月 25 日
                • 25/11/2011
                • 2554 年 11 月 25 日
                • 25-11-11
                • 25-11-2011
                • 29/12/32

                特別感興趣的是最后一個(gè)不使用公歷的例子:

                • 阿拉伯語(yǔ)(沙特阿拉伯)ar-SA:29/12/32 02:03:07 ?
                • Divehi(馬爾代夫)dv-MV:29/12/32 14:03:07
                • 達(dá)里語(yǔ)/普什圖語(yǔ)(阿富汗)prf-AF/ps-AF:29/12/32 2:03:07 ?.?

                雖然這些是您永遠(yuǎn)不必?fù)?dān)心的極端情況.

                <小時(shí)>

                2011 年 14 月 12 日更新:

                另外一個(gè)bug的演示是Datetime.Parse不能解析DateTime.ToString:

                String s = DateTime.Today.ToString("d");//返回14////12////2011"DateTime d = DateTime.Parse(s);//期望dd//MM//yyyy"

                .Parse 拋出異常.

                <小時(shí)>

                更新 02//8, 2012 09::56'12:

                除了不正確之外,日期分隔符 的任何使用都已被棄用.來(lái)自 MSDN:

                <塊引用>

                LOCALE_SDATE

                Windows Vista 及更高版本:此常量已棄用.使用 LOCALE_SSHORTDATE 代替.自定義語(yǔ)言環(huán)境可能沒有單個(gè)統(tǒng)一的分隔符.例如,2006 年 12 月 31 日"這樣的格式是有效的.

                LOCALE_STIME

                Windows Vista 及更高版本:此常量已棄用.請(qǐng)改用 LOCALE_STIMEFORMAT.自定義語(yǔ)言環(huán)境可能沒有單個(gè)統(tǒng)一的分隔符.例如,03:56'23"這樣的格式是有效的.

                解決方案

                這個(gè)特定的錯(cuò)誤是由于在 ShortDatePattern 等模式中沒有轉(zhuǎn)義的一些特殊字符的轉(zhuǎn)換.

                ShortDatePattern = "d//MM//yyyy";

                模式中的

                / 表示插入日期分隔符",但是當(dāng)字符串從系統(tǒng)復(fù)制到 DateTimeFormat<時(shí),擴(kuò)展已經(jīng)完成(至少在我的系統(tǒng)上)/代碼>結(jié)構(gòu).可悲的是它缺少轉(zhuǎn)義(顯然在任何不使用特殊字符作為分隔符的語(yǔ)言上不可見,并且在英語(yǔ)中不可見,因?yàn)樗惶鎿Q為自身)

                唯一的解決方案似乎是在 DateTimeFormat 實(shí)例的所有模式中轉(zhuǎn)義分隔符:

                var c = new System.Globalization.CultureInfo("qps-ploc", true);c.DateTimeFormat.ShortDatePattern =c.DateTimeFormat.ShortDatePattern.Replace("/", "'/'");c.DateTimeFormat.LongTimePattern =c.DateTimeFormat.LongTimePattern.Replace(":", "':'");Console.WriteLine(DateTime.Now.ToString(c));

                <小時(shí)>

                這是所有三種常見情況的完整代碼示例

                日期到字符串

                ///<summary>將日期轉(zhuǎn)換為當(dāng)前語(yǔ)言環(huán)境中的短日期字符串(例如 30//11//2011)</summary>///<param name="value">要轉(zhuǎn)換為短日期字符串的日期時(shí)間</param>///<returns>包含日期本地化版本的字符串</returns>公共靜態(tài)字符串 DateToStr(日期時(shí)間值){字符串格式 = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;//.NET 中的錯(cuò)誤在于它假定日期模式中的/"表示日期分隔符"http://.NET沒有意識(shí)到Windows返回的語(yǔ)言環(huán)境字符串是Windows格式字符串.//錯(cuò)誤暴露在使用兩個(gè)斜杠作為日期分隔符的語(yǔ)言環(huán)境中://dd//MM//yyyy//哪些 .NET 會(huì)誤解://30////11////2011//當(dāng)它真的應(yīng)該從字面上理解為://dd'//'MM'//'yyyy//這就是這個(gè)修復(fù)的作用format = format.Replace("/", "'/'");返回值.ToString(格式);}

                字符串時(shí)間

                ///<總結(jié)>///使用當(dāng)前語(yǔ)言環(huán)境中的短時(shí)間格式將時(shí)間轉(zhuǎn)換為字符串(例如 7::21 AM)///</總結(jié)>///<param name="value">將時(shí)間部分轉(zhuǎn)換為本地化字符串的 DateTime</param>///<returns>包含時(shí)間本地化版本的字符串</returns>公共靜態(tài)字符串 TimeToStr(日期時(shí)間值){字符串格式 = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;//.NET 中的錯(cuò)誤在于它假定時(shí)間模式中的:"表示時(shí)間分隔符"http://.NET沒有意識(shí)到Windows返回的語(yǔ)言環(huán)境字符串是Windows格式字符串.//錯(cuò)誤暴露在使用兩個(gè)冒號(hào)作為時(shí)間分隔符的語(yǔ)言環(huán)境中://h::mm::ss tt//哪些 .NET 會(huì)誤解://11::::39::::17 AM//當(dāng)它真的應(yīng)該從字面上理解為://h'::'mm'::'ss tt//這就是這個(gè)修復(fù)的作用格式 = format.Replace(":", "':'");返回值.ToString(格式);}

                日期時(shí)間到字符串

                ///<總結(jié)>///將日期時(shí)間轉(zhuǎn)換為當(dāng)前語(yǔ)言環(huán)境中的字符串(例如 30//11//2001 7::21 AM)///</總結(jié)>///<param name="datetime">要轉(zhuǎn)換為當(dāng)前語(yǔ)言環(huán)境中通用字符串的日期時(shí)間</param>///<returns>包含日期時(shí)間的本地化版本的字符串</returns>公共靜態(tài)字符串 DateTimeToStr(DateTime 日期時(shí)間){返回 DateToStr(datetime)+" "+TimeToStr(datetime);}

                My computer is configured with a culture that is not en-US.

                When using the native Win32 GetDateFormat function, i get correctly formatted dates:

                • 22//11//2011 4::42::53 P??M]

                This is correct; and is also how Windows renders it:

                • the taskbar

                • Region and Language settings

                • Windows Explorer

                • Outlook

                When i try to convert a date to a string in .NET using my current locale, e.g.:

                DateTime.Now.ToString();
                DateTime.Now.ToString(CultureInfo.CurrentCulture);
                

                i get an incorrect date:

                • 22////11////2011 4::::42::::53 P??M]

                This bug in .NET is evident anyplace in Windows that uses the buggy .NET code:

                • Windows Event Viewer:

                • Task Scheduler:

                • SQL Server Management Studio:

                How do i make .NET not buggy?

                How do i convert dates and times to strings using the current culture (correctly)?

                Note: The user is allowed to set their Windows to any locale preferences they want. As it is now, my program will not handle valid settings properly. Telling the user, "Don't do that" is pretty mean-spirited.

                A similar example comes from Delphi, which assumes that a date separator can never be more than one character. When Windows is configured with a locale that uses multiple characters for the date separator, e.g.:

                • sk-SK (Slovak - Slovakia) : .

                where dates should be formatted as:

                22. 11. 2011
                

                the code library fails to accept a date separator longer than one character, and falls back to:

                22/11/2011
                

                In the past some might suggest that you not to bother with such edge cases. Such suggestions carry no weight with me.

                i'll avoid getting into a pissing match with someone who wants to alter the meaning of my question by changing the title. But the question is not limited to pseudo-locales, specifically designed to find bugs in applications.

                Bonus Chatter

                Here's a unique list of date formats from around the world:

                • 11.11.25
                • 11.25.2011
                • 11/25/2011
                • 2011.11.25
                • 2011.11.25.
                • 2011/11/25
                • 2011-11-25
                    1. 2011

                • 25.11.11
                • 25.11.2011
                • 25.11.2011 г.
                • 25.11.2011.
                • 25//11//2011
                • 25/11 2011
                • 25/11/2011
                • 25/11/2554
                • 25-11-11
                • 25-11-2011
                • 29/12/32

                Of particular interest is the last example which doesn't use the gregorian calendar:

                • Arabic (Saudi Arabia) ar-SA: 29/12/32 02:03:07 ?
                • Divehi (Maldives) dv-MV: 29/12/32 14:03:07
                • Dari/Pashto (Afghanistan) prf-AF / ps-AF: 29/12/32 2:03:07 ?.?

                Although those are edge cases that you'd never have to worry about.


                Update 14//12//2011:

                Another demonstration of the bug is that Datetime.Parse cannot parse DateTime.ToString:

                String s = DateTime.Today.ToString("d");   //returns "14////12////2011"
                DateTime d = DateTime.Parse(s);            //expects "dd//MM//yyyy"
                

                The .Parse throws an exception.


                Update 02//8, 2012 09::56'12:

                Any use of a date separator is depricated, in addition to being incorrect. From MSDN:

                LOCALE_SDATE

                Windows Vista and later: This constant is deprecated. Use LOCALE_SSHORTDATE instead. A custom locale might not have a single, uniform separator character. For example, a format such as "12/31, 2006" is valid.

                LOCALE_STIME

                Windows Vista and later: This constant is deprecated. Use LOCALE_STIMEFORMAT instead. A custom locale might not have a single, uniform separator character. For example, a format such as "03:56'23" is valid.

                解決方案

                This specific bug is due to the transformation of some special characters that aren't escaped in the patterns like ShortDatePattern.

                ShortDatePattern = "d//MM//yyyy";
                

                / in a pattern means "insert the date separator" but here the expansion is already done (at least on my system) when the string is copied from the system to the DateTimeFormat structure. Sadly it is missing an escaping (Obviously not visible on any language not using a special character as a separator and not visible in english as it is replaced with itself)

                The only solution seem to be to escape the separators in all the patterns of the DateTimeFormat instance :

                var c = new System.Globalization.CultureInfo("qps-ploc", true);
                c.DateTimeFormat.ShortDatePattern =
                        c.DateTimeFormat.ShortDatePattern.Replace("/", "'/'");
                c.DateTimeFormat.LongTimePattern =
                        c.DateTimeFormat.LongTimePattern.Replace(":", "':'");
                Console.WriteLine(DateTime.Now.ToString(c));
                


                Here's full code samples for all three common cases

                Date to string

                /// <summary>Convert a date to the short date string in the current locale (e.g. 30//11//2011)</summary>
                /// <param name="value">A DateTime to be converted to a short date string</param>
                /// <returns>A string containing the localized version of the date</returns>
                public static String DateToStr(DateTime value)
                {
                    String format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
                
                    //The bug in .NET is that it assumes "/" in a date pattern means "the date separator"
                    //What .NET doesn't realize is that the locale strings returned by Windows are the Windows format strings. 
                    //The bug is exposed in locale's that use two slashes as for their date separator:
                    //  dd//MM//yyyy
                    // Which .NET misinterprets to give:
                    //  30////11////2011
                    // when really it should be taken literally to be:
                    //  dd'//'MM'//'yyyy
                    //which is what this fix does
                    format = format.Replace("/", "'/'"); 
                
                    return value.ToString(format);
                }
                

                Time to string

                /// <summary>
                /// Convert a time to string using the short time format in the current locale(e.g. 7::21 AM)
                /// </summary>
                /// <param name="value">A DateTime who's time portion will be converted to a localized string</param>
                /// <returns>A string containing the localized version of the time</returns>
                public static String TimeToStr(DateTime value)
                {
                    String format = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
                
                    //The bug in .NET is that it assumes ":" in a time pattern means "the time separator"
                    //What .NET doesn't realize is that the locale strings returned by Windows are the Windows format strings. 
                    //The bug is exposed in locale's that use two colons as their time separator:
                    //  h::mm::ss tt
                    // Which .NET misinterprets to give:
                    //  11::::39::::17 AM
                    // when really it should be taken literally to be:
                    //  h'::'mm'::'ss tt
                    //which is what this fix does
                    format = format.Replace(":", "':'"); 
                
                    return value.ToString(format);
                }
                

                Datetime to string

                /// <summary>
                /// Convert a datetime to a string in the current locale (e.g. 30//11//2001 7::21 AM) 
                /// </summary>
                /// <param name="datetime">A DateTime to be converted to a general string in the current locale</param>
                /// <returns>A string containing the localized version of the datetime</returns>
                public static String DateTimeToStr(DateTime datetime)
                {
                    return DateToStr(datetime)+" "+TimeToStr(datetime);
                }
                

                這篇關(guān)于.NET Date to string 在 Vista 偽文化中提供無(wú)效字符串的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時(shí)忽略空格)
                extracting just page text using HTMLAgilityPack(使用 HTMLAgilityPack 僅提取頁(yè)面文本)
                C# extracting data from XML(C# 從 XML 中提取數(shù)據(jù))
                Read a XML (from a string) and get some fields - Problems reading XML(讀取 XML(從字符串)并獲取一些字段 - 讀取 XML 時(shí)出現(xiàn)問(wèn)題)
                Reading large XML documents in .net(在 .net 中讀取大型 XML 文檔)
                How to create folder in Google Drive using .NET API?(如何使用 .NET API 在 Google Drive 中創(chuàng)建文件夾?)
                <tfoot id='NiYcI'></tfoot>

                <small id='NiYcI'></small><noframes id='NiYcI'>

                <legend id='NiYcI'><style id='NiYcI'><dir id='NiYcI'><q id='NiYcI'></q></dir></style></legend>

                      <i id='NiYcI'><tr id='NiYcI'><dt id='NiYcI'><q id='NiYcI'><span id='NiYcI'><b id='NiYcI'><form id='NiYcI'><ins id='NiYcI'></ins><ul id='NiYcI'></ul><sub id='NiYcI'></sub></form><legend id='NiYcI'></legend><bdo id='NiYcI'><pre id='NiYcI'><center id='NiYcI'></center></pre></bdo></b><th id='NiYcI'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='NiYcI'><tfoot id='NiYcI'></tfoot><dl id='NiYcI'><fieldset id='NiYcI'></fieldset></dl></div>

                          <tbody id='NiYcI'></tbody>
                          <bdo id='NiYcI'></bdo><ul id='NiYcI'></ul>
                        • 主站蜘蛛池模板: 一区二区在线不卡 | 免费看黄色视屏 | 国产91久久久久 | 久久无毛 | 色婷综合网 | 日韩欧美不卡 | 亚洲精品www久久久久久广东 | 国产黄色av网站 | 久久久久久国产精品免费免费 | 午夜av成人 | 成人国内精品久久久久一区 | www久久国产 | 亚洲毛片在线观看 | h片在线看 | 欧美中文字幕一区二区 | 91精品久久久久久久久 | 一区二区三区视频在线免费观看 | 国产精品久久九九 | 国产福利91精品一区二区三区 | 国产羞羞视频在线观看 | aaaaaaa片毛片免费观看 | 中文字幕国 | 国产精品久久久久久婷婷天堂 | 欧美在线a | 国产h视频 | 中文字幕一区二区三区不卡在线 | 免费观看一级特黄欧美大片 | 久久精品播放 | 成人高清视频在线观看 | 亚洲a视频 | 欧美在线观看网站 | 日韩欧美在线观看 | 国产精品久久久久久妇女 | 91丨国产| 成人免费区一区二区三区 | 国产精品视频一区二区三区不卡 | 夜夜草av| 黄色一级大片视频 | 日韩亚洲一区二区 | av资源中文在线天堂 | 国产精品一区视频 |