Wrong time on SQLite timestamp on the iPhone - iphone

From my iPhone app I insert some data in my sqlite database that include a date using the CURRENT_TIMESTAMP default value. Everything works great except for the fact that the time is 1 hour behind the time it should be. And that happens both on the device and the Simulator.
Any sqlite settings (like current time) i can access somewhere?
If you want to see the code I am using for that you can take a look at my answer to this post: sqlite datetime data type with iphone NSdate?

It sounds as though there might be a day light savings time zone inconsistency between SQLite and the time zone that NSDateFormatter is using. It looks as though SQLite stores the datetime in GMT. Therefore you'll need to make sure that NSDateFormatter is also set to GMT. It appears as though you can do this with:
gmtFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[gmtFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]
Once you have your NSDate instance I'd expect that you could for example get a string version of it showing the correct local time using a NSDateFormatter instance that is set with the local/system time zone.
Note: I haven't actually tried this.

You might want to consider using Core Data which is now available in O/S 3.0 - this handles things like this automatically for you without you having to specify timezones etc - it will just use the user's defaults and keep everything consistent.
There are many other advantages - but this seems like a relevant one.

I guess you better implement teabot's suggestion. The Problem comes when one timezone has wintertime and one doesnt...

I have just had to fix this problem also, and teapot's answer is the correct one.
The timestamp created by sqlite is in GMT, so when you read it from the database you need to tell your date formatter to read it in GMT also. The resulting NSDate will be a date correct in whatever timezone you are in.
The way you say you have corrected it, by adding 1 hour, will eventually fail on you. Anyone using the app in the GMT timezone will see all times +1 hour. Anyone in other timezones will see a completely different time offset.
(I know this is years old, but it has just helped me).

Well, since I couldn't find where this 1 hour difference comes from, I used the following method to fix the date. It's not a proper solution but it does exactly what i need. So in case someone else runs into the same trouble as me.. here's my fix:
NSDate *date = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:[formatter dateFromString:score.datetime]];
which is just adding a one hour delay to the datetime, therefore making it correct.

Related

Cloud Function writing wrong time on firestore

When I write a Timestamp on Firestore it shows 6.00PM on database though I have not defined any time in the Date object.
My Approach to get Timestamp from date :
let reservationDate = new Date(year,month,dayOfMonth);
let bookedRoomData = {
....
...
reservationDate: admin.firestore.Timestamp.fromDate(reservationDate),
...
};
What is the reason behind showing 6.00PM instead of 12.00AM ?
What should be done to fix this?
Screenshot of database is given bellow -
Since you tagged this google-cloud-functions, I'm assuming that your code is running in Cloud Functions.
When you create a new Date object on any machine, it uses the local machine's sense of timezone. So you're making a date at midnight in whatever timezone has been assigned to your Cloud Functions instance. It's not the timezone where you're computer's clock is set.
When you see a timestamp in the console, it's always going to appear on your computer's clock's configured timezone. So, your computer is 6 hours behind whatever is being used by Cloud Functions.
If you want a specific time, you should make sure your Date is configured that way. Consider using a library such as momentjs to create dates according to timezones of your interest. Bear in mind that Timestamp objects do not encode a timezone. They just render in the console according to your local timezone.

[[NSDate date]timeIntervalSince1970] seems to be 1hr behind

I am writing an app that communicates with some web services. When a variable is updated on the server, it will return a unix timestamp to tell me when it was updated. I can then compare it using:
[[NSDate date]timeIntervalSince1970]
to keep things in sync.
The system time on the iPhone is correct. But unix time seems to be 1hr behind the server(and system time as shown on the clock). Why is this happening? Do I need to set a time zone?
timeIntervalSince1970 is defined to return:
The interval between the receiver and the reference date, 1 January
1970, GMT.
Is it possible you're comparing to a time that is defined to be from 1 January 1970 in some other timezone? If so, the easiest thing to do is to use timeIntervalSinceDate: rather than timeIntervalSince1970, possibly having used NSDate's dateByAddingTimeInterval: to move from the reference date in GMT to the reference date in your ideal time zone.
You can use NSTimeZone's secondsFromGMTForDate: property if you don't want to hard code things.
As it can be understood from your question, the problem is in the timestamp that returned by server. So the real question is if you have an access to that server. Or do you know in what timezone it is. If first, update time on server. Second - calculate difference between timezones (you say it's a hour) and add it to your NSDate instance.
You also may use NSCalendar as it have -(void)setTimeZone:(NSTimeZone *)timezone; method.(thought it may be overhead for your needs)

Is this a bug in plist or Xcode?

G'day All
If you create a date item in the plist editor of Xcode or Apple's standalone plist editor you get something of the form <date>2010-05-29T10:30:00Z</date> which is a nice well formed ISO date at UTC (indicated by the "Z"). Because I'm in timezone UTC +10 when that's read into my app & then displayed I get 8:30 PM out, still good. However if that is a time in my timezone it should be <date>2010-05-29T10:30:00+10</date> (replacing "Z" with my timezone offset). All of my attempts at reading such dates into my iPhone app have had the plist rejected as if it is malformed & editing a plist with such a date in Apple's editors changed the "+10" to "Z" without adjusting the time.
Do others think I'm correct in thinking this is a bug in either plist or Xcode? My feeling is that the implementation of ISO date & time in plist is incomplete.
Cheers, Pedro :)
It's not a bug in either. In CoreFoundation (and Foundation), all dates are represented in Zulu time, which is why they are serialized that way. The date is then formatted for display based on the timezone of the device that wishes to display it. Although this is an ISO date string, the only valid time zone for CoreFoundation/Foundation is Zulu time.
If, for some reason, you need to track the time zone that any given date was generated in, you should track this as a separate property. If you need to write an XML property list from somewhere else, you must first convert the date to zulu and then write it out (although the documentation clearly specifies that these keys are for debugging aids/readability only and may change in the future). This makes plists a decent way to serialize data between two Cocoa/CoreFoundation applications, but a less suitable way of serializing data between a Cocoa/CoreFoundation app and some other application.

What is the proper way to convert a NSTimeInterval to a string, that works internationally?

I know the quick and dirty way of dividing by 60 to get minute, hours, etc
But is there an official way in the API already implemented, and using the appropriate language for minutes, seconds, etc?
Thanks for any help.
Use one of NSDate's -dateWithTimeInterval... methods to create a date from your interval, then hand it to an NSDateFormatter that is configured to display the date formatted as desired.

passing timezone from client (GWT) to server (Joda Time)

I'm using GWT on the client (browser) and Joda Time on the server. I'd like to perform some DB lookups bounded by the day (i.e. 00:00:00 until 23:59:59) that a request comes in, with the time boundaries based on the user's (i.e. browser) timezone.
So I have the GWT code do a new java.util.Date() to get the time of the request, and send that to the server. Then I use Joda Time like so:
new DateTime(clientDate).toDateMidnight().toDateTime()
The trouble of course is that toDateMidnight(), in the absence of a specified TimeZone, will use the system's (i.e. the server's) TimeZone. I've been trying to find a simple way to pass the TimeZone from the browser to the server without much luck. In GWT I can get the GMT offset with:
DateTimeFormat.getFormat("Z").fmt(new Date())
which results in something like "-0400". But Joda Time's DateTimeZone.forID() wants strings formatted like "America/New_York", or an integer argument of hours and minutes. Of course I can parse "-0400" into -4 hours and 0 minutes, but I'm wondering if there is not a more straightforward way of doing this.
You could use java.util.Date's getTimezoneOffset() method. It's deprecated, but that's pretty usual for Date handling in GWT currently.
And AFAIR, you can specify something similar to "UTC+4" in Joda time.
Update: I looked it up, and it's "+04:00". Or use DateTimeZone.forOffsetHours() or even forOffsetMillis().
Gwittir (http://www.gwtsite.com) is a library for GWT that includes many cool utilities, like databinding, animation, reflection, and more. However, there are some other interesting goodies as well like the new Joda Time integration. If you have ever been frustrated by GWT’s lack of java.util.Calendar support, you’ll love this, as it makes it easy to do date manipulations in your applications.
otherwise, there are other ways to get timezone offset with + & -.
import java.util.TimeZone;
use: TimeZone.getDefault().getRawOffset()
this function will return the offset time in millisecond about your phone seeting. For Example, GMT-04:00 is equals to (-4)*60*60*1000 = -14400000.
After some operations to get the number which you want.
I have a similar but slightly different problem I think.
I actually need to store the clients timezone on the server, so that I can send out messages about dates stored in their calendar.
The dates are stored in UTC time in google app engine and of course I can store the current Timezone offset when creating the appointment. The problem comes when for instance I want to send out a summary email with a list of upcoming appointments in it. These appointments need to be offset with the correct Timezone adjustments for the client (Im happy to assume that they are still in the same timezone as when they created the appointment).
The real problem comes with Daylight Savings adjustments, so for instance I might have appointments stored for Saturday 30th October 2010 at 1pm (BST[GMT+60]) and Monday 1st November 2010 at 1pm (GMT).
So as you can imagine, I cant just use the current timezone offset (BST) as that would mean that the appointment on Monday 1st November would be listed as 2pm rather than 1pm (GMT+60)
It occurs to me that the best way to deal with this is just to store the timezone offset with each appointment individually, but I feel it would be much better to be able to determine the original timezone correctly in the first place, then just let java do the correct adjustments.