# Re: [R] Basic date time arithmetics operations

Fan xiao.gang.fan1 at libertysurf.fr
Mon Apr 28 22:46:43 CEST 2003

>>Also for the function seq.dates (or seq.POSIXt), the
>>case for by="months" would be more useful if it takes
>>the situation of the end of month into account.
>>For example,
>>
>>> seq(ISOdate(2002,1,31), by="months", length=3)
>>
>>"2002-01-31 13:00:00 CET"
>>"2002-03-03 13:00:00 CET"
>>"2002-03-31 13:00:00 CET"
>>
>>rather than
>>
>>"2002-01-31 13:00:00 CET"
>>"2002-02-28 13:00:00 CET"
>>"2002-03-31 13:00:00 CET"
>
>I don't think that's a well-defined request.  What would you want from
>
> seq(ISOdate(2002,12,30), by="months", length=5)
>
>Should it give February 27, since you asked for the day before the end
>of the month?  What should it do in April?
>
>I think the current behaviour is doing what it should doing.  If you
>really want the last day of the month in three successive months, then
>you should probably get a sequence of first days of months, and then
>subtract a day from each.  Wish I could tell you how to do that!
>Hopefully someone else will.
>
>By the way, I noticed a typo in the help for difftime() (it was
>missing an escape on one of the % signs, so the end of the line
>disappeared), and I've committed a fix to R-patched.
>
>Duncan Murdoch
>

1. When we shift or add/substract n month(s) from a date, n should be
refleted in the month level (or more precisely YYYYMM level) arithmetics
between the resulting date and the original date,

2. For the day of month level, some sort of alignment should be considered.

The simplest way would be like what's doing the Oracle SQL function
add_months(d,n): If d is the last day of the month or if the resulting month
has fewer days than the day component of d, then the result is the last day
of the resulting month. Otherwise, the result has the same day component as d.

In a time series specialized software like Fame, the function SHIFTMTH
seems having more sophisticated options.

Best
--
Fan

************ VITE, C'EST LE MOMENT DE VOUS ABONNER A L'ADSL !!! **********
Du 3 avril au 5 mai, série limitée ADSL 128K, cliquez vite ici : http://register.tiscali.fr/adsl/
Offre promotionnelle soumise à conditions, réservée aux nouveaux abonnés ADSL Tiscali.