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

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

  • <tfoot id='wq1Uf'></tfoot>
    • <bdo id='wq1Uf'></bdo><ul id='wq1Uf'></ul>
    <legend id='wq1Uf'><style id='wq1Uf'><dir id='wq1Uf'><q id='wq1Uf'></q></dir></style></legend>

        iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法

        时间:2023-05-19

          <tfoot id='HKP3c'></tfoot>
              • <bdo id='HKP3c'></bdo><ul id='HKP3c'></ul>

              • <small id='HKP3c'></small><noframes id='HKP3c'>

                  <tbody id='HKP3c'></tbody>
                • <legend id='HKP3c'><style id='HKP3c'><dir id='HKP3c'><q id='HKP3c'></q></dir></style></legend>
                  <i id='HKP3c'><tr id='HKP3c'><dt id='HKP3c'><q id='HKP3c'><span id='HKP3c'><b id='HKP3c'><form id='HKP3c'><ins id='HKP3c'></ins><ul id='HKP3c'></ul><sub id='HKP3c'></sub></form><legend id='HKP3c'></legend><bdo id='HKP3c'><pre id='HKP3c'><center id='HKP3c'></center></pre></bdo></b><th id='HKP3c'></th></span></q></dt></tr></i><div id='HKP3c'><tfoot id='HKP3c'></tfoot><dl id='HKP3c'><fieldset id='HKP3c'></fieldset></dl></div>
                • 本文介绍了iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在寻找正确的方法来获取给定地点/时区的实际日期和时间,并能够将其与同一地点的给定日期/时间进行比较.p>

                  假设,例如,巴黎,即 GMT +1.就像我们现在一样,由于夏令时,巴黎也可以是 GMT+2,但据我所知,它是例外的一部分,因此必须将其考虑到答案中,而不是作为一般参数.这里重要的词是给定的地方".

                  所以,如果我想知道在澳大利亚悉尼或法国巴黎的日期和时间,并将该日期放入 NSDate,以便能够将其与另一个代表同一日期/时间的 NSDate 进行比较地方,我该怎么办?

                  我已经阅读了一页又一页的带有评论的问题和答案,即使是在接受的答案上,经验丰富的用户也会说:不是一个好的答案 -1,错误,不是正确的做法,绝对错误,...

                  那么,你知道正确的方法吗?

                  在一个完美的世界中,即使用户的手机不在正确的时间和/或日期和/或时区,或者任何可以接近完美而无需连接的任何事物,该日期/时间都是绝对的日期/时间服务器.

                  解决方案

                  在头疼不已,开始了解 NSDate 是什么之后,我想到了这样的解决方案.您如何看待这种做法?

                  //现在,一个代表世界各地现在的绝对日期和时间,可以用来玩NSDate *now = [NSDate 日期];//世界上特定地点的特定日历NSCalendar* parisCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];[parisCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];//现在从巴黎看到的组件NSDateComponents* componentsNowInParis = [parisCalendar 组件:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];//欺骗现在从巴黎看到的组件"的副本以强制 15:00:00,在巴黎NSDateComponents* componentsInParisAt15 = [[componentsNowInParis copy] autorelease];[componentsInParisAt15 setHour:15];[componentsInParisAt15 setMinute:0];[componentsInParisAt15 setSecond:0];//获取一个通用日期参考,代表从巴黎看到的 15:00:00,或从 GMT+4 看到的 19:00:00NSDate* dateAt15 = [parisCalendar dateFromComponents:componentsInParisAt15];//我们现在有两个可以相互比较的通用日期//如果现在"是 16:00:00,那么这些日期将显示世界各地 60 分钟的差异NSLog(@"%@", 现在);NSLog(@"%@", dateAt15);

                  一些参考:http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW4

                  但是,据我所知和测试,那一天/时间不可能是绝对的.它基于 iPhone 日期/时间/时区,这可能是错误的.

                  I'm searching the correct way to get the actual date and time for a given place / timezone, and being able to compare it to a given date/time for that same place.

                  Let's say, for the example, Paris, that is GMT +1. As we now, Paris can also be GMT+2 because of daylight saving, but it's part of an exception as far as I know so it must be taken in account into the answer, but not taken as a general param. The important words here are "given place".

                  So, if I want to know what date and time it is at Sidney Australia, or Paris France, and get that date into an NSDate for being able to compare it with another NSDate that would represent another date/time in the same place, how may I do ?

                  I've read pages and pages of questions and answers with comments, even on accepted answer, that says from experienced users : not a good answer -1, wrong, not the correct way of doing this, absolutely wrong, ...

                  So, do you know the correct real way to do that ?

                  In a perfect world, that date/time would be absolute even if the user's phone is not at the good time and/or date and/or timezone, or anything that can be near that perfection without needing for that to connect to a date/time server.

                  解决方案

                  After a big headache and starting to understand what NSDate is, I imagined that kind of solution. What do you think about that way of doing ?

                  // Now, an absolute date and time that represent now all around the world, that is made to play with
                  NSDate *now = [NSDate date];
                  
                  // A specific calendar for a specific place in the world
                  NSCalendar* parisCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
                  [parisCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];
                  
                  // Now components seen from Paris
                  NSDateComponents* componentsNowInParis = [parisCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
                  
                  // Tricking a copy of "Now components seen from Paris" to force 15:00:00, in Paris
                  NSDateComponents* componentsInParisAt15 = [[componentsNowInParis copy] autorelease];
                  [componentsInParisAt15 setHour:15];
                  [componentsInParisAt15 setMinute:0];
                  [componentsInParisAt15 setSecond:0];
                  
                  // Getting an universal date reference that represent what could be 15:00:00 seen from paris, Or 19:00:00 from GMT+4
                  NSDate* dateAt15 = [parisCalendar dateFromComponents:componentsInParisAt15];
                  
                  // We now have two universal dates that can be compared each other
                  // If "now" is 16:00:00, those date will show a 60 minutes difference all around the world
                  NSLog(@"%@", now);
                  NSLog(@"%@", dateAt15);
                  

                  Some reference : http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtTimeZones.html#//apple_ref/doc/uid/20000185-SW4

                  But, as far as I know and tested, that day/time cannot be really absolute. It is based on the iPhone date/time/timezone, that can be wrong.

                  这篇关于iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 iPhone 模拟器中更改时间和时区? 下一篇:从 iPhone 获取时区/国家

                  相关文章

                  <tfoot id='H1tlp'></tfoot>

                • <small id='H1tlp'></small><noframes id='H1tlp'>

                    <legend id='H1tlp'><style id='H1tlp'><dir id='H1tlp'><q id='H1tlp'></q></dir></style></legend>
                    • <bdo id='H1tlp'></bdo><ul id='H1tlp'></ul>

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