[R] converting a time to nearest half-hour

Allan Engelhardt allane at cybaea.com
Fri Jul 23 18:33:05 CEST 2010


The arithmetic that David describes should work fine (POSIXct is 
internally in UTC) unless you are in the Chatham Islands (which has a 
UTC+12:45 time zone [1]) or Nepal (UTC+05:45 [2]) or some such place 
with a funny idea about when the 1/2 hour mark is.


The formatting of the output may be tricky, if the original poster 
really want the (past/future) time zone as opposed to the current one, 
which is what his text could be read to suggest.

Good test cases would be "2010-03-28 01:46:00 GMT" which should 
(presumably) round to "2010-03-28 03:00:00 BST" and "2010-10-31 02:46 
BST" which probably rounds to "2010-10-31 02:00 GMT".  I can't quite get 
it to work in R, but it should be possible....


Allan

[1] https://secure.wikimedia.org/wikipedia/en/wiki/UTC%2B12:45
[2] https://secure.wikimedia.org/wikipedia/en/wiki/UTC%2B05:45

On 23/07/10 16:35, David Winsemius wrote:
>
> On Jul 23, 2010, at 11:20 AM, <Murali.Menon at avivainvestors.com> 
> <Murali.Menon at avivainvestors.com> wrote:
>
>> Hi folks,
>>
>> I've got a POSIXct datum as follows:
>>
>>> Sys.time()
>> [1] "2010-07-23 11:29:59 BST"
>>
>> I want to convert this to the nearest half-hour, i.e., to "2010-07-23 
>> 11:30:00 BST"
>>
>> (If the time were "11:59:ss", I want to convert to "12:00:00").
>>
>> How to achieve this?
>
> Couldn't you just coerce to numeric, divide by 60(sec)*30(half-hour 
> minutes), round to integer, multiply by 60*30,  coerce to POSIXct?
>
> _
> David Winsemius, MD
> West Hartford, CT
>



More information about the R-help mailing list