I am trying to convert a string to date in a separate time zone from the current system time zone. I have set the time zone of the dateformatter to this timezone. But the NSDate I get is always in my current time zone.
For instance, my current time zone is PDT. I have set the DateFormatter's time zone to CST. I have a string with the time in CST. But when I use date formatter to convert this string to date, I get the date in PDT, whereas I want it in CST.
Can someone please help.
Thanks.
NSDate stores only the seconds since 00:00:00 01 Jan. 2001. The description of NSDate will use your current time zone (if try to debug with NSLog for example).
You must get the default time zone with
[NSTimeZone defaultTimeZone]
and then calculate the offset by using
-(NSTimeInterval)secondsFromGMT
Related
I have a table storing a time value say "11:00 AM". This is what the client's would want the progress to run every day. The client also has a timezone info table letting me know which time zone its office is located. So "11:00 AM" in Eastern time zone is very different from "11:00 AM" in Pacific.
Then I have a program that checks whether the current time is what the client's desired runtime. This process runs in Eastern time zone always.
I am trying to figure out an easy way to compare whether my current time is actually the client's desired run time. What I am doing now is something like this:
Get the current DateTimeOffset of my environment. This will produce something like '2021-03-15 16:02:22.3112948 -04:00'
Then get the client's run time, which is "11:00 AM" or whatever the value, as well as client's timezone offset by using the sys.time_zone_info table. This give me the actual offset value, taking DST into consideration so that I don't have to calculate whether today is in DST or not.
I then construct a Datetimeoffset value from string by using current date value + client's run time value + the offset value. Then convert this into Eastern time zone using at time zone 'Eastern Standard Time'
By now, I have two values both in Eastern time zone and I can compare them. First question -- do you see any potential issue with my logic? Secondly, is there a better/easier way to do this?
Thanks!
I want to present a date to the user in the dates own timezone. So without it being converted to the users timezone.
Basically what happens: I retrieve a string '2015-04-01T15:35:00-04:00' from the backend, and I convert it to a NSDate. Later on I want to show the user the time: 15:35.
But NSDateFormatter converts the date to my system time zone (+02:00) which results in showing 21:35.
I've searched all over the internet to find out I can skip this convert, but I can't find anything.
What do I miss?
Help is really appreciated.
You will have to save the original timezone in addition to the NSDate representation and use it when you want to present the time in the original timezone.
NSDate keeps the date/time relative to GMT (UTC).
I'm doing a calculation to find the NSTimeInterval between a known futureDate (which is in GMT/UTC) and the current time (and timezone) where the device currently resides.
NSTimeInterval approachInterval = [futureDate timeIntervalSinceNow];
I know that NSDate does not store any timeZone specific data, this rather being something that is calculated when the date needs displaying on the device.
My question is that from my initial tests (I'm currently in GMT+0001) it seems that the NSTimeInterval returned does not account for my local timezone. Do I have to manually calculate the timezone offset and remove that from my interval, and what would be the best way of doing that given that I can't supply an NSDate with a correct timezone (using timeIntervalSinceDate: for example)
NSDate represents a single point in time. It does not have (and does not need!!) a time zone. So "a known futureDate (which is in GMT/UTC)" or "I can't supply an NSDate with a correct timezone" does not make sense.
[futureDate timeIntervalSinceNow] returns the time interval between futureDate and the
current date and time and is completely independent of any time zones.
If you do not get the expected result, then perhaps your calculation of futureDate was wrong.
If part of the data that RestKit gets from my server is a timestamp formatted as 2013-05-27 20:32:26 UTC, and later I want to find out the difference between the current time and this date, I do
NSTimeInterval difference = [[NSDate date] timeIntervalSinceDate:dateFromRestKit];
But this always seems to be off for me by 1 hour, and I'm guessing this is because I'm on BST, which is UTC+1. So I think that RestKit is ignoring the timezone specified in the date string, and is instead parsing the date using the current system timezone.
I understand that there may be some technical reason it ignores the time zone, but I found all this time stuff really difficult to get my ahead around so I'm not sure.
How can I work out the correct difference between the times?
Even if your timestamp contains the UTC format , try to specify the timezone for it again.
If you want the resulting difference of two date timezone in 'UTC' then , convert the other one in UTC format and then find the time Interval.
Do let me know , if any query. Or if possible , post your code , to get more clear about your question. (:
I have to parse strings like 2012-06-06T18:00:00 or 2012-06-06 without any timezone information. When I parse this the date formatter uses the local timezone and takes DST into account.
But what the date actually means is that this event occurs on 2012-06-06 not depending on the timezone, but on 2012-06-06T00:00:00 in every timezone.
Should I store the year/month/day values separately?
The same time in different time zones will correspond to different absolute times (which are represented by NSDate). So one NSDate object cannot represent what you are trying to represent. You will need one NSDate per time zone you are interested in.
You can create an NSDateComponent instance with your string. Then use NSCalender with the locale (time zone) you want to transform those date components into an NSDate (a point in time).