## testing whether it is quicker to convert epoch time using date as factor or numeric

# 5. Insert & populate HumanDate into the df
chkp("Converting epoch to Human date", chkpOn)  #checkpoint
system.time(loopNum())
system.time(loopNum())



loopNum <- function(loops=20, funx=section5Numeric()) {
  for (i in 1:loops) {
    cat("i is ", i,"\n")
    section5Numeric()
  }
}

loopFac <- function(loops=20, funx=section5Factor()) {
  for (i in 1:loops) {
    cat("i is ", i,"\n")
    section5Factor()
  }
}



###################

section5Factor <- function() {

#backup artistTable (so it can be reused in next trial)
atb <- artistTable
cat("inside Fac\n")

  # convert date field to factor
  artistTable$Day <- as.factor(artistTable$Day)

  # create a column in the df for date
  artistTable <- data.frame(artistTable[,1:2], "Date"=as.character(NA), artistTable[,3:ncol(artistTable)])
  artistTable$Date <- as.character(artistTable$Date)

  # insert the corresponding row into metadf
  metadf <- rbind(metadf[1:2,], "Date"=c(getCMT(artistTable[,3]), "character",FALSE), metadf[3:nrow(metadf),])

  # convert epochday to human, and insert into df
  # epochDaysInDf <- as.integer(artistTable$Day)   # if using FACTOR for $Day, then use:  epochDaysInDf <- as.integer(levels(artistTable$Day))
  epochDaysInDf <- as.integer(levels(artistTable$Day))
  for (eD in epochDaysInDf) {
    artistTable$Date[artistTable$Day==eD & !is.na(artistTable$Day)] <- getHumanDateFromEpochDAY(eD)
  }

  # convert date field to factor
  artistTable$Day <- as.character(artistTable$Day)


#restore artistTable
artistTable <- atb
}

###########

section5Numeric <- function() {

#backup artistTable (so it can be reused in next trial)
atb <- artistTable
cat("inside Num\n")

  # create a column in the df for date
  artistTable <- data.frame(artistTable[,1:2], "Date"=as.character(NA), artistTable[,3:ncol(artistTable)])
  artistTable$Date <- as.character(artistTable$Date)

  # insert the corresponding row into metadf
  metadf <- rbind(metadf[1:2,], "Date"=c(getCMT(artistTable[,3]), "character",FALSE), metadf[3:nrow(metadf),])

  # convert epochday to human, and insert into df
  epochDaysInDf <- as.integer(artistTable$Day)   # if using FACTOR for $Day, then use:  epochDaysInDf <- as.integer(levels(artistTable$Day))
  for (eD in epochDaysInDf) {
    artistTable$Date[artistTable$Day==eD & !is.na(artistTable$Day)] <- getHumanDateFromEpochDAY(eD)
  }


#restore artistTable
artistTable <- atb

}