[R-gregmisc-users] SF.net SVN: r-gregmisc:[1308] trunk/gdata
Brought to you by:
warnes
From: <gg...@us...> - 2008-12-31 13:27:01
|
Revision: 1308 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1308&view=rev Author: ggorjan Date: 2008-12-31 13:26:51 +0000 (Wed, 31 Dec 2008) Log Message: ----------- New functions getYear, getMonth, getDay, getHour, getMin, and getSec for extracting the date/time parts from objects of a date/time class. Added Paths: ----------- trunk/gdata/R/getDateTimeParts.R trunk/gdata/inst/unitTests/runit.getDateTimeParts.R trunk/gdata/man/getDateTimePart.Rd Added: trunk/gdata/R/getDateTimeParts.R =================================================================== --- trunk/gdata/R/getDateTimeParts.R (rev 0) +++ trunk/gdata/R/getDateTimeParts.R 2008-12-31 13:26:51 UTC (rev 1308) @@ -0,0 +1,90 @@ +### getDateTimePart.R +###------------------------------------------------------------------------ +### What: Extract date and time parts from various date and time classes +### $Id$ +### Time-stamp: <2008-12-30 22:42:58 ggorjan> +###------------------------------------------------------------------------ + +### {{{ getYear +###------------------------------------------------------------------------ + +getYear <- function(x, format, ...) + UseMethod("getYear") + +getYear.default <- function(x, format, ...) + stop("'getYear' can only be used on objects of a date/time class") + +getYear.Date <- +getYear.POSIXct <- +getYear.POSIXlt <- function(x, format="%Y", ...) + format(x=x, format=format, ...) + +### }}} +### {{{ getMonth +###------------------------------------------------------------------------ + +getMonth <- function(x, format, ...) + UseMethod("getMonth") + +getMonth.default <- function(x, format, ...) + stop("'getMonth' can only be used on objects of a date/time class") + +getMonth.Date <- +getMonth.POSIXct <- +getMonth.POSIXlt <- function(x, format="%m", ...) + format(x=x, format=format) + +### }}} +### {{{ getDay +###------------------------------------------------------------------------ + +getDay <- function(x, format, ...) + UseMethod("getDay") + +getDay.default <- function(x, format, ...) + stop("'getDay' can only be used on objects of a date/time class") + +getDay.Date <- +getDay.POSIXct <- +getDay.POSIXlt <- function(x, format="%d", ...) + format(x=x, format=format) + +### }}} +### {{{ getHour +###------------------------------------------------------------------------ + +getHour <- function(x, format, ...) + UseMethod("getHour") + +getHour.default <- function(x, format, ...) + stop("'getHour' can only be used on objects of a date/time class") + +### }}} +### {{{ getMin +###------------------------------------------------------------------------ + +getMin <- function(x, format, ...) + UseMethod("getMin") + +getMin.default <- function(x, format, ...) + stop("'getMin' can only be used on objects of a date/time class") + +### }}} +### {{{ getSec +###------------------------------------------------------------------------ + +getSec <- function(x, format, ...) + UseMethod("getSec") + +getSec.default <- function(x, format, ...) + stop("'getSec' can only be used on objects of a date/time class") + +### }}} +### {{{ Dear Emacs +## Local variables: +## folded-file: t +## End: +### }}} + +###------------------------------------------------------------------------ +### getDateTimePart.R ends here \ No newline at end of file Property changes on: trunk/gdata/R/getDateTimeParts.R ___________________________________________________________________ Added: svn:keywords + Added: trunk/gdata/inst/unitTests/runit.getDateTimeParts.R =================================================================== --- trunk/gdata/inst/unitTests/runit.getDateTimeParts.R (rev 0) +++ trunk/gdata/inst/unitTests/runit.getDateTimeParts.R 2008-12-31 13:26:51 UTC (rev 1308) @@ -0,0 +1,121 @@ +### runit.getDateTimeParts.R +###------------------------------------------------------------------------ +### What: Extract date and time parts from ... - unit tests +### $Id$ +### Time-stamp: <2008-12-30 22:41:18 ggorjan> +###------------------------------------------------------------------------ + +### {{{ --- Test setup --- + +if(FALSE) { + library("RUnit") + library("gdata") +} + +num <- 1 +cha <- "a" +fac <- factor(c("A")) + +tYear <- as.character(c(2006, 1995, 1005, 3067)) +tMonth <- c("01", "04", "06", "12") +tDay <- c("01", "12", "22", "04") +tDate <- paste(tYear, tMonth, tDay, sep="-") + +tHour <- c("05", "16", "20", "03") +tMin <- c("16", "40", "06", "52") +tSec <- c("56", "34", "05", "15") +tTime <- paste(tHour, tMin, tSec, sep=":") + +# tDateTime <- paste() + +cDate <- as.Date(tDate) +cDatePOSIXct <- as.POSIXct(cDate) +cDatePOSIXlt <- as.POSIXlt(cDate) + +### }}} +### {{{ --- getYear --- + +test.getYear <- function() +{ + checkException(getYear(x=num)) + checkException(getYear(x=cha)) + checkException(getYear(x=fac)) + + checkIdentical(getYear(x=cDate), tYear) + checkIdentical(getYear(x=cDatePOSIXct), tYear) + checkIdentical(getYear(x=cDatePOSIXlt), tYear) +} + +### }}} +### {{{ --- getMonth --- + +test.getMonth <- function() +{ + checkException(getMonth(x=num)) + checkException(getMonth(x=cha)) + checkException(getMonth(x=fac)) + + checkIdentical(getMonth(x=cDate), tMonth) + checkIdentical(getMonth(x=cDatePOSIXct), tMonth) + checkIdentical(getMonth(x=cDatePOSIXlt), tMonth) +} + +### }}} +### {{{ --- getDay --- + +test.getDay <- function() +{ + checkException(getDay(x=num)) + checkException(getDay(x=cha)) + checkException(getDay(x=fac)) + + checkIdentical(getDay(x=cDate), tDay) + checkIdentical(getDay(x=cDatePOSIXct), tDay) + checkIdentical(getDay(x=cDatePOSIXlt), tDay) +} + +### }}} +### {{{ --- getHour --- + +test.getHour <- function() +{ + checkException(getHour(x=num)) + checkException(getHour(x=cha)) + checkException(getHour(x=fac)) + +## checkIdentical(getHour(x=cDate), tHour) +} + +### }}} +### {{{ --- getMin --- + +test.getMin <- function() +{ + checkException(getMin(x=num)) + checkException(getMin(x=cha)) + checkException(getMin(x=fac)) + +## checkIdentical(getMin(x=cDate), tMin) +} + +### }}} +### {{{ --- getSec --- + +test.getSec <- function() +{ + checkException(getSec(x=num)) + checkException(getSec(x=cha)) + checkException(getSec(x=fac)) + +## checkIdentical(getSec(x=cDate), tSec) +} + +### }}} +### {{{ Dear Emacs +### Local variables: +### folded-file: t +### end: +### }}} + +###------------------------------------------------------------------------ +### runit.getDateTimeParts.R ends here Property changes on: trunk/gdata/inst/unitTests/runit.getDateTimeParts.R ___________________________________________________________________ Added: svn:keywords + Added: trunk/gdata/man/getDateTimePart.Rd =================================================================== --- trunk/gdata/man/getDateTimePart.Rd (rev 0) +++ trunk/gdata/man/getDateTimePart.Rd 2008-12-31 13:26:51 UTC (rev 1308) @@ -0,0 +1,106 @@ +% getDateTimeParts.Rd +%-------------------------------------------------------------------------- +% What: Extract date and time parts from ... - help +% $Id$ +% Time-stamp: <2008-12-30 22:44:20 ggorjan> +%-------------------------------------------------------------------------- + +\name{getYear} + +\alias{getDateTimeParts} +\alias{getYear} +\alias{getYear.default} +\alias{getYear.Date} +\alias{getYear.POSIXct} +\alias{getYear.POSIXlt} + +\alias{getMonth} +\alias{getMonth.default} +\alias{getMonth.Date} +\alias{getMonth.POSIXct} +\alias{getMonth.POSIXlt} + +\alias{getDay} +\alias{getDay.default} +\alias{getDay.Date} +\alias{getDay.POSIXct} +\alias{getDay.POSIXlt} + +\alias{getHour} +\alias{getHour.default} + +\alias{getMin} +\alias{getMin.default} + +\alias{getSec} +\alias{getSec.default} + +\title{Get date/time parts from date and time objects} + +\description{get* functions provide an *experimental* approach for + extracting the date/time parts from objects of a date/time class. + They are designed to be intiutive and thus lowering the learning + curve for work with date and time classes in \R{}.} + +\usage{ + +getYear(x, format, \ldots) +getMonth(x, format, \ldots) +getDay(x, format, \ldots) +getHour(x, format, \ldots) +getMin(x, format, \ldots) +getSec(x, format, \ldots) + +} + + +\arguments{ + \item{x}{generic, date/time object} + \item{format}{character, format} + \item{\ldots}{arguments pased to other methods} +} + +\value{Character} + +\author{Gregor Gorjanc} + +\seealso{ + \code{\link{Date}}, + \code{\link{DateTimeClasses}}, + \code{\link{strptime}} +} + +\examples{ + +## --- Date class --- + +tmp <- Sys.Date() +tmp + +getYear(tmp) +getMonth(tmp) +getDay(tmp) + +## --- POSIXct class --- + +tmp <- as.POSIXct(tmp) + +getYear(tmp) +getMonth(tmp) +getDay(tmp) + +## --- POSIXlt class --- + +tmp <- as.POSIXlt(tmp) + +getYear(tmp) +getMonth(tmp) +getDay(tmp) + +} + +\keyword{manip} +\keyword{misc} + +%-------------------------------------------------------------------------- +% getDateTimeParts.Rd ends here \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |