c# - Forcing DateTime.Parse to Fail for Invariant Dates -
i'll brief start, give details @ end. consider following code:
cultureinfo culturetotest = new cultureinfo("hu-hu"); thread.currentthread.currentculture = culturetotest; datetime testdatetime = new datetime(2014,12,13,23,24,25); string teststring = testdatetime.tostring(cultureinfo.invariantculture); datetime actualdatetime = datetime.parse(teststring);
the question whether there possible value of culturetotest
either cause datetime.parse
call throw exception or return wrong value?
context:
this set of unit tests. there body of code calls datetime.parse
without specifying culture. concern when code passed date in invariant or en-us cultures, code fail in cultures. proposed solution change code use
datetime.parse(string, cultureinfo.invariantculture)
in these cases.
in order unit test change, need call new code culture have made original datetime.parse(string)
fail, show changed code succeed.
the problem haven't yet found culture me. i'm going try manufacture one, thought i'd ask more general question first.
using code:
stringbuilder sb = new stringbuilder(); foreach (var culture in cultureinfo.getcultures(culturetypes.allcultures)) { try { thread.currentthread.currentculture = culture; datetime testdatetime = new datetime(2014, 12, 13, 23, 24, 25); string teststring = testdatetime.tostring(cultureinfo.invariantculture); datetime actualdatetime = datetime.parse(teststring); console.writeline(actualdatetime.day.tostring()); } catch (exception ex) { sb.appendline(culture.tostring()); } } console.writeline(sb.tostring());
the following cultures throw exception when parsing date:
am am-et ar ar-ae ar-bh ar-dz ar-eg ar-iq ar-jo ar-kw ar-lb ar-ly ar-ma ar-om ar-qa ar-sa ar-sy ar-tn ar-ye arn arn-cl as-in az az-cyrl az-cyrl-az az-latn az-latn-az ba ba-ru be-by bg bg-bg bn bn-bd bn-in br br-fr bs bs-cyrl bs-cyrl-ba bs-latn bs-latn-ba ca ca-es ca-es-valencia co co-fr cs cs-cz cy cy-gb da da-dk de de-at de-ch de-de de-li de-lu dsb dsb-de dv dv-mv el el-gr en-029 en-au en-bz en-gb en-hk en-ie en-in en-jm en-my en-nz en-sg en-tt en-zw es es-419 es-ar es-bo es-cl es-co es-cr es-do es-ec es-es es-gt es-hn es-mx es-ni es-pa es-pe es-pr es-py es-sv es-uy es-ve et et-ee fa fa-ir ff ff-latn ff-latn-sn fi fi-fi fo fo-fo fr fr-be fr-cd fr-ch fr-ci fr-cm fr-fr fr-ht fr-lu fr-ma fr-mc fr-ml fr-re fr-sn fy fy-nl ga ga-ie gd gd-gb gl gl-es gn gn-py gsw gsw-fr gu gu-in ha ha-latn ha-latn-ng he-il hi hi-in hr hr-ba hr-hr hsb hsb-de hy hy-am id id-id ig ig-ng is-is it-ch it-it iu iu-cans iu-cans-ca iu-latn iu-latn-ca jv jv-latn jv-latn-id ka ka-ge kk kk-kz kl kl-gl km km-kh kn kn-in kok kok-in ky ky-kg lb lb-lu lo lo-la lv lv-lv mg mg-mg mi mi-nz mk mk-mk ml ml-in mr mr-in ms ms-bn ms-my mt mt-mt my-mm nb nb-no nl nl-be nl-nl nn nn-no no nqo nqo-gn nso nso-za oc oc-fr om om-et or or-in pa pa-arab pa-arab-pk pa-in pt pt-ao pt-br pt-pt qut qut-gt quz quz-bo quz-ec quz-pe rm rm-ch ro ro-md ro-ro ru ru-ru rw rw-rw sa sa-in sah sah-ru sd sd-arab sd-arab-pk se se-fi se-no sk sk-sk sl sl-si sma-no smj-no smn smn-fi sms sms-fi sn sn-latn sn-latn-zw so-so sq sq-al sr sr-cyrl sr-cyrl-ba sr-cyrl-cs sr-cyrl-me sr-cyrl-rs sr-latn sr-latn-ba sr-latn-cs sr-latn-me sr-latn-rs sv-fi syr syr-sy ta ta-in ta-lk te te-in tg tg-cyrl tg-cyrl-tj th th-th ti ti-er ti-et tk tk-tm tn tn-bw tn-za tr tr-tr tt tt-ru tzm tzm-latn tzm-latn-dz tzm-tfng tzm-tfng-ma uk uk-ua ur ur-in ur-pk uz uz-cyrl uz-cyrl-uz uz-latn uz-latn-uz vi vi-vn wo wo-sn yo yo-ng zgh zgh-tfng zgh-tfng-ma zh-hant zh-hk zh-mo zh-sg zu zu-za zh-cht
Comments
Post a Comment