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