【发布时间】:2021-04-14 12:25:42
【问题描述】:
我创建了一个小应用程序来测试使用 UTC 偏移量的存储时间。我不明白结果。如果我在今天 14:01 运行程序,我会得到:
这个:
InsertPunch(new Employee { Id = 10, TimeZoneId = "Central Standard Time" });
在数据库中产生这个:
2021-01-08 13:01:06.3594141 -05:00
这个:
InsertPunch(new Employee { Id = 12, TimeZoneId = "Eastern Standard Time" });
在数据库中产生这个:
2021-01-08 14:01:07.5587251 -05:00
时间是正确的;第一个确实是 CT,第二个确实是 ET。但是为什么两者的偏移量都是-5? CT的偏移量不应该是-6吗?我假设我们可以看到这样的时间:
2021-01-08 14:01:07.5587251 -05:00
...我们知道 UTC 时间是 19:01 (14:01 + 5:00)。这是正确的。但是 CT 的结果是不正确的:13:01 + 5:00 = 18:01,而当前 UTC 时间实际上是 19:01。
我理解错了吗?还是我做错了什么?
static void Main(string[] args)
{
InsertPunch(new Employee { Id = 10, TimeZoneId = "Central Standard Time" });
InsertPunch(new Employee { Id = 12, TimeZoneId = "Eastern Standard Time" });
Console.WriteLine("Press any key to end.");
Console.ReadKey();
}
private static void InsertPunch(Employee employee)
{
var punchTimeUtc = DateTime.UtcNow; // Need timestamp in UTC to properly convert to employee's time zone.
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(employee.TimeZoneId); // Reference the employee's time zone.
var punchTimeInUsersZone = TimeZoneInfo.ConvertTimeFromUtc(punchTimeUtc, timeZoneInfo);
var punch = new Punch
{
EmployeeId = employee.Id,
PunchTime = punchTimeInUsersZone
};
string sql = "INSERT INTO [time].[Punch] ([EmployeeId],[PunchTime]) VALUES (@EmployeeId, @PunchTime)";
using (IDbConnection db = new SqlConnection(_connectionString))
{
db.Execute(sql, new { punch.EmployeeId, punch.PunchTime });
}
}
【问题讨论】:
-
@Larnu 为什么要删除 SQL Server 标记?
-
关于SQL Server的问题呢? (请注意,说代码具有 SQL Server 后端并不意味着它与 SQL Server 相关)。
-
结果来自 SQL Server 中的最终结果。我认为如果列的数据类型、SQL Server 设置等很重要,这可能是相关的。
-
您正在存储 UTC。您看到的偏移量来自您用来查看它的任何内容,并将其转换为您的时区。
-
问题是你插入了错误的数据;这不是 SQL Server 的错。那是C#代码的错。 SQL Server 不会更改数据(除非存在您没有告诉我们的触发器)。
标签: c# time timezone timezone-offset