1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
# Copyrights (C)
# for this R-port:
# 1999 - Diethelm Wuertz, GPL
# 2007 - Rmetrics Foundation, GPL
# Diethelm Wuertz <wuertz@phys.ethz.ch>
# www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
# see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
# see Rmetrics's copyright file
################################################################################
# FUNCTION: DESCRIPTION:
# timeLastDayInMonth Computes the last day in a given month and year
# timeFirstDayInMonth Computes the first day in a given month and year
# timeLastDayInQuarter Computes the last day in a given quarter and year
# timeFirstDayInQuarter Computes the first day in a given quarter and year
################################################################################
timeLastDayInMonth <-
function(charvec, format = "%Y-%m-%d", zone = myFinCenter,
FinCenter = myFinCenter)
{
# A function implemented by Diethelm Wuertz
# Description:
# Computes the last day in a given month and year
# Arguments:
# charvec - a character vector of dates and times.
# format - the format specification of the input character vector.
# FinCenter - a character string with the the location of the
# financial center named as "continent/city".
# Value:
# Returns the last day in a given month and year as a
# 'timeDate' object.
# FUNCTION:
# Set Timezone to GMT:
myTZ = Sys.getenv("TZ")
Sys.setenv(TZ = "GMT")
if (FinCenter == "") FinCenter = "GMT"
# Last day of month:
last.day = c(31,28,31, 30,31,30, 31,31,30, 31,30,31)
lt = strptime(charvec, format)
y = 1900 + lt$year
leap.year = (y%%4 == 0 & (y%%100 != 0 | y%%400 == 0))
leap.day = as.integer(leap.year)*as.integer(lt$mon == 1)
lt$mday = last.day[1 + lt$mon] + leap.day
# Return Value:
Sys.setenv(TZ = myTZ)
timeDate(lt, format = "%Y-%m-%d", zone = zone, FinCenter = FinCenter)
}
# ------------------------------------------------------------------------------
timeFirstDayInMonth <-
function(charvec, format = "%Y-%m-%d", zone = myFinCenter,
FinCenter = myFinCenter)
{
# A function implemented by Diethelm Wuertz
# Description:
# Computes the last day in a given month and year
# Changes:
#
# FUNCTION:
# Set Timezone to GMT:
myTZ = Sys.getenv("TZ")
Sys.setenv(TZ = "GMT")
if (FinCenter == "") FinCenter = "GMT"
# First Day In Month:
lt = strptime(charvec, format)
lt$mday = 1
# Return Value:
Sys.setenv(TZ = myTZ)
timeDate(lt, format = "%Y-%m-%d", zone = zone, FinCenter = FinCenter)
}
# ------------------------------------------------------------------------------
timeLastDayInQuarter <-
function(charvec, format = "%Y-%m-%d", zone = myFinCenter,
FinCenter = myFinCenter)
{
# A function implemented by Diethelm Wuertz
# Description:
# Computes the last day in a given month and year
# FUNCTION:
# First Day in Month:
charvec = timeFirstDayInMonth(charvec = charvec, format = format,
FinCenter = FinCenter)
# Set Timezone to GMT:
myTZ = Sys.getenv("TZ")
Sys.setenv(TZ = "GMT")
if (FinCenter == "") FinCenter = "GMT"
# Last Day in Quarter:
lt = strptime(charvec, format)
last.quarter = rep(c(3,6,9,12), each = 3) - 1
lt$mon = last.quarter[1 + lt$mon]
Sys.setenv(TZ = myTZ)
charvec = timeDate(lt, format = "%Y-%m-%d", zone = zone,
FinCenter = FinCenter)
# Return Value:
Sys.setenv(TZ = myTZ)
timeLastDayInMonth(charvec = charvec, format = format,
zone = zone, FinCenter = FinCenter)
}
# ------------------------------------------------------------------------------
timeFirstDayInQuarter <-
function(charvec, format = "%Y-%m-%d", zone = myFinCenter,
FinCenter = myFinCenter)
{
# A function implemented by Diethelm Wuertz
# Description:
# Computes the last day in a given month and year
# Changes:
#
# FUNCTION:
# First Day in Month:
charvec = timeFirstDayInMonth(charvec =charvec, format = format,
FinCenter = FinCenter)
# Set Timezone to GMT:
myTZ = Sys.getenv("TZ")
Sys.setenv(TZ = "GMT")
if (FinCenter == "") FinCenter = "GMT"
# First Day in Quarter:
lt = strptime(charvec, format)
first.quarter = rep(c(1,4,7,10), each = 3) - 1
lt$mon = first.quarter[1 + lt$mon]
# Return Value:
Sys.setenv(TZ = myTZ)
timeDate(lt, format = "%Y-%m-%d", zone = zone, FinCenter = FinCenter)
}
################################################################################
|