NSDate 如何获取当前时区的当天分数/秒
我正在尝试获取当天的日分数,以便将太阳与钟盘对齐。我可以这样计算日分数:
float dayFraction = (int)seconds%(86400+1)/86400.0;
当转换为弧度时,上面的方程会全天候地为太阳设置动画
此代码计算太阳的位置
CGAffineTransform transformHours = [self calculateLabelRelateivePositionFromCurrentTimeWithOffsetClockwiseInDegrees:((int)[NSDate timeIntervalSinceReferenceDate])%(86400+1)/86400.0*360 ];
问题是我需要获取当前时区的日分数。下面的等式始终使用 GMT 时间。有没有办法轻松获取时间戳或当前时区偏移量以应用于时间戳 如果也拥有此帐户夏令时时间,那就太好了!?
((int)[NSDate timeIntervalSinceReferenceDate])%(86400+1)/86400.0
更新: 感谢您的建议,我最终使用了以下代码:
int gmtOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
int daylightOffset = [[NSTimeZone localTimeZone] daylightSavingTimeOffset];
int daySecond = [NSDate timeIntervalSinceReferenceDate]+gmtOffset+daylightOffset)%
(86400+1)
I'm trying to get a day fraction for the current day to align a sun across a clock dial. I can calculate the day fraction like this:
float dayFraction = (int)seconds%(86400+1)/86400.0;
The equation above when converted to radians would continuously animate the sun around the clock
This code calculates where to position the sun
CGAffineTransform transformHours = [self calculateLabelRelateivePositionFromCurrentTimeWithOffsetClockwiseInDegrees:((int)[NSDate timeIntervalSinceReferenceDate])%(86400+1)/86400.0*360 ];
The problem is that I need to get the day fraction for the current time zone. The equation below always uses GMT time. Is there a way to easily get timestamp or the current time zone offset to apply to the timestamp It would be great to have this account for daylight savings time too!?
((int)[NSDate timeIntervalSinceReferenceDate])%(86400+1)/86400.0
Update:
Thank you for the suggestion, I ended up using the following code:
int gmtOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
int daylightOffset = [[NSTimeZone localTimeZone] daylightSavingTimeOffset];
int daySecond = [NSDate timeIntervalSinceReferenceDate]+gmtOffset+daylightOffset)%
(86400+1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)