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;








-1















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.










share|improve this 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

















-1















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.










share|improve this 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













-1












-1








-1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer

























  • 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















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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





share|improve this answer

























  • 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
















0














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





share|improve this answer

























  • 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














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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









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.





Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript