#include "headers.h"

void parser(char *buff, struct tm *timeptr){
  /* We want to put the parts of the time into the correct fields of the struct */
  /* Also keep the date the same unless it was entered */
  int token;
  int another_tok;
  int i;
  int j = 0;
  char *tempbuff;
  char temp;
  const char delimiter = ':';
  const char another_del = ' ';
  const char yet_another_del = '/';
  int length = strlen(buff);
  /* Pseudo-Parse */
  tempbuff = strtok(buff, &delimiter);
  temp = tempbuff[0] - 48;
  token = (int)temp;
  token *= 10;
  temp = tempbuff[1] - 48;
  token += (int)temp;
  timeptr->tm_hour = token;
  
  tempbuff = strtok(NULL, &delimiter);
  temp = tempbuff[0] - 48;
  token = (int)temp;
  token *= 10;
  temp = tempbuff[1] - 48;
  token += (int)temp;
  timeptr->tm_min = token;
  
  tempbuff = strtok(NULL, &another_del);
  temp = tempbuff[0] - 48;
  token = (int)temp;
  token *= 10;
  temp = tempbuff[1] - 48;
  token += (int)temp;
  timeptr->tm_sec = token;
  
  if(length > 8){
    /* We have a date as well */
    tempbuff = strtok(NULL, &yet_another_del);
    temp = tempbuff[0] - 48;
    token = (int)temp;
    token *= 10;
    temp = tempbuff[1] - 48;
    token += (int)temp;
    timeptr->tm_mon = token - 1;
    
    tempbuff = strtok(NULL, &yet_another_del);
    temp = tempbuff[0] - 48;
    token = (int)temp;
    token *= 10;
    temp = tempbuff[1] - 48;
    token += (int)temp;
    timeptr->tm_mday = token;
    
    tempbuff = strtok(NULL, &yet_another_del);
    temp = tempbuff[0] - 48;
    token = (int)temp;
    token *= 1000;
    temp = tempbuff[1] - 48;
    another_tok = (int)temp;
    another_tok *= 100;
    token += another_tok;
    temp = tempbuff[2] - 48;
    another_tok = (int)temp;
    another_tok *= 10;
    token += another_tok;
    temp = tempbuff[3] - 48;
    token += (int)temp;
    timeptr->tm_year = token - 1900;
  }
}