date - How to convert month to other duration measurement types? -


for duration-related calculations need convert values measured in "months" other formats, such years, days, or hours.

for example, proper way measure month in terms of days? 30 days? or 30.4375 days? (365.25 / 12) , format useful in cases?

if have information on casual/business use cases such conversions helpful too.

unfortunately, there's no single valid answer question.

if business use, first check whether there existing relevant standards or business practices define "month" means in business context. if yes, should follow definition closely possible, silly or awkward may seem.

for casual use, simplest solution pick use date manipulation library , whatever does. default behavior may not perfect, it's @ least close sensible compromise of many contradictory expectations users of such library may have.

ok, if insist on rolling own solution? in case, first choice should make how want represent date / time values. there @ least 2 common choices:

  1. the first option store dates / times using simple linear count of fixed time units given epoch, such julian days or unix timestamps. provides simple , compact date/time representation, makes comparing timestamps , simple date/time arithmetic (like adding n seconds time value) easy, , ensures time value corresponds (more or less) unique , defined point in time.

    the downside, you've noticed, arithmetic using "fuzzy" time units months or years gets difficult: can define year 365.25 days (or 365.2425 days, take account 97 out of every 400 years leap years in gregorian calendar) , month 1/12 years, cause adding year date-time value shift time of day (about) 6 hours, may unexpected.

    this approach doesn't let represent "floating" time value, times of day without specified date and time zone. (you can sort of deal floating time zones doing time math in utc , pretending it's in local time zone, can cause weird stuff happen around dst changeovers.) conversely, can cause difficulties if need represent imprecise date/time values, such dates without time component.

    in particular, if choose "natural" representation, imprecise datetimes represented starting point, e.g. unspecified time of day defaults 00:00:00.0, anything causes time part reduced fraction of second — like, say, shifting later time zone, or subtracting fuzzy time unit not integral number of days — flip date part previous day. example, representation, subtracting 1 year (= 265.2425 days) january 1, 2014 yield date in 2012 (specifically, december 31, 2012, 17:56:32)!

    you can avoid of these issues representing imprecise date/time values midpoints instead, e.g. date 2014 treated shorthand june 2, 2014, 12:00:00. lose, representation, ability build datetimes adding components: representation, 2014 + 5 months + 3 days isn't anywhere near may 3, 2014.

    also, when think you've at least got simple non-fuzzy time arithmetic unambiguously sorted out, someone's going tell leap seconds...

  2. the alternative approach store datetime values in decomposed year / month / day / hour / minute / second / etc. format. presentation, time intervals naturally stored in decomposed format: "one month + 17 days" is, in itself, valid time interval in such representation, , need not (and should not) simplified further.

    this has few obvious advantages:

    • fuzzy unit arithmetic (conceptually) simple: add 1 year date, increment year component one.

    • imprecise date/time values can naturally represented: pure date value, time-of-day components can left undefined (= e.g. represented negative values undefined components, or having each datetime value store precision).

    • you have precise control on when , if rollover occurs: adding year date in 2014 always yield date in 2015.

    • you can support floating time values, such times of day without specified date, or dates of year without specified year. floating time zones become supportable.

    that said, there disadvantages, too:

    • implementing date arithmetic gets more complex, since have deal non-trivial carry/borrow rules. (quick! what's date 10,000,000 seconds after may 3, 2014?)

    • you'll still have ambiguities month arithmetic: what's date 1 month after january 31? , depend on whether it's leap year or not?

    you can allow such format store "impossible" dates "february 31", optional method normalize them to, say, february 28 (or 29, leap year) later. has advantage of preserving (some) arithmetic consistency: allows (january 31 + 1 month) + 1 month equal march 31 expected.

    in ways, though merely postpones problem: presumably, january 31 + 24 hours should fall on february 1, day , month should january 31 + 1 month + 24 hours fall on? "obvious" choice march 1, whatever choose, there some sequence of arithmetic operations yield inconsistent results.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -