Created
October 12, 2015 21:12
-
-
Save mwiegant/a4d59001fd4e56481455 to your computer and use it in GitHub Desktop.
C++, date string processing functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* NOTE: I am not 100% sure these functions work as intended. They may serve | |
* as a useful place to start though if a problem involving data parsing needs | |
* to be solved. | |
*/ | |
// -------------------------------------------- | |
// Constants | |
// -------------------------------------------- | |
const char JAN[4] = "JAN"; | |
const char FEB[4] = "FEB"; | |
const char MAR[4] = "MAR"; | |
const char APR[4] = "APR"; | |
const char MAY[4] = "MAY"; | |
const char JUN[4] = "JUN"; | |
const char JUL[4] = "JUL"; | |
const char AUG[4] = "AUG"; | |
const char SEP[4] = "SEP"; | |
const char OCT[4] = "OCT"; | |
const char NOV[4] = "NOV"; | |
const char DEC[4] = "DEC"; | |
const int ZERO = 0; | |
const int ONE = 1; | |
const int TWO = 2; | |
const int THREE = 3; | |
const int FOUR = 4; | |
const int FIVE = 5; | |
const int SIX = 6; | |
const int SEVEN = 7; | |
const int EIGHT = 8; | |
const int NINE = 9; | |
const int TEN = 10; | |
// -------------------------------------------- | |
// Function Prototypes | |
// -------------------------------------------- | |
int dayStringtoInt(char character); | |
int monthStringToInt(char letter1, char letter2, char letter3); | |
int yearStringtoInt(char letter1, char letter2, char letter3, char letter4); | |
// -------------------------------------------- | |
// Function Implementations | |
// -------------------------------------------- | |
/** | |
* @brief Converts a single character into a number | |
* | |
* @details Given a character, checks if it can be converted to an integer and | |
* converts in if possible. Returns '-1' if the character cannot be | |
* converted. | |
* | |
* @param character: the character to convert | |
* | |
* @returns int: the integer value of the character. Returns '-1' if the | |
* character cannot be converted into an integer | |
* | |
* @note | |
* . returns '-1' if the character cannot be converted into an integer | |
*/ | |
int dayStringtoInt(char character) | |
{ | |
int result; | |
if( character >= '0' && character <= '9' ) | |
{ | |
result = int(character - '0'); | |
} | |
else | |
{ | |
result = -1; | |
} | |
return result; | |
} | |
/** | |
* @brief Given 3 letters, attempts to determine the month | |
* | |
* @details Takes in 3 letters and tries to match them to a month. | |
* | |
* @param letter1: the first letter | |
* @param letter2: the second letter | |
* @param letter3: the third letter | |
* | |
* @returns int: returns an int representing the month | |
* | |
* @note None | |
*/ | |
int monthStringToInt(char letter1, char letter2, char letter3) | |
{ | |
char month[4] = { letter1, letter2, letter3, NULL_CHAR }; | |
int monthIndex = -1; | |
// compare the month string given to each month of the year. If the string | |
// matches up with a month, record the index of that month and return the val | |
if( strComp(month, JAN) == ONE ) | |
{ | |
monthIndex = 0; | |
} | |
else if( strComp(month, FEB) == ONE ) | |
{ | |
monthIndex = 1; | |
} | |
else if( strComp(month, MAR) == ONE ) | |
{ | |
monthIndex = 2; | |
} | |
else if( strComp(month, APR) == ONE ) | |
{ | |
monthIndex = 3; | |
} | |
else if( strComp(month, MAY) == ONE ) | |
{ | |
monthIndex = 4; | |
} | |
else if( strComp(month, JUN) == ONE ) | |
{ | |
monthIndex = 5; | |
} | |
else if( strComp(month, JUL) == ONE ) | |
{ | |
monthIndex = 6; | |
} | |
else if( strComp(month, AUG) == ONE ) | |
{ | |
monthIndex = 7; | |
} | |
else if( strComp(month, SEP) == ONE ) | |
{ | |
monthIndex = 8; | |
} | |
else if( strComp(month, OCT) == ONE ) | |
{ | |
monthIndex = 9; | |
} | |
else if( strComp(month, NOV) == ONE ) | |
{ | |
monthIndex = 10; | |
} | |
else if( strComp(month, DEC) == ONE ) | |
{ | |
monthIndex = 11; | |
} | |
else | |
{ | |
monthIndex = -1; | |
} | |
return monthIndex; | |
} | |
/** | |
* @brief Given 4 letters, converts them into an integer representing the year | |
* | |
* @details Takes in 4 letters and tries to match them to a month. | |
* | |
* @param letter1: the first letter | |
* @param letter2: the second letter | |
* @param letter3: the third letter | |
* @param letter4: the fourth letter | |
* | |
* @returns int: returns an int representing the year | |
* | |
* @note None | |
*/ | |
int yearStringtoInt(char letter1, char letter2, char letter3, | |
char letter4) | |
{ | |
char yearString[5] = { letter1, letter2, letter3, letter4, NULL_CHAR }; | |
int index, total = ZERO; | |
for( index = ZERO; index < FOUR; index++ ) | |
{ | |
total = total * TEN + int( yearString[index]) - '0'; | |
} | |
return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment