Converting ‘created at’ time from Twitter to the time format of Facebook

Just because Facebook time format is closer to the UTC standard format, and I need to sort posts from Facebook & Twitter...
void normalizeTime( const char *twitterTimeToNormalize, char normalizedTime[] )
{
    // normalized format : 2014-06-20T06:27:00+0000
    // twitter time format : Thu Jun 12 15:52:58 +0000 2014
    
    char temp[5];
    char month[4];
    int day, hour, min, sec, offUTC, year;
    
    sscanf(twitterTimeToNormalize, "%s %s %2d %2d:%2d:%2d %5d %4d",
           temp, month, &day, &hour, &min, &sec, &offUTC, &year );
    
    // On Windows, there is _stricmp(), but on Mac it's strcasecmp().
    // So, let's make it lower case first.
    std::string month_string = month;
    std::transform(month_string.begin(), month_string.end(), month_string.begin(), ::tolower);
    
    int month_int = 0;
    
    if( month_string.compare("jan") == 0 ) {
        month_int = 1;
    }
    else if( month_string.compare("feb") == 0 ) {
        month_int = 2;
    }
    else if( month_string.compare("mar") == 0 ) {
        month_int = 3;
    }
    else if( month_string.compare("apr") == 0 ) {
        month_int = 4;
    }
    else if( month_string.compare("may") == 0 ) {
        month_int = 5;
    }
    else if( month_string.compare("jun") == 0 ) {
        month_int = 6;
    }
    else if( month_string.compare("jul") == 0 ) {
        month_int = 7;
    }
    else if( month_string.compare("aug") == 0 ) {
        month_int = 8;
    }
    else if( month_string.compare("sep") == 0 ) {
        month_int = 9;
    }
    else if( month_string.compare("oct") == 0 ) {
        month_int = 10;
    }
    else if( month_string.compare("nov") == 0 ) {
        month_int = 11;
    }
    else if( month_string.compare("dec") == 0 ) {
        month_int = 12;
    }
    
    // Now convert to standard time format, which is used by Facebook
    // 2014-06-20T06:27:00+0000
    sprintf(normalizedTime, "%4d-%02d-%02dT%02d:%02d:%02d%+05d",
            year, month_int, day, hour, min, sec, offUTC );
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: