[R] date calculation

Joshua Wiley jwiley.psych at gmail.com
Sat Oct 30 08:23:53 CEST 2010


On Fri, Oct 29, 2010 at 11:02 PM, Shi, Tao <shidaxia at yahoo.com> wrote:
> Hi Ben,
>
> That must be the case!  In fact if I do:
>
>> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="days", tz="GMT")
> Time difference of 195 days
>
>
> which supports your claim.
>
> Can someone from the R development team confirm this?

Combining empirical results with the documentation (you can read more
details about how timezones and dates are handled at ?DateTimeClasses
) which states that dst is used for relevant timezones (although this
behavior can be OS dependent), this is not really necessary.

For example, note that if one goes to a time before daylight savings:

> difftime(strptime("24NOV1902", format="%d%b%Y"), strptime("13MAY1902",format="%d%b%Y"), units="days")
Time difference of 195 days

and that the hours match exactly what one would expect given dst.

> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",format="%d%b%Y"), units="hours")
Time difference of 4681 hours
> 195 * 24 + 1
[1] 4681


In short, difftime() is performing as stated and documented (if
slightly unexpected at first glance).

Cheers,

Josh

>
> Thanks!
>
> ...Tao
>
>
>
>
>
> ----- Original Message -----
>> From:Ben Bolker <bbolker at gmail.com>
>> To:r-help at stat.math.ethz.ch
>> Cc:
>> Sent:Friday, October 29, 2010 7:54:53 PM
>> Subject:Re: [R] date calculation
>>
>>
>> Shi, Tao <shidaxia <at>
>> href="http://yahoo.com">yahoo.com> writes:
>
>> Could someone
>> explain to me why the following result is not a integer?
>>
>> >
>> difftime(strptime("24NOV2004", format="%d%b%Y"), strptime("13MAY2004",
>>
>> >format="%d%b%Y"), units="days")
>> Time difference of 195.0417
>> days
>
>  Presumably because this goes across a daylight-savings
>> time
> adjustment?  0.0417=1/24 days is 1 hour ...
>
>  Ben
>> Bolker
>
> ______________________________________________
>
>> ymailto="mailto:R-help at r-project.org"
>> href="mailto:R-help at r-project.org">R-help at r-project.org mailing list
>
>> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank
>> >https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting
>> guide http://www.R-project.org/posting-guide.html
> and provide commented,
>> minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list