Problem in getting DST time? - blackberry

I am trying to get the time of other GMT value by using
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone(gmt));
but how can i get the DST time at that time zone.

The TimeZone class provides a getDSTSavings() method for a specific TimeZone Object. (JavaDoc: "Returns the amount of time to be added to local standard time to get local wall clock time.")
The Calendar interface provides two getOffset() methods, which let you find out the offset from UTC. (JavaDoc: "Returns the offset of this time zone from UTC at the specified date. If Daylight Saving Time is in effect at the specified date, the offset value is adjusted with the amount of daylight saving. ")
please see this piece of code to grok the complicated ways of java time:
#Test
public void testDST() {
final TimeZone met = TimeZone.getTimeZone("MET");
Calendar gc = new GregorianCalendar(met);
final long timeInMillis = gc.getTimeInMillis();
final long gmtTime= timeInMillis-(gc.getTimeZone().getOffset(timeInMillis));
final Date gmtDate = new Date(gmtTime);
System.out.printf("%-40s: %tc\n%-40s: %tc\n%-40s: %tc\n%-40s: %d\n%-40s: %d",
"new Date() (local timezone)",new Date(),
"UTC", gmtDate ,
"now from Calendar with TC GMT+02:00",gc,
"zoneoffset",gc.get(Calendar.ZONE_OFFSET),
"dst savings",met.getDSTSavings());
}
You can also define your own SimpleTimeZone and provide custom DST rules, however, i have not found out how to get this information from the predefined TimeZones.
You should also be aware, that if TimeZone.getTimeZone(TZName) does not find the specified timezone, it does not throw an exception, but it just uses GMT, which can cause major misunderstandings.
You can find all this information (and a lot more) in javadoc for Calendar, TimeZone, Date, etc.

There are few methods available in java.util.TimeZone to get Daylight Saving Time. Please check out the BlackBerry Java Docs page.

Related

What is Difference among following time zones in swift

let timeZone = NSTimeZone.system.description
let localTimeZone = TimeZone.ReferenceType.local.description
let currentTimeZone = TimeZone.current.description
let defaultTimeZone = TimeZone.ReferenceType.default.description
let autoUpdateTimezon = TimeZone.autoupdatingCurrent.description
print ("System Timezone \(timeZone)")
print ("Local Timezone \(localTimeZone)")
print ("Current Timezone \(currentTimeZone)")
print ("Default Timezone \(defaultTimeZone)")
print ("Auto updating Timezone \(autoUpdateTimezon)")
OUTPUT
System Timezone Asia/Kolkata (current)
Local Timezone Asia/Kolkata (autoupdatingCurrent)
Current Timezone Asia/Kolkata (current)
Default Timezone Asia/Kolkata (current)
Auto updating Timezone Asia/Kolkata (autoupdatingCurrent)
So, i get all the output are same so whats the difference among these timezone and which timezone we should use in which case.
Problem
I used following to code for the date conversion
static func stringToString(strDate:String, fromFormat:String, toFormat:String)->String{
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") ?? TimeZone(identifier: "UTC") ?? TimeZone.ReferenceType.default
dateFormatter.dateFormat = fromFormat
let currentDate = dateFormatter.date(from: strDate) ?? Date()
dateFormatter.dateFormat = toFormat
dateFormatter.timeZone = TimeZone.ReferenceType.default
let currentDates = dateFormatter.string(from: currentDate)
return currentDates
}
Scene : My app is crashing in qatar if user set timezone automatically and off the 24 hours, but in india there is no crash
(TimeZone.ReferenceType.local)
I have given next build with TimeZone.ReferenceType.default and issue is solved
So, i cant understand what was the issue.
Crash Report
Old Code in which i am getting crash
Note that TimeZone.ReferenceType is basically NSTimeZone.
If you look at the docs for TimeZone and NSTimeZone you'll find out very quickly.
From the NSTimeZone:
The system class property returns the time zone currently used by the system, if known. This value is cached once the property is accessed and doesn't reflect any system time zone changes until you call the resetSystemTimeZone() method. The local class property returns an autoupdating proxy object that always returns the current time zone used by the system.
To summarise, system is cached so won't change when the user changes their time zone. You have to call resetSystemTimeZone to update it. local on the other hand automatically updates when the user changes their time zone.
The same thing is true for TimeZone:
TimeZone provides two static functions to get time zone values: current and autoupdatingCurrent. The autoupdatingCurrent time zone automatically tracks updates made by the user.
current corresponds to system and autoupdatingCurrent corresponds to local.
Local -> An object that tracks the current system time zone. Use this property when you want an object that always reflects the current system time zone. from ios 11, the local class property reflects the current system time zone, whereas previously it reflected the default time zone.
System -> The time zone currently used by the system. If you access the system class property, its value is cached by the app and doesn't update if the user subsequently changes the system time zone. In order for the system property to reflect the new time zone, you must first call the resetSystemTimeZone() method to clear the cached value.
Default -> The default time zone for the current app.If no default time zone has been set, the current system time zone is used. If the current system time zone cannot be determined, the GMT time zone is used instead.The default time zone is used by the app for date and time operations. You can set it to cause the app to run as if it were in a different time zone.
Current -> The time zone currently used by the system.
autoupdatingCurrent -> The time zone currently used by the system, automatically updating to the user’s current preference.
Source -> https://developer.apple.com/documentation/foundation/nstimezone

Date(timeIntervalSince1970:) returns 2 different results

I am getting some results from a weather API and one of that is date in epoch time stamp.
I found that converting with Date(timeIntervalSince1970:) I get the right date
I am using the specific number --> 1501452000 and I get 2 results on Playground
1) Jul 31,2017,12:00AM. -- when --> let date = Date(timeIntervalSince1970: 1501452000)
2) 2017-07-30 22:00:00 +0000 when --> print(date)
API results are :
"time_epoch": 1501452000,
"time": "2017-07-30 23:00",
By checking the rest of my results they are matching with the rest of the API results....... but when I convert 1501452000 -> to date I don't get the correct Hour 23:00 but 22:00 !
Any idea what is happening ?
is it wrong the API( I don't think so ) or the way I am converting it?
Thanks a lot
The timeIntervalSince1970 initializer sets up the time in the UTC timezone, while your API might be sending dates in GMT. When you are using print(data), you have different results, because if you are not using a DateFormatter to generate the String format of the Date object, it uses your devices current settings when formatting the Date object.
A Date object represents an absolute point in time, but when you are printing it with a DateFormatter, it gets converted into a location/time zone specific, relative representation. You just have to set up your DateFormatter to match the time zone settings of your API and you will see the dates correctly printed.
This issue happens on daylight saving times. Is your country changing daylight saving on this exact date?
let date = Date(timeIntervalSince1970: 1501452000) in Playgrounds should give you the time in your system's timezone when you see it on the right hand side.
When you print it and see 2017-07-30 22:00:00 +0000- this is the same timestamp in GMT
Is the API showing a particular time zone? It looks like GMT+1

How can I add timezone to Esper queries?

I am using Esper & I need to filter events by their timestamp. The events come from an external source.
The challenge is that the cutoff instant is at a different timezone than the events` timestamp, e.g. the cutoff instant is at 3:30 CET (e.g. Prague time) while the timestamp field of the event is at UTC.
This poses a problem when the timezone shifts to Daylight Savings Time, because the cutoff instant needs to be modified in the query. E.g. in this case, if the cutoff instant is 3:30 CET, during winter time it would be on 2:30 UTC and during DST it would be on 1:30 UTC. It means that I have to change the query when the time shifts into and out of DST.
This is the current query:
SELECT *
FROM my_table
WHERE timestamp_field.after( timestamp.withtime(2,30,0,0) )
I would like to have a robust solution that will save me the hassle of changing the cutoff timestamp queries every few months. Can I add the timezone to the query statement itself? Is there any other solution?
It may help to add an event property to the event that represents UTC time i.e. normalize the event timestamp to UTC and use the normalized property instead.
The query could also use a variable instead of the hardcoded numbers. Another option would perhaps be changing Esper source to take in a timezone for some func.s
After struggling unsuccessfully with trying ot do it in the WHERE caluse or using a Pattern, I managed to solve the issue using a [Single-Row Function plugin][1].
I pass the plugin function the cutoff hour, timezone & event timezone and compute the cutoff hour in the event's timezone.
My query changed to:
SELECT *
FROM my_table
WHERE timestamp_field.after( timestamp.withtime(
eventTZHour(2, 'UTC', 'Europe/Prague'), 30, 0, 0) )
I added the Java implementation in a class:
public class EsperPlugins {
public int eventTZHour(int hour, String eventTZ, String cutoffTZ) {
// return tz calculations
}
}
and finally registered the plugin in esper.cfg.xml:
<esper-configuration>
<plugin-singlerow-function name="eventTZHour"
function-class="EsperPlugins"
function-method="eventTZHour"/>
</esper-configuration>
[1]: http://www.espertech.com/esper/release-5.2.0/esper-reference/html/extension.html#custom-singlerow-function from esper's docs

get time of Different Timezone

My task: I am going to run a contest world wide at my website. A problem setter will set problems from a specific area of the world setting a time and date of starting time of the contest. I have to show that time correctly all over the world so the the contest starts at a time everywhere of the world.
My Idea : I planed to get the time from the problem setter of his time zone using server site language like php time(), & will store to database converting to timezone= zero (0). And who are going to attend the contest I'll just add hour(s) of that time zone with my database time.
Need help: I have no Idea how to convert that timestamps to timezone 'zero', even how can I get the ±hour(s) of current timezone?
Thank you...
Step 1:
Let the user choose his timezone. You could fill a dropdown with values from this site: http://php.net/manual/en/timezones.php
Step 2:
Convert the timezone to servertime
$timezone_client = new DateTimeZone('America/Denver');
$timezone_server = new DateTimeZone('Pacific/Nauru');
$datetime = new DateTime('2013-01-25 12:00:00', timezone_client);
$datetime->setTimezone($timezone_server);
echo $datetime->format('Y-m-d H:i:s');
Timezone 0 = "UTC" (sometimes called GMT)
Your system / language will have a Timezone class, which provides difference to GMT/UTC

iOS/iCal switches times over daylight savings with recurring event from ICS file

I have an event that is set up using UTC and it recurs over daylight savings time. After the daylight savings time switch, the time is moved an hour. UTC does not use DST, so the time should stay the same. I can add an event after DST with the EXACT same UTC time value and the calendar shows the correct time. I've posted my ICS File below with the recurring event and the one that happens after. Has anyone run into this? Any suggested fixes or changes?
["BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//sethhill v1.0//NONSGML //EN\nCALSCALE:GREGORIAN\nMETHOD:PUBLISH\nBEGIN:VEVENT\nDTSTAMP:20120725T222602Z\nDTSTART:20120724T180000Z\nDTEND:20120724T183000Z\nRRULE:FREQ=WEEKLY;UNTIL=20130707T180000Z\nSUMMARY:Class 4 - NODST\nBEGIN:VALARM\nTRIGGER:-PT45M\nACTION:DISPLAY\nDESCRIPTION:NODST\nEND:VALARM\nEND:VEVENT\nBEGIN:VEVENT\nDTSTAMP:20120725T222602Z\nDTSTART:20121120T190000Z\nDTEND:20121120T193000Z\nRRULE:FREQ=WEEKLY;UNTIL=20130707T180000Z\nSUMMARY:Class 4 - TEST1DST\nBEGIN:VALARM\nTRIGGER:-PT45M\nACTION:DISPLAY\nDESCRIPTION:TEST1DST\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR"]
I was wrong about how DST and UTC work. I was thinking that having a set UTC time meant that the calendar would read it as a set time and display the correct local time auto-magically. In case anyone else is a little confused, think of it like this: MST is UTC-07:00 MDT (DST) is UTC-06:00. If you have a calendar event that is set to repeat throughout the year and it is 18:00 UTC, it will be at 12:00PM MDT or 11:00AM MST. For an ICS file you can either define a STANDARD timezone and a DAYLIGHT timezone, or break the event into multiple parts, with the correct UTC time for each.
I hope this clears things up for someone else.
public static DateTime GetDSTStart(int year, TimeZoneInfo timeZone)
{
DateTime finder = new DateTime(year,3,8);
finder = finder.AddHours(6);
for (int i = 0; i < 7; i++)
{
if(timeZone.IsDaylightSavingTime(finder.AddDays(i))){
return (finder.AddDays(i).AddHours(-6));
}
}
finder = DateTime.MinValue;
return finder;
}
public static DateTime GetDSTEnd(int year, TimeZoneInfo timeZone)
{
DateTime finder = new DateTime(year, 11, 1);
finder = finder.AddHours(6);
for (int i = 0; i < 7; i++)
{
if (!timeZone.IsDaylightSavingTime(finder.AddDays(i)))
{
return (finder.AddDays(i).AddHours(-6));
}
}
finder = DateTime.MinValue;
return finder;
}
iCal displays your calendar in your local time zone (or one you can set manually), but not UTC.
In an region using daylight saving time, the mapping from UTC to local time will change depending on the date. Thus, if the ics file has the date in UTC it will exhibit the problem you described. You need to:
store the time zone ID,
and also the definition of the time zone ID you use, mapping from the ID to the actual UTC offset, in your ics file.
Part 2 might not be needed on some systems, but it is better to store the TZID right in your file for maximum portablility.

Resources