void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential) { std::string::const_iterator it = fmt.begin(); std::string::const_iterator end = fmt.end(); while (it != end) { if (*it == '%') { if (++it != end) { switch (*it) { case 'w': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break; case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break; case 'b': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break; case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break; case 'd': NumberFormatter::append0(str, dateTime.day(), 2); break; case 'e': NumberFormatter::append(str, dateTime.day()); break; case 'f': NumberFormatter::append(str, dateTime.day(), 2); break; case 'm': NumberFormatter::append0(str, dateTime.month(), 2); break; case 'n': NumberFormatter::append(str, dateTime.month()); break; case 'o': NumberFormatter::append(str, dateTime.month(), 2); break; case 'y': NumberFormatter::append0(str, dateTime.year() % 100, 2); break; case 'Y': NumberFormatter::append0(str, dateTime.year(), 4); break; case 'H': NumberFormatter::append0(str, dateTime.hour(), 2); break; case 'h': NumberFormatter::append0(str, dateTime.hourAMPM(), 2); break; case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break; case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break; case 'M': NumberFormatter::append0(str, dateTime.minute(), 2); break; case 'S': NumberFormatter::append0(str, dateTime.second(), 2); break; case 's': NumberFormatter::append0(str, dateTime.second(), 2); str += '.'; NumberFormatter::append0(str, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break; case 'i': NumberFormatter::append0(str, dateTime.millisecond(), 3); break; case 'c': NumberFormatter::append(str, dateTime.millisecond()/100); break; case 'F': NumberFormatter::append0(str, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break; case 'z': tzdISO(str, timeZoneDifferential); break; case 'Z': tzdRFC(str, timeZoneDifferential); break; default: str += *it; } ++it; } } else str += *it++; } }
std::string DateTimeFormatter::format(const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential) { std::string result; std::string::const_iterator it = fmt.begin(); std::string::const_iterator end = fmt.end(); while (it != end) { if (*it == '%') { if (++it != end) { switch (*it) { case 'w': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break; case 'W': result.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break; case 'b': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1], 0, 3); break; case 'B': result.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break; case 'd': result.append(NumberFormatter::format0(dateTime.day(), 2)); break; case 'e': result.append(NumberFormatter::format(dateTime.day())); break; case 'f': result.append(NumberFormatter::format(dateTime.day(), 2)); break; case 'm': result.append(NumberFormatter::format0(dateTime.month(), 2)); break; case 'n': result.append(NumberFormatter::format(dateTime.month())); break; case 'o': result.append(NumberFormatter::format(dateTime.month(), 2)); break; case 'y': result.append(NumberFormatter::format0(dateTime.year() % 100, 2)); break; case 'Y': result.append(NumberFormatter::format0(dateTime.year(), 4)); break; case 'H': result.append(NumberFormatter::format0(dateTime.hour(), 2)); break; case 'h': result.append(NumberFormatter::format0(dateTime.hourAMPM(), 2)); break; case 'a': result.append(dateTime.isAM() ? "am" : "pm"); break; case 'A': result.append(dateTime.isAM() ? "AM" : "PM"); break; case 'M': result.append(NumberFormatter::format0(dateTime.minute(), 2)); break; case 'S': result.append(NumberFormatter::format0(dateTime.second(), 2)); break; case 'i': result.append(NumberFormatter::format0(dateTime.millisecond(), 3)); break; case 'c': result.append(NumberFormatter::format(dateTime.millisecond()/100)); break; case 'z': result.append(tzdISO(timeZoneDifferential)); break; case 'Z': result.append(tzdRFC(timeZoneDifferential)); break; default: result += *it; } ++it; } } else result += *it++; } return result; }