1context("Stamp") 2 3test_that("stamp selects the correct format", { 4 5 test_dates <- read.table(header = T, stringsAsFactors=F, 6 textConnection(" 7 date expected 8 'February 20th 1973' 'August 13th 2012' 9 ## 'february 14, 2004' 'August 13, 2012' 10 'Sunday, May 1, 2000' 'Monday, Aug 13, 2012' 11 'Sunday, May 1, 2000' 'Monday, Aug 13, 2012' 12 'february 14, 04' 'August 13, 12' 13 'Feb 20th 73' 'Aug 13th 12' 14 'January 5 1999 at 7pm' 'August 13 2012 at 11AM' 15 'jan 3 2010' 'Aug 13 2012' 16 'Jan 1, 1999' 'Aug 13, 2012' 17 'jan 3 10' 'Aug 13 12' 18 '01 3 2010' '08 13 2012' 19 '1 3 10' '08 13 12' 20 '1 13 89' '08 13 12' 21 '5/27/1979' '08/13/2012' 22 '12/31/99' '08/13/12' 23 'DOB:12/11/00' 'DOB:08/13/12' 24 'Thu, 1 July 2004 22:30:00' 'Mon, 13 August 2012 11:37:53' 25 'Thu, 1st of July 2004 at 22:30:00' 'Mon, 13st of August 2012 at 11:37:53' 26 'Thu, 1July 2004 at 22:30:00' 'Mon, 13August 2012 at 11:37:53' 27 'Thu, 1July2004 22:30:00' 'Mon, 13August2012 11:37:53' 28 ## '21 Aug 2011, 11:15:34 pm' '13 Aug 2012, 11:37:53 AM' 29 '1979-05-27 05:00:59' '2012-08-13 11:37:53' 30 '1979-05-27' '2012-08-13' 31 '3 jan 2000' '13 Aug 2012' 32 '17 april 85' '13 August 12' 33 '27/5/1979' '13/08/2012' 34 '20 01 89' '13 08 12' 35 '00/13/10' '12/13/08' 36 '14 12 00' '13 08 12' 37 ## '03:23:22 PM' '11:37:53 AM' 38 '2001-12-31T04:05:06Z' '2012-08-13T11:37:53Z' 39 ")) 40 41 D <- as.POSIXct("2012-08-13 11:37:53", tz = "UTC") 42 for (i in seq_along(test_dates$date)) { 43 ## print(i) 44 expect_equal(stamp(test_dates[[i, "date"]])(D), test_dates[[i, "expected"]]) 45 } 46 47}) 48 49test_that(".format_offset works as expected", { 50 51 df_winter <- data.frame( 52 tz = c("America/Chicago", "UTC", "Europe/Paris"), 53 Oo = c("-06", "+00", "+01"), 54 Oz = c("-0600", "+0000", "+0100"), 55 OO = c("-06:00", "+00:00", "+01:00"), 56 stringsAsFactors = FALSE 57 ) 58 59 with(df_winter, 60 for (i in 1:nrow(df_winter)) { 61 expect_equal(.format_offset(ymd("2013-01-01", tz = tz[i]), "%Oo"), Oo[i]) 62 expect_equal(.format_offset(ymd("2013-01-01", tz = tz[i]), "%Oz"), Oz[i]) 63 expect_equal(.format_offset(ymd("2013-01-01", tz = tz[i]), "%OO"), OO[i]) 64 }) 65 66 df_summer <- data.frame( 67 tz = c("America/Chicago", "UTC", "Europe/Paris"), 68 Oo = c("-05", "+00", "+02"), 69 Oz = c("-0500", "+0000", "+0200"), 70 OO = c("-05:00", "+00:00", "+02:00"), 71 stringsAsFactors = FALSE 72 ) 73 74 with(df_summer, 75 for (i in 1:nrow(df_summer)) { 76 expect_equal(.format_offset(ymd("2013-07-01", tz = tz[i]), "%Oo"), Oo[i]) 77 expect_equal(.format_offset(ymd("2013-07-01", tz = tz[i]), "%Oz"), Oz[i]) 78 expect_equal(.format_offset(ymd("2013-07-01", tz = tz[i]), "%OO"), OO[i]) 79 }) 80 81 ## half-hour timezone 82 expect_warning(.format_offset(ymd("2013-07-01", tz = "Asia/Kolkata"), "%Oo")) 83 expect_equal(suppressWarnings(.format_offset(ymd("2013-07-01", tz = "Asia/Kolkata"), "%Oo")), "+0530") 84 expect_equal(.format_offset(ymd("2013-07-01", tz = "Asia/Kolkata"), "%Oz"), "+0530") 85 expect_equal(.format_offset(ymd("2013-07-01", tz = "Asia/Kolkata"), "%OO"), "+05:30") 86}) 87 88test_that("stamp works with ISO-8601 formats", { 89 90 stamp_Ou <- stamp("2013-01-01T06:00:00Z") 91 stamp_Oo <- stamp("2013-01-01T00:00:00-06") 92 stamp_Oz <- stamp("2013-01-01T00:00:00-0600") 93 stamp_OO <- stamp("2013-01-01T00:00:00-06:00") 94 95 96 tz <- c("America/Chicago", "UTC", "Europe/Paris") 97 Ou <- c("2013-01-01T06:00:00Z", "2013-01-01T00:00:00Z", "2012-12-31T23:00:00Z") 98 Oo <- c("2013-01-01T00:00:00-06", "2013-01-01T00:00:00+00", "2013-01-01T00:00:00+01") 99 Oz <- c("2013-01-01T00:00:00-0600", "2013-01-01T00:00:00+0000", "2013-01-01T00:00:00+0100") 100 OO <- c("2013-01-01T00:00:00-06:00", "2013-01-01T00:00:00+00:00", "2013-01-01T00:00:00+01:00") 101 102 ## cbind(tz, Ou, Oo, Oz, OO) # if you want to see them 103 104 for (i in seq_along(tz)) { 105 expect_equal(stamp_Ou(ymd("2013-01-01", tz = tz[i])), Ou[i]) 106 expect_equal(stamp_Oo(ymd("2013-01-01", tz = tz[i])), Oo[i]) 107 expect_equal(stamp_Oz(ymd("2013-01-01", tz = tz[i])), Oz[i]) 108 expect_equal(stamp_OO(ymd("2013-01-01", tz = tz[i])), OO[i]) 109 } 110 111 ## half-hour timezone 112 expect_equal(suppressWarnings(stamp_Ou(ymd("2013-01-01", tz = "Asia/Kolkata"))), 113 "2012-12-31T18:30:00Z") 114 expect_warning(stamp_Oo(ymd("2013-01-01", tz = "Asia/Kolkata"))) 115 expect_equal(suppressWarnings(stamp_Oo(ymd("2013-01-01", tz = "Asia/Kolkata"))), 116 "2013-01-01T00:00:00+0530") 117 expect_equal(stamp_Oz(ymd("2013-01-01", tz = "Asia/Kolkata")), 118 "2013-01-01T00:00:00+0530") 119 expect_equal(stamp_OO(ymd("2013-01-01", tz = "Asia/Kolkata")), 120 "2013-01-01T00:00:00+05:30") 121 122 ## vectorization 123 expect_equal(stamp_OO(ymd(c("2013-01-01", "2010-01-01"), tz = "Asia/Kolkata")), 124 c("2013-01-01T00:00:00+05:30", "2010-01-01T00:00:00+05:30")) 125 126 ## format not at end of template (fails on windows 7, %z output format is 127 ## completely screwed there) 128 129 ## stamp_OO_strange <- stamp("2013-01-01T00:00:00-06:00 KK") 130 131 ## expect_equal(stamp_OO_strange(ymd("2013-01-01", tz="Asia/Kolkata")), 132 ## "2012-12-31T18:30:00+0000 KK") 133}) 134 135test_that("stamp works with missing date-times", { 136 f <- stamp(c("2019-26-04", NA_character_), quiet = TRUE) 137 dt <- ymd_hms("2000-01-02 00:00:00") 138 expect_equal(f(dt), "2000-02-01") 139}) 140 141test_that("stamp recognizes correctly B orders", { 142 formater <- stamp("Sunday, November 30, 23:15", "ABdHM", quiet = TRUE) 143 x <- ymd_hm(c("2017-01-20 15:15", "2017-02-11 10:10")) 144 expect_equal(formater(x), c("Friday, January 20, 15:15", "Saturday, February 11, 10:10")) 145}) 146 147 148 149## ## Don't delete this. We need it for interactive testing 150## y <- c('February 20th 1973', 151## "february 14, 2004", 152## "Sunday, May 1, 2000", 153## "Sunday, May012000", 154## "february 14, 04", 155## 'Feb 20th 73', 156## "January 5 1999 at 7pm", 157## "jan 3 2010", 158## "Jan 1, 1999", 159## "jan 3 10", 160## "01 3 2010", 161## "1 3 10", 162## '1 13 89', 163## "5/27/1979", 164## "12/31/99", 165## "DOB:12/11/00", 166## 'Thu, 1 July 2004 22:30:00', 167## 'Thu, 1st of July 2004 at 22:30:00', 168## 'Thu, 1July 2004 at 22:30:00', 169## 'Thu, 1July2004 22:30:00', 170## "21 Aug 2011, 11:15:34 pm", 171## "1979-05-27 05:00:59", 172## "1979-05-27", 173## "3 jan 2000", 174## "17 april 85", 175## "27/5/1979", 176## '20 01 89', 177## '00/13/10', 178## "14 12 00", 179## "03:23:22 pm") 180 181## cbind(y, unlist(lapply(y, function(x) stamp(x)(D)))) 182