According to Apple Doc, the 'wrappingComponents' parameter serve this purpose:
If true, the component should be incremented and wrap around to
zero/one on overflow, and should not cause higher components to be
incremented. The default value is false.
What I'm having trouble understanding is the 'overflow' part. What is this overflow and when does this overflow happen? Apple Doc currently does not explain this part in its documentation.
Thanks for your answer in advance.
"Overflow" means that the result of the adding of date components goes over the allowed range of that component. For example, adding 5 days to June 30 is an "overflow" because June 35 does not exist. Other examples include adding 7 hours to 18:00, 4 months to December, etc. This also applies to subtracting too.
What happens by default (wrapping components = false) is that the larger component gets incremented: if you add 5 days to June 30, you get July 5:
However, if you set it to true, it wraps around, meaning that the larger component doesn't change - you get June 5.
Today is June 8 for me. Adding 29 days with wrapping components gives June 7:
let newDate = Calendar.current.date(byAdding: DateComponents(day: 29), to: Date(), wrappingComponents: true)
print(newDate)
Related
I have the year a car was purchased in column A. For example, 2015. I'm trying to calculate the age of the vehicle comparing the year provided in column A to TODAY() in an arrayformula, like this...
={"Vehicle Age";arrayformula(if(A2:A="",,(datedif(A2:A,today(),"Y"))))}
For some reason, it gives me the number 115 as the result for every cell where a year has been specified. Any idea why? I can't seem to find an answer on this anywhere on the internets.
Thanks for your help!
You are mixing apples and oranges here, so to speak.
Internally, Google Sheets sees all full dates as a number of days from an origin point of December 31, 1899. As such, the year 2015 on its own, in a comparison with a full date will be seen as two-thousand-fifteen days since December 31, 1899 (or July 7, 1905 — which was 115 some-odd years ago, as would be the case with any relatively recent year, because they'll all be interpreted in their raw form by Sheets as a cluster of days from late June to early July of 1905).
Instead, you want to compare the years only, which will mean extracting the year from TODAY(), since A2:A are already year-only numbers:
={"Vehicle Age";arrayformula(if(A2:A="",,year(TODAY())-A2:A))}
However, since any year's car models are actually released the year before, you may want to add a year to your formula:
={"Vehicle Age";arrayformula(if(A2:A="",,year((TODAY())-A2:A)+1))}
Of course, you could also have turned your A2:A years into real dates (e.g., January 1 of each year listed) and then used datedif as well:
={"Vehicle Age";arrayformula(if(A2:A="",,datedif(DATE(A2:A,1,1),today(),"Y")))}
... or with that extra year added ...
={"Vehicle Age";arrayformula(if(A2:A="",,datedif(DATE(A2:A,1,1),today(),"Y")+1))}
={"Vehicle Age";arrayformula(if(A2:A="",,datedif(DATE(A2:A,1,1),today(),"Y")))}
Not working
can it be that Google Sheets is not calculating right?
=DAYS360(DATE(2016;12;31); date(2017;1;1))
is 1 and also this is 1 but is should be 2:
=DAYS360(DATE(2016;12;30); date(2017;1;1))
Whats wrong here?
I think it is because of the formula that is set to 360 days. It is said here that DAYS360 - Days between two dates on a 360-day year. So it means that you will only have a 30 days a month not including the date 31 in the months of (Jan, March, May, July, Aug, Oct and Dec).
For example, you use this =DAYS360(DATE(2016,10,25), date(2016,10,30)) so the expected output is 5 - which is correct
If you use =DAYS360(DATE(2016,10,25), date(2016,10,31)) the expected output is 6 - because you specify the date 31.
But, if you use =DAYS360(DATE(2016,10,25), date(2016,11,3)), the output is 8 not 9 - because it did not include the date 31.
I hope I explained it clearly.
I am writing a Google Calendar sidebar gadget to keep track of the total hours per event tag (as determined in details of the event i.e. "tags: work").
Users can change the current week, month, day they are viewing in the calendar and I want to be able to count up the hours pertaining to their current view.
I don't see anywhere in the gadget API (or any other Google Calendar API) that allows gadgets to access the currently displayed view. I have noticed that the URL has an anchor tag that looks like
g|week-2+23127+23137+23131
which corresponds to viewing Monday Feb. 23, 2015 - Sunday March 1, 2015 in week mode.
I have also noticed the following relationships:
23127 is the first day in the view
23137 is the last day in the view
23131 is the day selected in the month view (on the left of the calendar)
If there is a way to get the currently displayed view using the API, that would be ideal but I would settle for parsing the anchor tag. Unfortunately I cannot decipher how the numbers work.
Google API
The currently displayed date range can be accessed using the following call:
google.calendar.subscribeToDates(function(d) {
// do something
});
where d is a Google date range d.startTime and d.endTime being the beginning and end.
Numbers
The numbers in the URL do not correspond directly to epoch date and time. Rather, each year has 512 days associated with it and each month has 32 days. For example, February has 28 days regularly but every leap year it has 29. The calendar never has to adjust for this since it simply allots each month 32 days and comes out with a nice even number every time.
A careful examination of the date ranges displayed will also show you that if you subtract the number for December 31 from January 1 you get 130. Accounting for the beginning and the end (don't count December 31 and January 1) will give you 128.
12 * 32 + 128 = 512 -- 12 months a year, 32 days a month and a 128 gap per year
Also, for some reason January 1, 1970 has the associated number of 33 so add that to your calculations when determining dates.
This wouldn't fit in the comments, but here's how the encoding works:
The encoding scheme makes it easy to find the day/month/year from the number.
Take 23131 which yields Feb 27, 2015 (from the example in your question).
Divide by 512 and add 1970 (epoch) for the year.
23131 / 512 = 45.xxx => 45 + 1970 = 2015.
Get the remainder of that division and divide by 32 to find the month.
23131 mod 512 = 91 / 32 = 2.xxx = February
Get the remainder of that division and it's the day.
91 mod 32 = 27
In the process of migrating one of my Win32 VCL application from Delphi 2006 to delphi XE6 I encountered the following issue :
One of my forms has a TMonthCalendar (plugged on a TPanel for the record) to help the user select a week to view in a graph. By week I mean Monday being the first day and Sunday the last (french locale). To achieve such a week selection pattern I set the multiSelect property to true and put the following code inside the CalendarClick event :
MonthCalendar1.MultiSelect := True;
//Temporarily storing the selected day in a variable
TempoDate := MonthCalendar1.Date;
//searching for the monday right before the selected day (by user)
while dayOfWeek(TempoDate) <> 2 do
TempoDate := IncDay( TempoDate , -1 );
//Setting the monday as the start date of the selection
MonthCalendar1.Date := TempoDate;
//Setting the Sunday as the last day of selection
MonthCalendar1.EndDate := IncDay(tempoDate, 6);
That used to work well on Delphi 2006 ( compiled on a win XP computer ).
Now that I have ported the same code to Delphi XE6 ( compiled on a win7 computer ) I have the following problems :
When clicking the right arrow (>) to switch to the next month it fails most of the time. It actually fails when the monday of the week containing the 1st of the next month is still in the previous month. Ex : switching from Sept '14 to Oct '14 fails because the monday before Oct. 1st is in september (Monday Sept. 29th).
So that brings me back to September.
On the other hand, switching from August 14 to September 14 works because Sept. 1st is a monday.
When clicking on the first days of the next month (the few grey one you can click on) the month doesn't switch anymore.
all that used to work before.
I've made some specific isolation tests :
Creating a minimal app under XE6 with the same behaviour -> still fails (of course)
Creating the same minimal app under Delphi 2006 -> it all work as expected.
My intuition is that the TMonthCalendar now takes the .Date property to define which month to show, while on D2006 it used to take .EndDate property.
Doesn't know if this is a VCL evolution or a microsoft MonthCalendar underlying component behaviour change (since i compiled on XP then SEVEN ).
Thanks for your help
Useful documentation :
http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.ComCtrls.TMonthCalendar
http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar(v=vs.110).aspx
Unfortunately I can't provide you with solution but athleast I have eplanation for current behavior.
The problem you are facing is the TMonthCalender controll itself and which date fields is trated as selected even when using multiselect.
If you take a good look you will notice that even when using multiselect one day always have doted square around it. That date controls which month is focused.
So now you need to figure out how to change that behavior working with multiple selection enabled. I laredy tried setting the Date and EndDate properties so that EndDate value was actually lower since I thought that doing so miygt force MonthCalendar to treat last day of the weak to be selected when detirmining which month is focused but it has no effect. But it has no effect.
As for finding starting and ending week date use these functions:
TempDate := MonthCalendar1.Date;
WeekStart := StartOfTheWeek(TempDate);
WeekEnd := EndOfTheWeek(TempDate);
Both of these functions treat monday as fist day of the week.
I wish I could have helped you more.
Trying to parse and XLSX file using roo gem in a ruby script.
In excel dates are stored as floats or integers in the format DDDDD.ttttt, counting from 1900-01-00 (00 no 01). So in order to convert a date such as 40396 - you would take 1900-01-00 + 40396 and you should get 2010-10-15, but I'm getting 2010-08-08.
I'm using active_support/time to do calculation like so:
Time.new("1900-01-01") + 40396.days
Am I doing my calculation wrong or is there a bug in active support?
I'm running ruby 1.9.3-mri on Windows 7 + latest active_support gem (3.2.1)
EDIT
I was looking at the older file in Excel with the wrong data - my script / console were pulling the right data - hence my confusion - I was doing everything right, except for using the right file!!!! Damn the all-nighters!
Thanks to everyone replying, I will keep the question here in case somebody needs info on how to convert dates from excel using ruby.
Also for anyone else running into this - spreadsheet gem DOES NOT support reading XLSX files at this point (v 0.7.1) properly - so I'm using roo for reading, and axlsx for writing.
You have an off-by-one error in your day numbering - due to a bug in Lotus 1-2-3 that Excel and other spreadsheet programs have carefully maintained compatibility with for 30+ years.
Originally, day 1 was intended to be January 1, 1900 (which would, as you stated, make day 0 equal to December 31, 1899). But Lotus incorrectly considered 1900 to be a leap year, so if you use the Lotus numbers for the present and count backwards, correctly making 1900 a common year, the day numbers for everything before March 1st, 1900, are one too high. Day 1 becomes December 31st, 1899, and day 0 shifts back to the 30th. So the epoch for date arithmetic in Lotus-based spreadsheets is really Saturday, December 30th, 1899. (Modern Excel and some other spreadsheets extend the Lotus bug-compatibility far enough to show February 1900 actually having a 29th day, so they will label day 0 "December 31st" while agreeing that it was a Saturday! But other Lotus-based spreadsheets don't do that, and Ruby certainly doesn't either.)
Even allowing for this error, however, your stated example is incorrect: Lotus day number 40,396 is August 6th, 2010, not October 15th. I have confirmed this correspondence in Excel, LibreOffice, and Google sheets, all of which agree. You must have crossed examples somewhere.
Here's one way to do the conversion:
Time.utc(1899,12,30) + 40396.days #=> 2010-08-06 00:00:00 UTC
Alternatively, you could take advantage of another known correspondence. Time zero for Ruby (and POSIX systems in general) is the moment January 1, 1970, at midnight GMT. January 1, 1970 is Lotus day 25,569. As long as you remember to do your calculations in UTC, you can also do this:
Time.at( (40396 - 25569).days ).utc # => 2010-08-06 00:00:00 UTC
In either case, you probably want to declare a symbolic constant for the epoch date (either the Time object representing 1899-12-30 or the POSIX "day 0" value 25,569).
You can replace those calls to .days with multiplication by 86400 (seconds per day) if you don't need active_support/core_ext/integer/time for anything else, and don't want to load it just for this.
"Excel stores dates and times as a number representing the number of days since 1900-Jan-0, plus a fractional portion of a 24 hour day: ddddd.tttttt . This is called a serial date, or serial date-time." (http://www.cpearson.com/excel/datetime.htm)
If your column contains a date time, rather then just a date, the following code is useful:
dt = DateTime.new(1899, 12, 30) + excel_value.to_f
Also keep in mind that there are 2 modes of dates in an excel worksheet, 1900 based and 1904 based, which typically is enabled by default for spreadsheets created on the mac. If you consistently find your dates off by 4 years, you should use a different base date:
dt = DateTime.new(1904, 1, 1) + excel_value.to_f
You can enable/disable 1904 date mode for any spreadsheet, but the dates will then appear off by 4 years in the spreadsheet if you change the setting after adding data. In general you should always use 1900 date mode since most excel users in the wild are windows based.
Note: A gotcha with this method is that rounding might occur +/- 1 second. For me the dates I import are "close enough" but just something to keep in mind. A better solution might use rounding on fractional seconds to solve this issue.
You're doing your calculation wrong. How do you arrive at the expected result of 2010-10-15?
In Excel, 40396 is 2010-08-06 (not using the 1904 calendar, of course). To demonstrate that, type 40396 into an Excel cell and set the format to yyyy-mm-dd.
Alternatively:
40396 / 365.2422 = 110.6 (years -- 1900 + 110 = 2010)
0.6 * 12 = 7.2 (months -- January = 1; 1 + 7 = 8; 8 = August)
0.2 * 30 = 6 (days)
Excel's calendar incorrectly includes 1900-02-29; that accounts for one day's difference between your 2010-08-08 result; I'm not sure about the reason for the second day of difference.