Algorithm for Calendar Calculation - algorithm

One of the interview question..
If
1st january 2006 --> Sunday
Then
1st january 2010 --> ???

add 1 day for each non-leap year and 2 days for each leap year whatever comes that value mod 7.And finally add that amount of days with the given day.
for eg.
2007=1 day
2008=2 days
2009=1 day
2010=1 day
so total 5 days
and 5 % 7=5
Add 5 days in sunday,so you will get Friday

Year 2006 is not a leap year, it has 365 days.
Year 2007 has 365 days.
Year 2008 is a leap year, it has 366 days.
Year 2009, 365 days.
So there're 365+365+366+365==1461 days different of the two dates.
One week has 7 days, and 1461 mod 7 is 5, 5 days after Sunday is Friday.
So the answer is Friday.

Related

PromQL query for CPU average for current week & last week from Monday to Friday

I am trying to figure out the Prometheus query for average CPU usage for current week and last week from ONLY Monday to Friday excluding the weekends. I have come up with a query which fetches the information from last 1 hour for current time and last hour a week ago which is as below
For current last 1 h:-
1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1h])) by (instance)
For over last hour a week ago:-
1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1h] offset 1w)) by (instance)
Did anyone had similar requirements for finding the same from Monday to Friday please?

Calculate business hours between two dates by excluding non business hours in pandas data frame

I have two columns with start time and end time. I want to calculate difference between these two columns and exclude non-business hours. Business hours start on Monday 2:30 AM and ends on Saturday 6:30 AM.
Monday and Saturday has different business hours than Tuesday, Wednesday, Thursday & Friday
Business hours on Monday starts at 2:30 and ends at 24:00 and for rest days starting Tuesday till Friday starts at 00:00 and ends at 24:00
Business hours on Saturday starts at 00:00 and ends at 7:00
Input data
1st Output- where difference will be calculated for each row excluding non-business working hours
2nd Output- Will filter based on state and add all the calculated hours
enter image description here

Ruby week number results

Why when I use
Time.now.strftime('%Y%W')
or
Date.today.strftime('%Y%W')
they return 201912 while it should be 201913 as we are in week 13, not 12.
How to get the current week number?
If you look in the documentation, it says this:
Week number: The first week of YYYY that starts with a Sunday or
Monday (according to %U or %W). The days in the year before the first
week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
%W - Week number of the year. The week starts with Monday. (00..53)
January 1st, 2019 was on a Tuesday, so that would have been week 0 - making today week 12.
Time.now.strftime('%Y%V')
Would give you the output your looking for.
%V - Week number of the week-based year (01..53)
You have to look for the week-based year.

oracle function to calculate week of date by considering Sunday to Saturday as a week

Hi we have requirement to determine week of date by considering "Sunday to Saturday" as one week but i went through the link [oracle function][1]
[1]: http://www.techonthenet.com/oracle/functions/to_char.php here there are options like 'IW'(week of year ISO standard) which calculates week of year by considering Monday to Sunday as one week but we have specs to consider "Sunday to Saturday" as one week.Can any one suggest how to calculate ?
In order to determine the "week number" you need basically two informations:
On which day does your week start? This you provided, week begins on Sunday
Which week do you consider as first week of the year? This you did
not tell us.
The second definition may overrule the first one, e.g. "week 1 starts on the first day of the year and continues to the seventh day of the year." as used in Oracle TO_CHAR(..., 'WW')

Identify years with identical days of the week (e.g. 1994, 2005, 2011)

I have an idea for a story in which certain events happen repeatedly throughout the year of calendar dates that perfectly match each other for example 2011, 2005, 1994 one could replace these calendars with each other
I would like to be able to find calendar years past and future
If someone could help me please as I have no programming ability
Thanks in advance
Check Wikipedia.
You only need to compare the day of the week on which the first day of the year falls, and compare leap years and non-leap years separately (they will obviously differ). If the first day is the same and the days in the year are the same, so will the whole year be the same.
With this reduction, we need to first know what is a leap year. From wikipedia, the algorithm is
if year modulo 400 is 0
then is_leap_year
else if year modulo 100 is 0
then not_leap_year
else if year modulo 4 is 0
then is_leap_year
else
not_leap_year
Then we need to calculate the first day of the year. Before we start, we need a grounding. Let's take 2000, which starts on a Saturday. Every year we move forward, we move forward one day in the week except if the year follows a leap year in which case we move forward two days.
Let's walk through an example. 2000 starts on Saturday. 2001 starts on a Monday, 2 days later because 2000 is a leap year. 2002 starts on Tuesday. 2003 on a Wednesday. 2004 on a Thursday. 2005 on a Saturday, because 2004 is a leap year. From this we see that 2000 and 2005 start on the same day of the week, but the one is a leap year while the other is not. If we continue we'll find 2011 starts on a Saturday, and is therefore identical to 2005.
We can work backwards in similar fashion to find years in the past, or we can just choose an earlier starting year.
Dominical letter B: ... 1910 1921 1927 1938 1949 1955 1966 1977 1983 1994 2005 2011 2022 2033 2039 2050 2061 2067 2078 2089 2095 ...
Be careful about going back before 1752 and 1582, where 11 and 10 days were dropped from the calendar, respectively. see dataandtime.com. Otherwise there are exactly 14 possible calendars ... Half with a Feb 29th, half without. Each pair of calendars starts on a different day of the week. There should be an excel spreadsheet out there (its easy enough to generate one) that maps the years since 1752 into one of 14 columns.
2016 ends on the same days of the week as 1994, 2005 and 2011, but 2016 starts on Friday (leap year), while all the other years start on Saturday, therefore we need to wait until 2022 before the dates in 2011 match up again.

Resources