I just took a stab at creating some code (pasted below) that would check for a day change. However, now I'm reading that there's a special function for day changes.
NSCalendarDayChangedNotification
Apparently it works in iOS8 and later. But people keep using the phrase "you can listen to it"... as if using that function is like tuning into a radio station. I looked it up and the last WWDC provided a presentation with some relevant code:
Reacting to the Change of Day • You may want to run some code when the
day changes NSCalendarDayChangedNotification • Example
noteCenter = [NSNotificationCenter defaultCenter];
observer = [noteCenter addObserverForName:NSCalendarDayChangedNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
// your code here
}];
My Question
The PDF I found doesn't say much else though. So my question is how does this function work? The PDF makes it seem like it truly is just this simple, paste that code in and I'm done. Is that true? Will it adjust for timezones? Do I have to enable this notification center that it is referencing?
My code (not sure if it would work):
//Find out if it's a new day
let lastDateUsed_NSDefault = NSUserDefaults.standardUserDefaults()
if let endOf_LastDateUsed = lastDateUsed_NSDefault.objectForKey("lastDateUsed") as! NSDate! {
print("Success! NSUserDefault key: 'lastDateUsed' returned \(endOf_LastDateUsed)")
//Check if the day has changed
let calendar = NSCalendar.currentCalendar()
let startOfToday = calendar.startOfDayForDate(now)
if endOf_LastDateUsed.compare(startOfToday) == NSComparisonResult.OrderedAscending {
//endOf_LastDateUsed is greater than startOfToday.
print("Do nothing! Last date used must have been today!")
} else if endOf_LastDateUsed.compare(startOfToday) == NSComparisonResult.OrderedAscending {
//Day has changed. Run alert.
//Save "endOfToday" date value to NSDefault
let endOfToday = startOfToday.dateByAddingTimeInterval(24 * 60 * 60)
NSUserDefaults.standardUserDefaults().setObject(endOfToday, forKey:"lastDateUsed")
print("Time stored in NSDefault: \(endOfToday)")
}
}
else {
print("Failure! NSUserDefault key: 'lasteDateUsed' returned nothing. No record.")
//First time user - Set end of today
let calendar = NSCalendar.currentCalendar()
let startOfToday = calendar.startOfDayForDate(now)
print("startOfToday: \(startOfToday)")
let endOfToday = startOfToday.dateByAddingTimeInterval(24 * 60 * 60)
//Store
NSUserDefaults.standardUserDefaults().setObject(endOfToday, forKey:"lastDateUsed")
print("Time stored in NSDefault: \(endOfToday)")
}
The documentation (which was slightly harder to locate than I'd have hoped, admittedly) is reasonably clear:
Posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone.
So yes, it should handle daylight saving changes appropriately, given that it knows about the time zone. I don't know about enabling the notification centre, but that should be easy enough to test by adjusting the system clock or time zone on a device and seeing what happens.
(What isn't clear from the docs is whether this event is fired if the date is changed via manual intervention, e.g. through changing the system time or time zone in a way that directly changes the date. It would be worth you experimenting with that.)
Related
I am trying to control the running speed of my model programmatically, because in my simulation, if it is not working hour, nothing happens. I wrote the code below and used a cyclical event that cycle every hour to execute it. I am not sure why it does not work. Thanks for the help!
Date date = date();
if (getDayOfWeek(date)==1||getDayOfWeek(date)==7)
{
getEngine().runFast();
}
else if (getHourOfDay(date)<8||getHourOfDay(date)==12||getHourOfDay(date)>=17)
{
getEngine().runFast();
}
else
{
getEngine().setRealTimeScale(5);
}
You are using getEngine().runFast(); which in the documentation says
The run is done in virtual time mode regardless any settings.
But I could not get it to work without adding the getEngine().setRealTimeMode(false); code
I tested this and it works - the engine time jumps from weekday 6 (Friday) to weekday 2 (Monday)
if (getDayOfWeek()==1||getDayOfWeek()==7) {
getEngine().setRealTimeMode(false);
getEngine().runFast();
} else if (getHourOfDay()<8||getHourOfDay()==12||getHourOfDay()>=17){
getEngine().setRealTimeMode(false);
getEngine().runFast();
} else {
getEngine().setRealTimeMode(true);
getEngine().setRealTimeScale(5);
}
Note that you don't need to get date as the default is to use the current model date
I have a problem with Google Docs' Utilities.formatDate() function.
I have a spreadsheet that contains all of the orders we place in the lab. When an order is delivered our lab manager enters the delivery date in the relevant cell in such a spreadsheet, in the following format: dd.MM.yyyy.
I created a script that, provided certain conditions, will email whoever placed that order alerting them that the order has been delivered on that particular date. Here is the code:
function DeliveryAlerts() {
try {
var email_dict = {"Y":"Y#Z.com"}
var spreadsheet = SpreadsheetApp.openById("ABC");
SpreadsheetApp.setActiveSpreadsheet(spreadsheet);
var sheet = spreadsheet.getSheetByName("Orders");
var values = sheet.getRange("A2:Q251").getValues();
var bgcolours = sheet.getRange("A2:Q251").getBackgrounds();
for(var i=0;i<=249;i++)
{
var j = i + 2;
if (values[i][16]=="Yes" && values[i][11]!="" && bgcolours[i][16]!="#b8b8b8")
{
var email_address = email_dict[values[i][13]];
var cur_date = Utilities.formatDate(values[i][11], "GMT+1", "EEE dd.MM.yyyy");
var message = "Hello there,\n\nYour order of " + values[i][4] + " has been delivered on "+ cur_date +".\n\nBest wishes";
var subject = "Delivery alert";
MailApp.sendEmail(email_address, subject, message,{replyTo:"abc#abc.com", name:"ABC"});
sheet.getRange("Q"+j).setBackground("#b8b8b8");
}
}
} catch (err) {
MailApp.sendEmail("abc#abc.com", "Delivery Alerts Script in Order Master List", err);
}
}
I use
Utilities.formatDate(values[i][11], "GMT+1", "EEE dd.MM.yyyy") to reformat the date from, say, 25.05.2015 (that is, the value in the cell) to Mon 25.05.2015. However, what I get instead is Sun 24.05.2015.
Does anybody know what is going on?
Thank you in advance.
Nicola
Check the time zone setting in the script editor. Under the FILE menu, choose PROJECT PROPERTIES in the script editor. It's possible to have a different time zone setting in Apps Script, than is in the spreadsheet. This is a common issue that arises. Apps Script allows a separate time zone setting from the spreadsheet. Also, even if the time is only off by one minute, if the time setting of the date is all zeros, it's common to get the problem that you are having. When a user enters a date, it's possible that no time setting is made. So the time is set to all zeros. The date is correct, but the time is all zeros. Even if the date was typed in at 3 in the afternoon, for example, and the date is correct, the time setting can be midnight of that day. So, even if you subtracted one second from that date, it would now be the day before.
I've noticed that if a date property comes back from the server with the value "2013-07-11T17:11:04.700", then breeze changes the value to Thu Jul 11 19:11:04 UTC+0200 2013.
Notice the time is now 2 hours ahead !
I had already come across this issue when saving entities, so I had to explicitly convert my date properties using momentjs :
date.hours(date.hours() - moment().zone() / 60);
But now it seems the problem occurs also when doing read operations.
What's the best way to make sure breeze does not alter values of my date properties ?
Breeze does not manipulate the datetimes going to and from the server in any way EXCEPT to add a UTZ timezone specifier to any dates returned from the server that do not already have one. This is only done because different browsers interpret dates without a timezone specifier differently and we want consistency between browsers.
The source of your issues is likely to be that when you save your data with dates to the database, that the dateTime datatype you are using does NOT contain a timezone offset. This means that when the data is retrieved you are likely "losing" the offset and the Breeze default mentioned above kicks in. This can be corrected by using a database date time datatype with an timezone offset ( datetime2 or datetimeoffset in SQLServer).
Note that your browser DOES format dates according to it's current timezone.
Another approach is that you can replace Breeze's DataType.parseDateFromServer to NOT infer any time zone info if it is not provided:
breeze.DataType.parseDateFromServer = function (source) {
return new Date(Date.parse(source));
};
However, this can run into the problem that different browsers interpret DateTime strings without a time zone offset differently... So you may still get strange results depending on the browser. If that happens you will need to add some browser detection code to the snippet above.
Another alternative is to do the following using the moment.js library.
breeze.DataType.parseDateFromServer = function (source) {
var date = moment(source);
return date.toDate();
};
Not sure how helpful this is, but hopefully Breeze's behavior is clearer.
By default, Breeze does not provide any way to do this, but you can keep the below code in your model JS file to overcome this issue:
breeze.DataType.parseDateFromServer = function (source) {
if (typeof source === 'string') {
//Check for local offset time or UTC time from server
if (source.slice(-1) !== "Z") {
var oldSource = source;
try {
source = source.substring(0, source.lastIndexOf("-") - 1)
source = new Date(source);
var tzDifference = source.getTimezoneOffset();
//convert the offset to milliseconds, add to targetTime, and make a new Date
var offsetTime = new Date(source.getTime() + tzDifference * 60 * 1000);
return offsetTime;
}
catch (err) {
source = new Date(source);
return source;
}
}
else {
source = new Date(source);
var tzDifference = source.getTimezoneOffset();
//convert the offset to milliseconds, add to targetTime, and make a new Date
var offsetTime = new Date(source.getTime() + tzDifference * 60 * 1000);
return offsetTime;
}
}
}
I have been trying to use UILocalNotification and setting timezone to some other timezone than my default.
The purpose is to do something like:
-User will schedule an alert with time and timezone
-Show alert when that timezone becomes specified time.
In more verbal description, let say I live in LosAngeles and my gf is in Chicago. I want to set up an alert when chicago is 7am, for weekdays.
For one alert, I can do without timezone, to do absolute time. But I want to allow users to set repeat flags, in which case I can't seem to do - I tried setting localnotification's timezone property to chicago as above example, however the local notification will not fire.
I logged the uinocalnotification during serialization process, and here's one output:
"<UIConcreteLocalNotification: 0xfb25a50>{fire date = 2011-02-06 06:02:00 -0800, time zone = America/Chicago (CST) offset -21600, repeat interval = 16, next fire date = 2011-02-06 08:02:00 -0800}"
I set firedate for 9:02am chicago absolute time, and timezone to cst - and the log does mention that the fire date is indeed 06:02 my local time. However, nothing happens. Also, next fire date is weird since it should be +1 day, not +2 hrs.
Here's how I set this up:
Class classUILocalNotification = NSClassFromString(#"UILocalNotification");
if (classUILocalNotification != nil) {
id note = [[classUILocalNotification alloc] init];
NSString *body = #"body message";
switch (repeatflag) {
case 1: [note setRepeatInterval:NSDayCalendarUnit]; break;
case 2: [note setRepeatInterval:NSWeekdayCalendarUnit]; break;
case 3: [note setRepeatInterval:NSWeekCalendarUnit]; break;
case 4: [note setRepeatInterval:NSMonthCalendarUnit]; break;
case 5: [note setRepeatInterval:NSYearCalendarUnit]; break;
default: break;
}
[note setFireDate:dt];
[note setAlertBody:body];
[note setTimeZone:timezone];
[note setHasAction:NO];
}
This seems like some unknown behavior. Anyone have a better way to achieve what I am trying to do?
Thanks!
There appears to be a bug with UILocalNotification when using non-local timezone. The first event fired is fired for the correct date and time and timezone. However, subsequent events fire for the given date and time in your local timezone instead of the one set in the setTimeZone method. [iOS 6.1]
I need to calculate the number of seconds that have passed between two events on the iPhone. To do so, I need to store the time that the first event occurred and check it against the time the second event occurred to see if more than 30 seconds has passed.
I'm about to begin trying to accomplish this using the NSDate class, but I was wondering if there's a simpler way to do this without using objects, as I would prefer to store simple, intrinsic values instead of objects.
Thanks for your wisdom!
If you really want to avoid storing objects, you can do something like:
double startTime = [[NSDate date] timeIntervalSince1970];
//Run your other code
double endTime = [[NSDate date] timeIntervalSince1970];
if (endTime - startTime > 30) {
//30 seconds have passed
}
You can use
[NSDate dateWithTimeIntervalSinceReferenceDate:anotherDate];
or use the initWithTimeIntervalSinceReferenceDate: method
have you seen UIEvent's timestamp?
example:
double event1Timestamp, event2Timestamp; ///< ivars in your class?
{ // some function/method body
event1Timestamp = event1.timestamp;
}
{ // some other function/method body
/*** later that day ***/
event2Timestamp = event2.timestamp;
if (30 <= (event2Timestamp-event1Timestamp)) {
printf("ok, it's been 30 seconds");
}
}
where event1 and event2 are naturally the incoming events