<p>Given a <code>date</code> string in the form&nbsp;<code>Day Month Year</code>, where:</p>

<ul>
	<li><code>Day</code>&nbsp;is in the set <code>{&quot;1st&quot;, &quot;2nd&quot;, &quot;3rd&quot;, &quot;4th&quot;, ..., &quot;30th&quot;, &quot;31st&quot;}</code>.</li>
	<li><code>Month</code>&nbsp;is in the set <code>{&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, &quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;, &quot;Jul&quot;, &quot;Aug&quot;, &quot;Sep&quot;, &quot;Oct&quot;, &quot;Nov&quot;, &quot;Dec&quot;}</code>.</li>
	<li><code>Year</code>&nbsp;is in the range <code>[1900, 2100]</code>.</li>
</ul>

<p>Convert the date string to the format <code>YYYY-MM-DD</code>, where:</p>

<ul>
	<li><code>YYYY</code> denotes the 4 digit year.</li>
	<li><code>MM</code> denotes the 2 digit month.</li>
	<li><code>DD</code> denotes the 2 digit day.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> date = &quot;20th Oct 2052&quot;
<strong>Output:</strong> &quot;2052-10-20&quot;
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> date = &quot;6th Jun 1933&quot;
<strong>Output:</strong> &quot;1933-06-06&quot;
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> date = &quot;26th May 1960&quot;
<strong>Output:</strong> &quot;1960-05-26&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The given dates are guaranteed to be valid, so no error handling is necessary.</li>
</ul>

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

class Solution {
public:
    string reformatDate(string date) {
        // ---- MONTH FORMATTING ----
        // Dictionary to map the month string to the number.
        map<string, int> months = {
            {"Jan", 1}, {"Feb", 2}, {"Mar", 3}, {"Apr", 4},
            {"May", 5}, {"Jun", 6}, {"Jul", 7}, {"Aug", 8},
            {"Sep", 9}, {"Oct", 10}, {"Nov", 11}, {"Dec", 12}};

        // Finding the first space to determine where the month is in the string.
        size_t firstSpacePos = date.find(' ');

        // Assuming the month abbreviation is 3 characters, this gets the month abbreviation string using substr.
        string monthAbbreviation = date.substr(firstSpacePos + 1, 3);

        // This uses the dictionary to convert the abbreviation to the number linked in the key.
        int monthNumber = months[monthAbbreviation];

        // This converts the month to a string.
        string month = std::to_string(monthNumber);

        // This checks the length of the month string, and if it isn't two digits, 
        // it prefixes it with a '0' to fit the  date format.
        if(month.length() == 1)
        {
            month.insert(0, "0");
        }

        // ---- DAY FORMATTING ----
        // All of the day suffixes to help us find where the day is within the date string.
        string daySuffix1 = "th";
        string daySuffix2 = "nd";
        string daySuffix3 = "rd";
        string daySuffix4 = "st";

        // Using size_t incase a substring isn't found
        size_t prefix1 = date.find(daySuffix1);
        size_t prefix2 = date.find(daySuffix2);
        size_t prefix3 = date.find(daySuffix3);
        size_t prefix4 = date.find(daySuffix4);

        // We use the min function to find which prefix has a valid value within it,
        // as when date.find doesn't work it returns a value of 'std::string::npos' which is a very large number.
        size_t dayPos = min({prefix1, prefix2, prefix3, prefix4});

        // This changes the day to a string so it can be used.
        string day = date.substr(0, dayPos);

        // This suffixes 0 at the beginning of the day if it is one digit.
        if(day.length() == 1)
        {
            day.insert(0, "0");
        }

        // This checks the size of the date string and then gets the string for the year by using substring between
        // (size - 4) and (total size), which returns our year as we are keeping within 4 digits.
        int size = date.size();
        string year = date.substr(size-4, size);

        // This builds the string for the date in the format we want and returns it.
        string dateBuilt = year + "-" + month + "-" + day;
        return dateBuilt;
    }
};
8100