• <tfoot id='7rNwZ'></tfoot>

        <bdo id='7rNwZ'></bdo><ul id='7rNwZ'></ul>
      <legend id='7rNwZ'><style id='7rNwZ'><dir id='7rNwZ'><q id='7rNwZ'></q></dir></style></legend>

        <small id='7rNwZ'></small><noframes id='7rNwZ'>

        <i id='7rNwZ'><tr id='7rNwZ'><dt id='7rNwZ'><q id='7rNwZ'><span id='7rNwZ'><b id='7rNwZ'><form id='7rNwZ'><ins id='7rNwZ'></ins><ul id='7rNwZ'></ul><sub id='7rNwZ'></sub></form><legend id='7rNwZ'></legend><bdo id='7rNwZ'><pre id='7rNwZ'><center id='7rNwZ'></center></pre></bdo></b><th id='7rNwZ'></th></span></q></dt></tr></i><div id='7rNwZ'><tfoot id='7rNwZ'></tfoot><dl id='7rNwZ'><fieldset id='7rNwZ'></fieldset></dl></div>

        在 c# 中的特定时区创建日期时间

        时间:2023-10-07
          <tbody id='5Pg55'></tbody>

        <small id='5Pg55'></small><noframes id='5Pg55'>

        • <bdo id='5Pg55'></bdo><ul id='5Pg55'></ul>
            <i id='5Pg55'><tr id='5Pg55'><dt id='5Pg55'><q id='5Pg55'><span id='5Pg55'><b id='5Pg55'><form id='5Pg55'><ins id='5Pg55'></ins><ul id='5Pg55'></ul><sub id='5Pg55'></sub></form><legend id='5Pg55'></legend><bdo id='5Pg55'><pre id='5Pg55'><center id='5Pg55'></center></pre></bdo></b><th id='5Pg55'></th></span></q></dt></tr></i><div id='5Pg55'><tfoot id='5Pg55'></tfoot><dl id='5Pg55'><fieldset id='5Pg55'></fieldset></dl></div>

                <tfoot id='5Pg55'></tfoot>
              1. <legend id='5Pg55'><style id='5Pg55'><dir id='5Pg55'><q id='5Pg55'></q></dir></style></legend>
                  本文介绍了在 c# 中的特定时区创建日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个单元测试来测试机器上时区何时更改的情况,因为它已被错误地设置然后更正.

                  I'm trying to create a unit test to test the case for when the timezone changes on a machine because it has been incorrectly set and then corrected.

                  在测试中,我需要能够在非本地时区创建 DateTime 对象,以确保运行测试的人无论身在何处都可以成功地执行此操作.

                  In the test I need to be able to create DateTime objects in a none local time zone to ensure that people running the test can do so successfully irrespective of where they are located.

                  从 DateTime 构造函数中可以看出,我可以将 TimeZone 设置为本地时区、UTC 时区或未指定.

                  From what I can see from the DateTime constructor I can set the TimeZone to be either the local timezone, the UTC timezone or not specified.

                  如何创建具有特定时区(如 PST)的 DateTime?

                  How do I create a DateTime with a specific timezone like PST?

                  推荐答案

                  Jon 的回答 谈到 TimeZone,但我建议使用 TimeZoneInfo 代替.

                  Jon's answer talks about TimeZone, but I'd suggest using TimeZoneInfo instead.

                  我个人喜欢尽可能保持 UTC 格式(至少在过去是这样;为未来存储 UTC 有潜在的问题),所以我建议这样的结构:

                  Personally I like keeping things in UTC where possible (at least for the past; storing UTC for the future has potential issues), so I'd suggest a structure like this:

                  public struct DateTimeWithZone
                  {
                      private readonly DateTime utcDateTime;
                      private readonly TimeZoneInfo timeZone;
                  
                      public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
                      {
                          var dateTimeUnspec = DateTime.SpecifyKind(dateTime, DateTimeKind.Unspecified);
                          utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTimeUnspec, timeZone); 
                          this.timeZone = timeZone;
                      }
                  
                      public DateTime UniversalTime { get { return utcDateTime; } }
                  
                      public TimeZoneInfo TimeZone { get { return timeZone; } }
                  
                      public DateTime LocalTime
                      { 
                          get 
                          { 
                              return TimeZoneInfo.ConvertTime(utcDateTime, timeZone); 
                          }
                      }        
                  }
                  

                  您可能希望将TimeZone"名称更改为TimeZoneInfo"以使事情更清楚 - 我自己更喜欢更简短的名称.

                  You may wish to change the "TimeZone" names to "TimeZoneInfo" to make things clearer - I prefer the briefer names myself.

                  这篇关于在 c# 中的特定时区创建日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 PST/CEST/UTC/等形式的时区解析 DateTime 下一篇:.NET 中 UTC 和 GMT 标准时间之间的差异

                  相关文章

                  <legend id='CRAQr'><style id='CRAQr'><dir id='CRAQr'><q id='CRAQr'></q></dir></style></legend>

                      <small id='CRAQr'></small><noframes id='CRAQr'>

                    1. <tfoot id='CRAQr'></tfoot>
                      <i id='CRAQr'><tr id='CRAQr'><dt id='CRAQr'><q id='CRAQr'><span id='CRAQr'><b id='CRAQr'><form id='CRAQr'><ins id='CRAQr'></ins><ul id='CRAQr'></ul><sub id='CRAQr'></sub></form><legend id='CRAQr'></legend><bdo id='CRAQr'><pre id='CRAQr'><center id='CRAQr'></center></pre></bdo></b><th id='CRAQr'></th></span></q></dt></tr></i><div id='CRAQr'><tfoot id='CRAQr'></tfoot><dl id='CRAQr'><fieldset id='CRAQr'></fieldset></dl></div>
                        <bdo id='CRAQr'></bdo><ul id='CRAQr'></ul>