Check isDaylightSavingTime for - ios

The Using Time Zones Guide
shows how to check if daylight saving time (DST) is in action for the current date and how to get the next date when daylight saving time changes. With calls to:
isDaylightSavingTime
daylightSavingTimeOffset
nextDaylightSavingTime
E.g.
Bool isDST = [[NSTimeZone systemTimeZone] isDaylightSavingTime];
But I need to find out if a future date is DST or not so that when users scroll to a distant and far off date, they can see my graph plotted with the time axis labelled correctly (GMT or BST).
The problem is that isDayLightSavingTime seems to take two parameters, one passed to it explicitly (NSTimeZone) and the other implicitly, namely the current system time.

You just need to call isDaylightSavingTimeForDate, passing in the relevant NSDate.
It's not that isDaylightSavingTime itself has two parameters - it's that it calls isDaylightSavingTimeForDate with the current date. From the docs for isDaylightSavingTime:
This method invokes isDaylightSavingTimeForDate: with the current date as the argument.
So just skip the "convenience" method and call isDaylightSavingTimeForDate specifying the date you want to check. (I'd give some sample code, but the chances of me getting Objective C syntax correct are very slim...)

Related

iOS : Timezone Issue on Local timezone of iPhone device

I have been working on an application that takes dates from server in a certain format as given below.
"2015-02-03 00:00:00"
I want to show them with different UI format using NSDateFormatter
but as i change the timezone it show previous day or sometimes next day as i changing timezone. One important thing that server side date can be in any of timezone. So, we don't know what kind of timezone date has.
I need help to show this to the date as it is on server not change due to local timezone.
Thanks.
The simplest approach would be to parse it as if it were UTC - making it always valid, and without ever needing DST adjustments - and then format it in UTC as well. In other words, set the time zone of both the formatter and parser to UTC. For types which need a time zone, that's the simplest way of faking "There isn't a time zone here, just treat it all as local to an unspecified time zone."

How do I store the time part of NSDateComponents?

I want to store times type value (but not the date) in Core Data. Which data type should I use?
Possible options:
Three integer attributes for hour, minutes, seconds.
One string attribute for "HH:MM:SS".
One integer attribute for the number of seconds since midnight.
The best representation depends on what type of queries involving the time you want to
execute. The first option is better if you want to search for a given hour, minute or second. The other options are better if you have to search for time intervals.
My approach would probably be to convert it to an HHMMSS format, either as an integer or a string. NSDate isn't appropriate because it is stored as a interval since an epoch (seconds since 1970)
The date portion of an NSDate is inherently part of time. NSDate is a NSTimeInterval number of seconds positive or negative since the reference date. With a locale and a timezone you can get an appropriate localized display string on the fly.
In most cases it makes far more sense to store the number behind the NSDate returned by timeIntervalSinceReferenceDate
It's small and portable and correct.
Then recreate an NSDate with dateWithTimeIntervalSinceReferenceDate:
Then use an NSDateFormatter with NSLocale and NSTimeZone to get an appropriate value for display to users.
It's an unwieldy and cumbersome set of tools that actually help you do it right.
The good news is all the effort you expend in doing this pays off in making it easier next time and giving you code that will do the right thing and have a backing value that is actually really easy to store and manipulate.
Additionally by using the API correctly you will lose no precision and have more possibilities open to you.
Trying to do it with format strings alone seems easy and simple but is the road to bad code.

Set cxDateEdit Time

Is there a way to set cxDateEdit's time to 0.00.00 ? Now,it always fetches the current time.
I want the time to 00. Thought I do not need time, even if I remove it, my date still takes time into the account.
You can turn off the display of the time by setting the Properties in the Object inspector. More specifically, Expand the Properties property and set Kind = ckDate
This seems to work:
DateUtils.DateOf(cxDateEdit1.Date);
The DateOf function as described in the reference:
Strips the time portion from a TDateTime value. Call DateOf to convert
a TDateTime value to a TDateTime value that includes only the date
information (sets the time portion to 0, which means midnight).

XQuery - determine whether a given UTC time falls in daylight saving time in a given timezone

In XQuery, how can I determine whether a UTC time falls within daylight saving time in a region?
Something like:
declare function local:IsDaylightSavingTime($utcDateTime as xs:utcDateTime, timeZone xs:dayTimeDuration) as xs:boolean {
...
}
This is not possible.
How would you expect a time zone to be represented by an xs:dayTimeDuration? That would assume a time zone is the same thing as a time zone offset, which it is not. Please read the timezone tag wiki.
Besides, any program that is capable of performing this function needs some type of time zone database. While it is conceivable that a particular XQuery implementation decided to incorporate a time zone database, that certainly wouldn't be in the XQuery language itself.

How to get the timezone name from numeric timezone value in Android

I am basically trying to read a .vcs file in Android. It has timezone value in the below format:
TZ:+05:30
Now I want to get the timezone name corresponding to this value. Means in this case it would be Kolkata(India).
Is there any code to achieve this in android?
TimeZone tz = TimeZone.getTimeZone("GMT-06:00");
String tzarr[] = TimeZone.getAvailableIDs(tz.getRawOffset());
for(int i=0;i<tzarr.length;i++)
(I'm assuming you can parse the text into an offset easily enough.)
In general, you can't. Something like "+05:30" just represents an offset from UTC at one particular time. It doesn't express how the offset changes within the time zone across the year (or across history). For example, for some of the time the Europe/London time zone has the same offset as Africa/Casablanca - but not always.
Assuming this is associated with a specific date/time, you could use TimeZone.getAvailableIDs, iterate over all the time zones, check what the offset from UTC is at that particular instant (using TimeZone.getOffset(long)) and see which time zones have the right offset at the right time. There could be many such zones though.
If you don't have a specific date/time, it's even more ambiguous. You can use getRawOffset and getDSTSavings to see whether the target offset is either the standard or DST offset for any particular zone - although note that these calls assume that for a particular time zone, the DST offset and standard offset remain the same across history (which isn't always true).

Resources