c# - 如何让 .NET WinForms 组件使用 SET 时区而不是客户端时区?

标签 c# .net winforms web-services activereports

在我发现另一个论坛网站上有我需要回答的完全相同的问题(但没有答案)之前,我并不期待描述这个问题。所以感谢this guy这是我的问题...

We have a large Windows .Net application (winform executable) that gets installed on the client's desktop. This application calls web services on a server in a different timezone. Virtually all date oriented components detect the timezone difference and automatically adjust the datetime values (returned in dataSets generated by SQL queries), which is generally desirable in most applications but causing problems with accounting related applications that are "date" not "datetime" oriented. We are only interested in the "date" portion. However, a date of 1/1/2003 GMT-5 is automatically converted to 12/31/2002 11:00 GMT-6 on the client. Rather than going through all of the code and extracting the UniversalTime to get back to 1/1/2003 for visual purposes we would like to simply "fake" the timezone for the client-side executable by making it think it's in the same timezone as the server.

Question: Can we set the TimeZone programmatically for the currently running instance only, rather than the global setting?

我真的没有太多要补充的,因为这是我们的确切问题。在我们的例子中,我们有 ActiveReports 将远程 SQL 数据提取到数据集中,然后将报告绑定(bind)到数据集中。因此,例如,生日是错误的,因为我们正在为他们存储日期,而 X 小时正在减去西部时区的日期?所以生日相差负 1。

有什么想法吗?

谢谢!

赛斯

最佳答案

我们解决这个问题的方法是序列化日期的字符串表示。

字符串日期的属性实现执行与实际日期属性的适当转换。

例如:

    [NonSerialized]
    public DateTime MyDate { get; set; }

    [System.Xml.Serialization.XmlAttribute("MyDate")]
    public string MyDateString
    {
        get
        {
            if (MyDate.Equals(DateTime.MinValue))
            {
                return string.Empty;
            }
            else
            {
                return MyDate.ToShortDateString();
            }
        }
        set
        {
            DateTime dtTemp = DateTime.MinValue;
            if (DateTime.TryParse(value, out dtTemp))
            {
                MyDate = dtTemp;
            }
            else
            {
                MyDate = DateTime.MinValue;
            }
        }
    }

关于c# - 如何让 .NET WinForms 组件使用 SET 时区而不是客户端时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8929494/

相关文章:

c# - 在 WinForms 中设置按钮填充父级的所有宽度和顶部

c# - 如何以编程方式 (C#) 从 POP3 邮件中获取附件?

c# - 我怎样才能删除这个网格中多余的瓷砖?

c# - LINQ 递归查询返回分层组集

c# - 如何在 ms 图表中旋转特定标签?

c# - LINQ - 必填字段,不关心是否为空

c# - 使用 UAC 部署 C# 应用程序

c# - 在 C# 中将 XML 反序列化为类 obj

c# - 即使使用 CriticalFinalizerObject,也不会在未处理的异常后调用终结器

C# 右键单击​​ TreeView 节点