菜鸟教程小白 发表于 2022-12-11 19:55:32

ios - swift中以下时区之间有什么区别


                                            <p><pre><code>let timeZone = NSTimeZone.system.description
let localTimeZone = TimeZone.ReferenceType.local.description
let currentTimeZone = TimeZone.current.description
let defaultTimeZone = TimeZone.ReferenceType.default.description
let autoUpdateTimezon = TimeZone.autoupdatingCurrent.description

print (&#34;System Timezone \(timeZone)&#34;)
print (&#34;Local Timezone \(localTimeZone)&#34;)
print (&#34;Current Timezone \(currentTimeZone)&#34;)
print (&#34;Default Timezone \(defaultTimeZone)&#34;)
print (&#34;Auto updating Timezone \(autoUpdateTimezon)&#34;)
</code></pre>

<p><strong>输出</strong></p>

<blockquote>
<p>System Timezone Asia/Kolkata (current)</p>

<p>Local Timezone Asia/Kolkata (autoupdatingCurrent)</p>

<p>Current Timezone Asia/Kolkata (current)</p>

<p>Default Timezone Asia/Kolkata (current)</p>

<p>Auto updating Timezone Asia/Kolkata (autoupdatingCurrent)</p>
</blockquote>

<p>所以,我得到的所有输出都是相同的,所以这些时区之间有什么区别,在这种情况下我们应该使用哪个时区。 </p>

<p><strong>问题</strong> </p>

<p>我使用以下代码进行日期转换</p>

<pre><code>static func stringToString(strDate:String, fromFormat:String, toFormat:String)-&gt;String{
      let dateFormatter = DateFormatter()
      dateFormatter.timeZone = TimeZone.init(abbreviation: &#34;UTC&#34;) ?? TimeZone(identifier: &#34;UTC&#34;) ?? TimeZone.ReferenceType.default
      dateFormatter.dateFormat = fromFormat
      let currentDate = dateFormatter.date(from: strDate) ?? Date()
      dateFormatter.dateFormat =toFormat
      dateFormatter.timeZone = TimeZone.ReferenceType.default
      let currentDates = dateFormatter.string(from: currentDate)
      return currentDates
    }
</code></pre>

<blockquote>
<p>Scene : My app is crashing in qatar if user set timezone automatically and <code>off the 24 hours</code>, but in india there is no crash
(TimeZone.ReferenceType.local)</p>

<p>I have given next build with   <code>TimeZone.ReferenceType.default</code> and issue is solved </p>

<p>So, i cant understand what was the issue.</p>
</blockquote>

<p>崩溃报告</p>

<p> <a href="/image/46sYf.png" rel="noreferrer noopener nofollow"><img src="/image/46sYf.png" alt="enter image description here"/></a> </p>

<p>我正在崩溃的旧代码</p>

<p> <a href="/image/HEQa0.png" rel="noreferrer noopener nofollow"><img src="/image/HEQa0.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>Local</strong> -> 跟踪当前系统时区的对象。如果您想要一个始终反射(reflect)当前系统时区的对象,请使用此属性。从 ios 11 开始,本地类属性反射(reflect)当前系统时区,而之前它反射(reflect)的是默认时区。</p>

<p><strong>系统</strong> -> 系统当前使用的时区。如果您访问系统类属性,则其值由应用程序缓存,并且如果用户随后更改系统时区,则不会更新。为了让系统属性反射(reflect)新的时区,必须先调用<code>resetSystemTimeZone()</code>方法清除缓存值。</p>

<p><strong>默认</strong> -> 当前应用的默认时区。如果没有设置默认时区,则使用当前系统时区。如果无法确定当前系统时区,则使用 GMT 时区。应用程序使用默认时区进行日期和时间操作。您可以将其设置为使应用像在不同的时区一样运行。</p>

<p><strong>当前</strong> -> 系统当前使用的时区。</p>

<p><strong>autoupdatingCurrent</strong> -> 系统当前使用的时区,自动更新到用户当前的偏好。</p>

<p>来源-> <a href="https://developer.apple.com/documentation/foundation/nstimezone" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/foundation/nstimezone</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - swift中以下时区之间有什么区别,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49504976/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49504976/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift中以下时区之间有什么区别