Parse a date string into a certain timezone (supporting daylight saving time)(将日期字符串解析为某个时区(支持夏令时))
问题描述
好的,过去几周我一直在努力工作,但遇到了一个小问题.我不认为我的头脑现在完全能够胜任这项任务:) 所以我需要一些提示/帮助!这可能很简单,但我的脑袋还没动.
用户将在 AEST 中输入日期和时间.还有一个应用程序设置默认"时区(因为它可能需要更改),当前设置为澳大利亚东部标准时间"
所以我们在美国的服务器上有一个没有时区的用户字符串和一个定义的系统时区(所以本地不匹配,不能更改或使用)
现在我需要的是一种方式来表示使用时区 X 解析此用户输入的字符串"我不能只输入 +10 或 +11 作为偏移量,因为日期可能在夏令时之内或之外;是的,即使在同一时区,它也会在 +10 和 +11 之间改变!
当前 AEST 时间也可能在 DST 之内或之外,因此我不能只将 UTC 日期转换为当前 AEST 时间并获取zzz"字符串并将其附加,因为日期将相差一个小时在当前 DST 设置之外输入的任何内容.
现在代码实际上就是这样做的:
唯一的问题是,如果用户输入的日期在 DST 小时的变化上是正确的,它仍然可能是一个小时的时间,因为它只是读取当前的偏移量并使用它,然后检查它是否应该是夏令时与否,如果它在那里,它会读取不正确.但是,它比我现在拥有的要好几英里.
谁能帮我清理这个功能?这是我需要的最佳路线吗?想法?
这是一个预定义格式的简单解决方案,这也可以是动态的.我个人用它来与 javascript 交谈:
Ok i've been working very hard the last few weeks and i've come across a small problem. I don't think my mind is quite up to the task right now :) so i need some tips/help! it's likely very simple but my head isn't clicking yet.
Users will enter a date and time in AEST. There is also a application set "default" timezone (as it might need to change), currently its set to "AUS Eastern Standard Time"
So we have a user string with no time zone and a defined system time zone on a server in the USA (So local doesn't match and it can't be changed or used)
Now what i need is a way to say "parse this user entered string using the timezone X" i can't just enter +10 or +11 as the offset as the dates could be in or out of daylight savings; which yes does change it between +10 and +11 even for the same timezone!
The current AEST time might also be in or out of DST so i can't just convert a UTC date to the current AEST time and get the "zzz" string and attach it either as dates will be off by an hour for anything entered out of the current DST setting.
For now the code actually does just that:
Then i check if the date isn't valid by checking if it still == DateTimeOffset.MinValue or convert it to UTC and add to the database, it will the be converted back to AEST when displayed. However some dates are off by an hour and others are perfect (as expected) :)
What is the most elegant way to solve this?
EDIT:
To help explain the problem, i wrote some test code as a windows test application:
Whats the output of this?
Parsed: 2011/01/01 10:00:00 +10:00 UTC: 2011/01/01 00:00:00 +00:00 Changed Back: 2011/01/01 11:00:00 +11:00
Please note the hour is off by one and the offset is different. Additionally, what if we JUST change the date entered to:
we get:
Parsed: 2011/04/20 10:00:00 +10:00 UTC: 2011/04/20 00:00:00 +00:00 Changed Back: 2011/04/20 10:00:00 +10:00
Which is perfectly good and fine, using the same code just a different entered date.
This happens because the current DST setting and the DST setting of the entered date are different, this is what i want a solution for :)
Think of it like the chicken and egg problem. I need the correct timezone data for the entered string before i parse it which i can only get after i've parsed the string (so will be an elaborate solution)
Or i need .NET to parse the string using the myTimeZone object so it knows what to set it to itself, but i can't see any functions that do this, they all take a already parsed and set datetime or datetimeoffset object
So i'm looking for elegant solutions others might have done? I certainly can't be the only one who has noticed this?
EDIT2:
Ok i've made a 'working' function that solves the problem i think, here is an example (add a textbox to a c# windows app and use the code below to test yourself):
And the output:
Diff: +11:00 Read1: 2011/01/01 10:00:00 +11:00 Read1 - UTC: 2010/12/31 23:00:00 +00:00 Changed Back: 2011/01/01 10:00:00 +11:00 Diff: +10:00 Read2: 2011/04/20 10:00:00 +10:00 Read2 - UTC: 2011/04/20 00:00:00 +00:00 Changed Back: 2011/04/20 10:00:00 +10:00
Only thing is there might be a problem if the user entered date is right on the change over hour of DST it still might be an hour off as it's just reading the current offset and using that, then checking if its supposed to be daylight savings or not, and if its out there it would read incorrectly. However it's miles better than what i have now.
Can anyone maybe help me with cleaning up this function? Is this the best route go down for what i need? ideas?
Heres a simple solution for a predefined format, this can be dynamic as well. I personally use this for talking with javascript:
这篇关于将日期字符串解析为某个时区(支持夏令时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!