简体   繁体   English

如何在 Swift 中获取日期的时区

[英]How To Get Timezone Of Date In Swift

I only want to get the current date object's timezone.我只想获取当前日期对象的时区。 The Date class in Swift does not have a way to do so. Swift 中的日期 class 没有办法这样做。 I have seen ways to convert from one timezone to another.我已经看到了从一个时区转换到另一个时区的方法。 But no way to do so extracting the timezone from a date object.但是没有办法从日期 object 中提取时区。

Why is that?这是为什么?

Is there a way to do so?有办法吗? Thoughts on how?怎么想的? Thanks谢谢

Dates don't have time zones.日期没有时区。 They're simply a point in time.它们只是一个时间点。

Eg, the moment in which Neil Armstrong stepped foot on the moon.例如,尼尔阿姆斯特朗踏上月球的那一刻。 You could describe that moment in UTC, or EST, or any other TZ (or even in other calendar systems), but they're all referring to the same moment .您可以用 UTC、EST 或任何其他 TZ(甚至其他日历系统)来描述那个时刻,但它们都指的是同一时刻 That would be modelled by a singular date object.这将由单一日期 object 建模。

By another analogy, an object has a width, but it doesn't know its unit.另一个类比,object 有一个宽度,但它不知道它的单位。 Its width can be measured in centimetres, inches, plank lengths or light years.它的宽度可以用厘米、英寸、木板长度或光年来衡量。 These are all abstractions we put on top of the concept of distance, and they all describe the same thing.这些都是我们放在距离概念之上的抽象,它们都描述了同一件事。

Internally, they're just a Double that counts the number of secs since the reference date.在内部,它们只是一个 Double,用于计算自参考日期以来的秒数。 Clearly, there's no timezone there.显然,那里没有时区。

Any time zones you see relating to dates are just an interpretive layer on top of that (eg print will call .description , which will format it to your local tz for your convenience.).您看到的与日期相关的任何时区都只是其上的一个解释层(例如 print 将调用.description ,为了您的方便,它会将其格式化为您当地的 tz。)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM