How to calculate a 4-week period of the billing year [closed]How to generate a random string in RubyHow to get a random number in RubyHow to pass command line arguments to a rake taskHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyHow to make --no-ri --no-rdoc the default for gem install?How do I get the current absolute URL in Ruby on Rails?How to write to file in Ruby?How to check whether a string contains a substring in Ruby?How to install a specific version of a ruby gem?
What should we do with manuals from the 80s?
(A room / an office) where an artist works
Why aren’t there water shutoff valves for each room?
What is the hottest thing in the universe?
What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?
Does an Irish VISA WARNING count as "refused entry at the border of any country other than the UK?"
Global BGP Routing only by only importing supernet prefixes
Why is there a dummy union member in some implemetations of std::optional?
Is it possible to know the exact chord from the roman numerals
How was the murder committed?
Would the USA be eligible to join the European Union?
Doesn't the speed of light limit imply the same electron can be annihilated twice?
Shifting tenses in the middle of narration
Why is the second S silent in "Sens dessus dessous"?
Will using a resistor in series with a LED to control its voltage increase the total energy expenditure?
Why does Japan use the same type of AC power outlet as the US?
How can God warn people of the upcoming rapture without disrupting society?
Identifying My Main Water Shutoff Valve / Setup
Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?
How can I communicate my issues with a potential date's pushy behavior?
A trip to the library
Do I have to cite common CS algorithms?
Help, I cannot decide when to start the story
Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?
How to calculate a 4-week period of the billing year [closed]
How to generate a random string in RubyHow to get a random number in RubyHow to pass command line arguments to a rake taskHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyHow to make --no-ri --no-rdoc the default for gem install?How do I get the current absolute URL in Ruby on Rails?How to write to file in Ruby?How to check whether a string contains a substring in Ruby?How to install a specific version of a ruby gem?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm refactoring a method that should return 4-week periods. A period is calculated as the number of 4-weekly blocks in the current billing year. There are thirteen periods per billing year. First billing year began on 24/08/2008.
I have created a formula:
def period
period = (((self.utc.to_time - Time.new(2008,8,23,0,0,0,0)) / 60 / 60 / 24 / 7 / 52).modulo(1) * 13).ceil
period == 0 ? 13 : period
end
Over the years, it has become inaccurate, and we're now seeing a shift of a whole period: bookings in Period 1 are showing as Period 2. I tried re-calculating the formula with no success. I feel the formula is too complex anyway, and can probably be achieved more simply.
ruby
closed as unclear what you're asking by sawa, Umair, cegfault, Henry Woody, Adil B Mar 27 at 16:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm refactoring a method that should return 4-week periods. A period is calculated as the number of 4-weekly blocks in the current billing year. There are thirteen periods per billing year. First billing year began on 24/08/2008.
I have created a formula:
def period
period = (((self.utc.to_time - Time.new(2008,8,23,0,0,0,0)) / 60 / 60 / 24 / 7 / 52).modulo(1) * 13).ceil
period == 0 ? 13 : period
end
Over the years, it has become inaccurate, and we're now seeing a shift of a whole period: bookings in Period 1 are showing as Period 2. I tried re-calculating the formula with no success. I feel the formula is too complex anyway, and can probably be achieved more simply.
ruby
closed as unclear what you're asking by sawa, Umair, cegfault, Henry Woody, Adil B Mar 27 at 16:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
1
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26
add a comment |
I'm refactoring a method that should return 4-week periods. A period is calculated as the number of 4-weekly blocks in the current billing year. There are thirteen periods per billing year. First billing year began on 24/08/2008.
I have created a formula:
def period
period = (((self.utc.to_time - Time.new(2008,8,23,0,0,0,0)) / 60 / 60 / 24 / 7 / 52).modulo(1) * 13).ceil
period == 0 ? 13 : period
end
Over the years, it has become inaccurate, and we're now seeing a shift of a whole period: bookings in Period 1 are showing as Period 2. I tried re-calculating the formula with no success. I feel the formula is too complex anyway, and can probably be achieved more simply.
ruby
I'm refactoring a method that should return 4-week periods. A period is calculated as the number of 4-weekly blocks in the current billing year. There are thirteen periods per billing year. First billing year began on 24/08/2008.
I have created a formula:
def period
period = (((self.utc.to_time - Time.new(2008,8,23,0,0,0,0)) / 60 / 60 / 24 / 7 / 52).modulo(1) * 13).ceil
period == 0 ? 13 : period
end
Over the years, it has become inaccurate, and we're now seeing a shift of a whole period: bookings in Period 1 are showing as Period 2. I tried re-calculating the formula with no success. I feel the formula is too complex anyway, and can probably be achieved more simply.
ruby
ruby
edited Mar 27 at 11:36
sawa
135k31 gold badges220 silver badges317 bronze badges
135k31 gold badges220 silver badges317 bronze badges
asked Mar 27 at 10:58
Peter FealeyPeter Fealey
34 bronze badges
34 bronze badges
closed as unclear what you're asking by sawa, Umair, cegfault, Henry Woody, Adil B Mar 27 at 16:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by sawa, Umair, cegfault, Henry Woody, Adil B Mar 27 at 16:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by sawa, Umair, cegfault, Henry Woody, Adil B Mar 27 at 16:42
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
1
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26
add a comment |
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
1
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
1
1
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26
add a comment |
1 Answer
1
active
oldest
votes
I would just use Date
instead of Time
and the calculation would be much easier.
DAYS_PER_PERIOD = 28
def period
days = Date.today - Date.new(2008, 8, 23)
periods = days / DAYS_PER_PERIOD
periods.to_i # would return a `Rational` instead of an `Integer` otherwise
end
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would just use Date
instead of Time
and the calculation would be much easier.
DAYS_PER_PERIOD = 28
def period
days = Date.today - Date.new(2008, 8, 23)
periods = days / DAYS_PER_PERIOD
periods.to_i # would return a `Rational` instead of an `Integer` otherwise
end
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
add a comment |
I would just use Date
instead of Time
and the calculation would be much easier.
DAYS_PER_PERIOD = 28
def period
days = Date.today - Date.new(2008, 8, 23)
periods = days / DAYS_PER_PERIOD
periods.to_i # would return a `Rational` instead of an `Integer` otherwise
end
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
add a comment |
I would just use Date
instead of Time
and the calculation would be much easier.
DAYS_PER_PERIOD = 28
def period
days = Date.today - Date.new(2008, 8, 23)
periods = days / DAYS_PER_PERIOD
periods.to_i # would return a `Rational` instead of an `Integer` otherwise
end
I would just use Date
instead of Time
and the calculation would be much easier.
DAYS_PER_PERIOD = 28
def period
days = Date.today - Date.new(2008, 8, 23)
periods = days / DAYS_PER_PERIOD
periods.to_i # would return a `Rational` instead of an `Integer` otherwise
end
answered Mar 27 at 11:32
spickermannspickermann
65.4k7 gold badges63 silver badges86 bronze badges
65.4k7 gold badges63 silver badges86 bronze badges
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
add a comment |
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
This is great, thanks. What the client threw in was that at some point in the last 11 years they changed the way they calculated periods and added an extra week somewhere. With a change to the start date (2008, 8, 23) to one after the extra week was thrown in, and the code above, we're good!
– Peter Fealey
Mar 27 at 15:25
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Define 4-week period. What day of week does a week start on? How are the first and the last week of a (billing) year defined?
– sawa
Mar 27 at 11:30
1
This would be a good example to provide some assertions/specs for, along with the additional rules (What about leap years? And the extra day at the end of the year?)
– Jay Dorsey
Mar 27 at 12:26