How does NSMonthCalendarUnit and NSYearCalendarUnit behave exactly when used with UILocalNotification repeatInterval? - ios

How does NSMonthCalendarUnit behave exactly related to UILocalNotification repeatInterval ?
Find same day next month ?
or
Find the date for 30 days later ?
For example:
if i create a UILocalNotification with date:27/02/2015 and repeatInterval:NSMonthCalendarUnit when will be the next notification ? Do not mind whether February is 27 or 28. Just consider it is always 28. It is not the real question.
Will repetition be on 27/03/2015 or 29/03/2015 ?
Same question also applies for NSYearCalendarUnit. Is it just a addition of 365 days or does it mean same day, same month of next year (27/03/2016) ?

I would post this as an edit, but it's a bit long.
I imagine this does is based on the unit. For instance, if you create a uilocalnotification with date Feb 2, 2015 and give it an NSMonthCalendarUnit, it will call it again at every month interval for the same date.
Broken down into components Feb 2 2015 is NSDayCalendarUnit=2, NSMonthCalendarUnit=2 NSYearCalendarUnit=2015. So, the notification will be called whenever NSDayCalendarUnit=2 and NSYearCalendarUnit=2015 for any NSMonthCalendarUnit. (so if you did a leap-year date at the end of the month, like jan 30, and it was a leap year month in february, it wouldn't get called.) So, this would have the same day and year for ever NSMonthCalendarUnit. The same goes for year, except for years.
It would not make much sense for this to be an addition of days. Imagine setting the NSMonth interval for a date on the first and having it be returned on the 31st of the same month, that wouldn't be a very practical API.

Related

Why does ordinalityOfUnit use Monday as the first day of the week when using NSCalendarUnitEra?

I'm trying to calculate the number of calendar weeks between two dates. I'm using the following code:
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:thenDate];
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:nowDate];
A date of 15-1-2017, a Sunday, results in 105192 weeks, and 16-1-2017, a Monday, results in 105193. That would indicate it is using Monday as the first day of the week. I have verified that cal.firstWeekday is 1. If I change the inUnit from NSCalendarUnitEra to NSCalendarUnitYear it works correctly. Is there a way around this?
I suppose I could subtract one day from both dates but that seems very hackie.
After much experimenting, if you make sure all dates are in GMT and you set cal timeZone to GMT it works as one would expect.

Get date from week number in Google Sheets

If I have week 7 in 2017 what week date is the Monday in that week in Google Sheets?
=DATE(B9,1,1)-WEEKDAY(DATE(B9,1,1),3)+7*(WEEKDAY(DATE(B9,1,1),3)>3)+7*(A9-1)
is the least complicated formula I know which works for week numbers in Sweden (i.e. Monday first day of week, ISO rules for what is week 1).
Short answer (A1==Week, B1==Year):
=DATE(B1;1;1)+((A1-1)*7)-WEEKDAY(DATE(B1;1;1);3)
Long answer:
DATE(<year>;1;1) // days since 1970 until the frist day of the year
plus
((<week number>-1)*7) // how many days into the year is this week
minus
WEEKDAY(DATE(<year>;1;1);3) // how many extra days from previous year in first week
PS:
This assumes monday as the first day of week you have to change the arguments for WEEKDAY to change it to sunday
Because of this definition (https://en.wikipedia.org/wiki/Week) the 4th of January must be used instead the 1st. The 4th of January is the first day which is always in the week 1.
=DATE(B1;1;4)+((A1-1)*7)-WEEKDAY(DATE(B1;1;4);3)
If you are using ISO weeks, the accepted answer doesn't account for weeks overlapping on 2 technical years like 2020-w53, which is from 28 Dec 2020 until 3 Jan 2021.
Therefore I'm using this formula instead:
=DATE(K2,1,1)-WEEKDAY(DATE(K2,1,1),2)+7*(WEEKDAY(DATE(K2,1,1),2)>3)+7*(L2-1) +1
Where K is the Year, and L is the Week number (split in 2 columns from yyyy-ww)
to have it in an arrayformula:
=ArrayFormula(if(K2:K="",, DATE(K2:K,1,1)-WEEKDAY(DATE(K2:K,1,1),2)+7*(WEEKDAY(DATE(K2:K,1,1),2)>3)+7*(L2:L-1) +1 ))
You can use =ArrayFormula(if(E2:E="",,split(E2:E,"-"))) to split yyyy-ww in two columns.
NOTE: This formula would return the Monday (Which is the first day of the week in international standard, ISO)
Worked this up for 2023. It will work through end of 2024 too .. that said the AND logic is flawed .. feel free to suggest something to make this better
=IFS(
AND(ISOWEEKNUM(A8)=52,YEAR(A8)<>YEAR(A7)),
DATE(YEAR(A8-1),1,1)-WEEKDAY(DATE(YEAR(A8-1),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8-1),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1),
DATE(YEAR(A8),1,1)-WEEKDAY(DATE(YEAR(A8),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1)
)

Get current date past midnight, Objective C

I want to get the current date, even if the time has passed midnight. Imagine it's friday night the 6th of June 2014 - we check the date Saturday at 2 am, but we still want this to count as being friday. How would I go about this?
Let's just say we cut it at 9am the next day. I.e. we will assume previous date until the time has passed 9 am. Yes, this is software used at a nightclub, as you can imagine.
I guess this would involve something like subtracting 1 day from the current date if the hour is less than 10?
You can use interval to specific date like
[[NSDate date] dateByAddingTimeInterval:interval];
Assuming the cutoff is 9am, all you need to do is create an NSDate that's 9 hours earlier than the actual time.
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-(9 * 3600)];
That way every day begins and ends at 9am.

Get Week Of The Year

Delphi Xe, Win 7, System TimeZone UTC+4:0, The first day of week in system - Monday, Time is synchronised with time.windows.com
In a palette of standard components Delphi Win32 there is component MonthCalendar, at it property WeekNubers=true (display of numbers of weeks), date=now, the first day - Monday.
Shows on 19 number (today) - number of week 43.
I request number through dateutils.WeekOfTheYear(now) = returns 42.
Source (0.5 Mb) http://www.sendspace.com/file/yuz6ko
Judging by an initial code of component MonthCalendar - there like all it is true (even if not to expose the first day of week, it takes value from system parametres).
And in inquiry WeekOfTheYear it is told, that all is considered also standard ISO applied, but numbers do not coincide :(
In any way I will not understand, where one week disappears - if to look on a calendar, should be 43 and if to consider, that weeks 53 and the first concerns last year...
By the way WeekOfTheYear on 01.01.2011 too returns 52 though like should return 1...
Why distinctions and where it is correct?
Both results are correct, the difference is because the DateUtils.WeekOfTheYear function uses the ISO 8601 standard definition of a week. That is, a week is considered to start on a Monday and end on a Sunday.
Check this explanation about the Weeks Numbers in the ISO 8601 format.
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in
week 01. If 1 January is on a Friday, Saturday or Sunday, it is in
week 52 or 53 of the previous year (there is no week 00). 28 December
is always in the last week of its year.
Also the Embarcadero online help for the DateUtils.WeekOfTheYear function says
....if the first calendar day of the year is a Friday, Saturday, or
Sunday, then for the first three, two, or one days of the calendar
year, WeekOfTheYear returns the last week of the previous year.
Similarly, if the last calendar day of the year is a Monday, Tuesday,
or Wednesday, then for the last one, two, or three days of the
calendar year, WeekOfTheYear returns 1 (the first week of the next
calendar year).
So due which the 01-01-2011 was Saturday this week is considered the week number 52 of the previous year.
And that also explains why the week number returned for today (19-10-2011) is 42 instead of 43.
First of all, I could see no difference between the week numbers in the TMonthCalendar component and those returned by DateUtils.WeekOfTheYear(). (Monday is the first day of week on my system.) Both show 42 for the current week and 52 (not 53) for the week of 2011-01-01. (It's 53 for 2010-01-01, so maybe you just mixed that up.)
Now, as to why it's 52 or 53, but not 1, well, it's according to the definition of the first week by ISO:
First week is the week with the year's first Thursday in it.
So week number 1 in 2011 starts on 2011-01-03, and the previous one belongs to the previous ISO year.
The TMonthCalendar can treat the last week of the year as the first week of the next year, WeekOfTheYear function does not do it, hence the difference.
For example according to TMonthCalendar, 1 Jan 2012 belongs to both last week of 2011 and first week of 2012; 2 Jan 2012 is the first day of the second week of 2012.
According WeekOfTheYear function, 1 Jan 2012 belongs to the last week of 2011 only; 2 Jan 2012 is the first day of the first week of 2012.
Monday is the first day of the week for me.

Store a day of week (mon, tue, wed, ...) in rails db and compare with the day of today

I am developing a rails 3.1 app where I need to store opening hours for stores. I want to store opening time, closing time and the day of the week. My first idea is to store a day number where 1 is monday and 7 sunday but is there a better approach which is more suitable when I want to internationalize my application?
If i store the day number, how can I compare it with the day of week today to check if the store is open or not?
Thanks for any help
Sounds like you're on the right track. To figure out the current day of the week:
Date.today.wday
Although that considers Sunday 0 and Saturday 6.
A number value should be fine. Use the numbers used by the Ruby Time class to keep things simple: http://www.ruby-doc.org/core/classes/Time.html#M000369
Sunday will be 0, Monday 1, etc...

Resources