【发布时间】:2009-11-12 15:56:14
【问题描述】:
我有一个来自用户的时区,必须将其转换为总分钟数才能存储在数据库中。我有以下代码,它看起来很丑。我是 C# 新手,想知道是否有更好的方法来做到这一点。
string tz = userList.Rows[0][1].ToString().Trim();
//Timezones can take the form of + or - followed by hour and then minutes in 15 minute increments.
Match tzre = new Regex(@"^(\+|-)?(0?[0-9]|1[0-2])(00|15|30|45)$").Match(tz);
if (!tzre.success)
{
throw new
myException("Row 1, column 2 of the CSV file to be imported must be a valid timezone: " + tz);
}
GroupCollection tzg = tzre.Groups;
tz = Convert.ToInt32(tzg[0].Value + Convert.ToString(Convert.ToInt32(tzg[1].Value) * 60 + Convert.ToInt32(tzg[2]))).ToString();
【问题讨论】:
-
经过一番研究,我找到了TimeSpan。
-
您知道数据库的 DateTime 值(并将其存储为 UTC)。也许比存储分钟更好
-
你能举例说明 userList.Rows[0][1].ToString() 中有什么吗?
-
@PoweRoy,我无法选择我的数据库如何存储时区。数据库将它们存储为分钟,所以我必须将它们存储为分钟。
-
有人可以向我解释一下我的问题是如何不清楚或没有用的,作为我收到的 -1 投票的标准吗?