File: utils.R

package info (click to toggle)
chron 2.3-62-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ansic: 130; makefile: 2
file content (436 lines) | stat: -rw-r--r-- 12,738 bytes parent folder | download | duplicates (4)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
".Holidays" <-
    structure(.Data = c(8035, 8180, 8220, 8285, 8365, 8394),
              format = structure(.Data = "m/d/y", .Names = "dates"),
              origin = structure(.Data = c(1, 1, 1970),
              .Names = c("month", "day", "year")),
              class = c("dates", "times"),
              .Names = c("New Year's Day", "Memorial Day",
              "Independence Day", "Labor Day", "Thanksgiving",
              "Christmas"))
"day.abb" <-
    c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
"day.name" <-
    c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
      "Saturday")
"month.length"<-
    c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

"days"<-
function(x)
{
    if(!inherits(x, "dates"))
        x <- as.chron(x)
    d <- month.day.year(floor(as.numeric(x)), origin. = origin(x))$day
    ## use paste to avoid bug in ordered() as in beta release 8/92
    d <- ordered(paste(d), paste(1:31))
    d
}
"hours"<-
function(x)
{
    if(!inherits(x, "times"))
        x <- as.chron(x)        
    x <- as.numeric(x)
    sec <- round(24 * 3600 * abs(x - floor(x)))    
    hh <- sec %/% 3600
    hh
}
"minutes"<-
function(x)
{
    if(!inherits(x, "times"))
        x <- as.chron(x)        
    x <- as.numeric(x)
    sec <- round(24 * 3600 * abs(x - floor(x)))
    hh <- sec %/% 3600
    mm <- (sec - hh * 3600) %/% 60
    mm
}
"seconds"<-
function(x)
{
    if(!inherits(x, "times"))
        x <- as.chron(x)
    x <- as.numeric(x)
    sec <- round(24 * 3600 * abs(x - floor(x)))
    hh <- sec %/% 3600
    mm <- (sec - hh * 3600) %/% 60
    ss <- trunc(sec - hh * 3600 - 60 * mm)
    ss
}

"quarters.default"<-
function(x, abbreviate = TRUE)
{
    if(!inherits(x, "dates"))
        if((is.character(x) || is.numeric(x)))
            x <- chron(x)
        else return(NULL)
    v <- month.day.year(floor(as.numeric(x)))$month
    out <- (v - 1) %/% 3 + 1
    lbl <- if(abbreviate)
        c("1Q", "2Q", "3Q", "4Q")
    else
        c("I", "II", "III", "IV")
    out <- lbl[out]
    ordered(out, levels = lbl, labels = lbl)
}
"months.default"<-
function(x, abbreviate = TRUE)
{
    if(!inherits(x, "dates"))
        if((is.character(x) || is.numeric(x)))
            x <- chron(x)
        else return(NULL)
    out <- month.day.year(as.numeric(x), origin. = origin(x))$month
    lbl <- if(abbreviate) month.abb else month.name
    out <- lbl[out]
    ordered(out, levels = lbl, labels = lbl)
}

"weekdays.default" <-
function(x, abbreviate = TRUE)
{
    if(!inherits(x, "dates"))
        if((is.character(x) || is.numeric(x)))
            x <- chron(x)
        else stop("x must inherit from dates")
    v <- month.day.year(as.numeric(x), origin. = origin(x))
    out <- day.of.week(v$month, v$day, v$year) + 1
    lbl <- if(abbreviate) day.abb else day.name
    out <- lbl[out]
    ordered(out, levels = lbl, labels = lbl)
}

"years" <-
function(x)
{
    if(!inherits(x, "dates"))
        x <- as.chron(x)
    y <- month.day.year(as.numeric(x), origin. = origin(x))$year
    y <- ordered(y)
    y
}


"clock2frac" <-
function(str)
{
    h <- as.numeric(substring(str, 1, 2))
    m <- as.numeric(substring(str, 4, 5))
    w <- substring(str, 6, 7)
    if(any(h < 0, h > 12, m < 0, m > 59))
        stop("misspecified time")
    pm <- w == "pm" | w == "PM"
    h[pm] <- h[pm] + 12
    f <- (h * 3600 + m * 60)/(24 * 3600)
    f
}

"count.events" <-
function(x, by)
    table(cut(x, breaks = by))

"count.fields.str" <-
function(str, sep = "")
{
    n <- length(str)
    white.space <- missing(sep) || sep == ""
    .C(C_cnt_flds_str,
       strings = as.character(str),
       nstrings = as.integer(n),
       sep = as.character(sep),
       white.space = as.integer(white.space),
       counts = integer(n))$counts
}

"day.of.week" <-
function(month, day, year)
{
    ix <- year + trunc((month - 14)/12)
    jx <- (trunc((13 * (month + 10 - (month + 10) %/% 13 * 12) - 1)/5)
           + day + 77 + (5 * (ix - (ix %/% 100) * 100)) %/% 4
           + ix %/% 400 - (ix %/% 100) * 2)
    jx %% 7
}

"format<-" <-
function(x, ..., value)
    UseMethod("format<-")

"frac2clock" <-
function(f)
{
    sec.per.day <- 24 * 3600
    secs <- f * sec.per.day
    h <- secs %/% 3600
    m <- round((secs - h * 3600)/60, 0)
    i <- h >= 13
    h[i] <- h[i] - 12
    pm <- rep("am", length(f))
    i <- f > 0.5
    pm[i] <- "pm"
    m <- paste(m)
    i <- nchar(m) == 1
    m[i] <- paste("0", m[i], sep = "")
    h <- paste(h)
    i <- nchar(h) == 1
    h[i] <- paste("0", h[i], sep = "")
    paste(h, ":", m, pm, sep = "")
}


"is.holiday" <-
function(x, holidays)
{
    if(!inherits(x, "dates")) x <- as.chron(x)
    if(missing(holidays)) {
        if(exists(".Holidays"))
            holidays <- .Holidays
        else holidays <- NULL
    } else if (length(holidays) == 0) holidays <- NULL
    if (is.null(holidays)) return(rep(FALSE, length(x)))
    orig.x <- origin(x)
    if(!is.null(orig.h <- origin(holidays)) && any(orig.x != orig.h))
        origin(holidays) <- orig.x
    out <- match(floor(x), floor(holidays), 0)
    as.logical(out)
}

"is.weekend" <-
function(x)
{
    if(!inherits(x, "dates")) x <- as.chron(x)
    v <- month.day.year(as.numeric(x), origin. = origin(x))
    out <- day.of.week(v$month, v$day, v$year) + 1	
    ## recall out is between 1 (Sunday) and 7 (Saturday)
    out == 1 | out == 7
}

"julian.default" <-
function(x, d, y, origin., ...)
{
    only.origin <- all(missing(x), missing(d), missing(y))
    if(only.origin) x <- d <- y <- NULL	# return days since origin
    if(missing(origin.) || is.null(origin.))
        if(is.null(origin. <- getOption("chron.origin")))
            origin. <- c(month = 1, day = 1, year = 1970)
    nms <- names(d)
    xdy <- cbind(x, d, y)
    m <- c(origin.[1], xdy[, "x"])      # prepend month of new origin
    d <- c(origin.[2], xdy[, "d"])      # prepend day of new origin
    y <- c(origin.[3], xdy[, "y"])      # prepend year of new origin
    ##
    ## code from julian date in the S book (p.269)
    ##
    y <- y + ifelse(m > 2, 0, -1)
    m <- m + ifelse(m > 2, -3, 9)
    c <- y %/% 100
    ya <- y - 100 * c
    out <- ((146097 * c) %/% 4 + (1461 * ya) %/% 4
            + (153 * m + 2) %/% 5 + d + 1721119)
    ## now subtract the new origin from all dates
    if(!only.origin) {
        if(all(origin. == 0))
            out <- out[-1]
        else
            out <- out[-1] - out[1]
        ## orig according to S algorithm
    }
    names(out) <- nms
    out
}

"julian2mine" <-
function(x)
{
    v <- month.day.year(x)
    d <- as.character(v$day)
    i <- nchar(d) == 1
    d[i] <- paste("0", d[i], sep = "")
    paste(d, month.abb[v$month], v$year, sep = "")
}

"leap.year" <-
function(y)
{
    if(inherits(y, "dates"))
        y <- month.day.year(as.numeric(y), origin. = origin(y))$year
    y %% 4 == 0 & (y %% 100 != 0 | y %% 400 == 0)
}

"mine2julian" <-
function(str)
{
    d <- substring(str, 1, 2)
    m <- substring(str, 3, 5)
    y <- substring(str, 6, 9)
    m <- match(m, month.abb, nomatch = NA)
    julian(m, as.numeric(d), as.numeric(y))
}

"month.day.year" <-
function(jul, origin.)
{
    if (!inherits(jul, "dates")) jul <- as.chron(jul)
    if(missing(origin.) || is.null(origin.))
        if(is.null(origin. <- getOption("chron.origin")))
            origin. <- c(month = 1, day = 1, year = 1970)
    if(all(origin. == 0)) shift <- 0 else shift <- julian(origin. = origin.)
    ## relative origin
    ## "absolute" origin
    j <- as.integer(floor(jul)) + as.integer(shift)
    j <- j - 1721119
    y <- (4 * j - 1) %/% 146097
    j <- 4 * j - 1 - 146097 * y
    d <- j %/% 4
    j <- (4 * d + 3) %/% 1461
    d <- 4 * d + 3 - 1461 * j
    d <- (d + 4) %/% 4
    m <- (5 * d - 3) %/% 153
    d <- 5 * d - 3 - 153 * m
    d <- (d + 5) %/% 5
    y <- 100 * y + j
    y <- y + ifelse(m < 10, 0, 1)
    m <- m + ifelse(m < 10, 3, -9)
    list(month = m, day = d, year = y)
}

"my.axis" <-
function(x, simplify = TRUE, ...)
{
    ## put date labels in one line plus time lables on second line
    px <- pretty(x)
    xx <- chron(px, format = attr(x, "format"), origin. = origin(x))
    lbls <- format(xx, enclose = c("", ""), sep = "\n", simplify = simplify)
    axis(1, at = px, labels = lbls, ...)
    invisible(list(at = px, labels = lbls))
}

"origin" <-
function(x)
    attr(x, "origin")
"origin<-" <-
function(x, value)
{
    if (length(value) != 3 || any(is.na(value)))
        stop("origin must be a month, day, year vector")
    if (value[1] < 1 || value[1] > 12)
        stop("month out of range in origin")
    n <- month.length[value[1]] +
        as.numeric(value[1] == 2 && leap.year(value[3]))
    if (value[2] < 1 || value[2] > n)
        stop("day out of range in origin")
    cl <- class(x)
    class(x) <- NULL
    jval <- julian(value[1], value[2], value[3], origin. = c(0, 0, 0))	
    ## adjust days for new origin (new.x + new.o == old.x + old.o)
    if (!is.null(ox <- attr(x, "origin")))
        x <- x - jval + julian(ox[1], ox[2], ox[3], origin. = c(0, 0, 0))
    new.origin <- unlist(month.day.year(jval, origin. = c(0, 0, 0)))
    attr(x, "origin") <-
        structure(new.origin, names = c("month", "day", "year"))
    class(x) <- cl
    x
}

"parse.format" <-
function(format, year.abb = getOption("chron.year.abb"), ...)
{
    ## determine order of month, day, year or hour, min, secs
    abb <- TRUE                         # short notation?
    mon.abb <- FALSE                    # should month names be abbreviated?
    if(is.null(year.abb))
        year.abb <- TRUE
    if((nf <- nchar(format)) == 5) {
        ## abbreviated dates/times
        sep <- substring(format, 2, 2)
        fmt <- substring(format, first = c(1, 3, 5), last = c(1, 3, 5))
    }
    else if(nf == 3) {
        sep <- ""                       # no sep
        fmt <- substring(format, first = 1:3, last = 1:3)
    }
    else {
        ## full format (month names)
        abb <- FALSE
        sep <- gsub("^[[:alpha:]]+([^[:alpha:]]).*", "\\1", format)
        if(sep == format)
            stop(paste("unrecognized format", format))
        fmt <- unlist(unpaste(format, sep = sep))
        mon.abb <- if(any(fmt == "month")) FALSE else TRUE
    }
    periods <- substring(tolower(fmt), 1, 1) # m, d, & y in right order
    return(list(abb = abb, sep = sep, periods = periods, 
                mon.abb = mon.abb, year.abb = year.abb))
}

"unpaste" <-
function(str, sep = "/", fnames = NULL, nfields = NULL,
         first = c(1, 3, 5), width = 2)
{
    ## split str into fields separated by sep or by fiels specified by
    ## start positions and field widths; output a list 
    str <- as.character(str)
    nas <- is.na(str) | str == ""
    if(sep != "") {
        if(is.null(nfields)) {
            ## use a simple heuristic
            nf <- count.fields.str(str[!nas], sep = sep)
            cnt <- table(nf)
            nfields <- sort(unique(nf))[cnt == max(cnt)]
        }
        str[nas] <- paste(rep(NA, nfields), collapse = sep)
        nf <- count.fields.str(str, sep = sep)
        bad <- seq_along(str)[nf != nfields]
        if(n.bad <- length(bad)) {
            if(n.bad > 10)
                msg <- paste(n.bad, 
                             "entries set to NA",
                             "due to wrong number of fields")
            else msg <- paste(
                              "wrong number of fields in entry(ies)",
                              paste(bad, collapse = ", "))
            warning(msg)
            nas[bad] <- TRUE
            str[nas] <- paste(rep(NA, nfields), collapse = sep)
        }
        n <- length(str)
        white.space <- FALSE
        out <- .Call(C_unpaste,
                     as.character(str),
                     as.character(sep),
                     as.logical(white.space),
                     as.integer(nfields))
        for(i in seq_along(out))
            out[[i]][nas] <- as.character(NA)
    }
    else {
        last <- first + width - 1
        out <- vector("list", length = length(first))
        for(i in seq_along(first)) {
            out[[i]] <- substring(str, first[i], last[i])
            out[[i]][nas] <- as.character(NA)
        }
    }
    names(out) <- fnames
    return(out)
}

.str_to_ymd_list <-
function(str, fmt)
{
    str <- as.character(str)
    nas <- is.na(str) | str == ""
    periods <- fmt$periods
    widths <- cbind(y = nchar(str) - 4, m = 2, d = 2)
    last <- apply(widths[, fmt$periods, drop = FALSE], 1, cumsum)
    first <- rbind(0, last[-3, , drop = FALSE]) + 1
    out <- vector("list", length = 3)
    for(i in seq_along(periods)) {
        out[[i]] <- substring(str, first[i, ], last[i, ])
        out[[i]][nas] <- as.character(NA)
    }
    names(out) <- periods
    out
}