Exemple #1
0
int WDate::parseShortMonthName(const std::string& v, unsigned& pos)
{
  if (pos + 2 >= v.length())
    return -1;

  std::string m = v.substr(pos, 3);
  
  for (int i = 1; i <= 12; ++i) {
    if (m == shortMonthName(i).toUTF8()) {
      pos += 3;
      return i;
    }
  }

  return -1;
}
QString ExtDate::toString( const QString& format ) const
{
	if ( ! isValid() ) return QString::null;

	//We use the KDE Date format specs.
	//Replace occurences of the following tokens with their
	//corresponding values:
	//
	// %Y The year, including centuries prefix (e.g., "1984")
	// %y The year, excluding centuries prefix (e.g., "84")
	// %n Numerical month value (e.g., "3" for March)
	// %m Numerical month value, two digits (e.g., "03" for March)
	// %e Numerical day value (e.g., "3" on March 3rd)
	// %d Numerical day value, two digits (e.g., "03" on March 3rd)
	// %b Month name, short form (e.g., "Mar" for March)
	// %B Month name, long form (e.g., "March")
	// %a Weekday name, short form (e.g., "Wed" for Wednesday)
	// %A Weekday name, long form (e.g., "Wednesday")

	//All other characters are left as-is.

	QString result( format );

	result.replace( "%Y", QString().sprintf( "%d", year() ) );
	result.replace( "%y", QString().sprintf( "%02d", (year() % 100) ) );
	result.replace( "%n", QString().sprintf( "%d", month() ) );
	result.replace( "%m", QString().sprintf( "%02d", month() ) );
	result.replace( "%e", QString().sprintf( "%d", day() ) );
	result.replace( "%d", QString().sprintf( "%02d", day() ) );
	result.replace( "%b", shortMonthName( month() ) );
	result.replace( "%B", longMonthName( month() ) );
	result.replace( "%a", shortDayName( dayOfWeek() ) );
	result.replace( "%A", longDayName( dayOfWeek() ) );

	return result;
}
Exemple #3
0
CString CDateTime::toString(const CString& format) const
{
    if (isNull())
    {
        return CString();
    }

    CString str;
    bool special = false;
    const unsigned int len = format.getSize();

    for (unsigned int i = 0 ; i < len ; ++i)
    {
        if (format[i] == CChar('%'))
        {
            special = true;
        }
        else if (special)
        {
            switch (format[i].toLatin1())
            {
                default:
                    str += format[i];
                    break;

                case 'a':
                    str += (m_hour < 12 ? "am" : "pm");
                    break;

                case 'A':
                    str += (m_hour < 12 ? "AM" : "PM");
                    break;

                case 'd':

                    if (m_day < 10)
                        str += '0';

                    str += CString::fromNumber(m_day);
                    break;

                case 'D':
                    str += shortDayName(dayOfWeek(m_year, m_month, m_day));
                    break;

                case 'F':
                    str += longMonthName(m_month);
                    break;

                case 'g':

                    if (m_hour == 0)
                    {
                        str += "12";
                    }
                    else if (m_hour < 13)
                    {
                        str += CString::fromNumber(m_hour);
                    }
                    else
                    {
                        str += CString::fromNumber(m_hour - 12);
                    }

                    break;

                case 'G':
                    str += CString::fromNumber(m_hour);
                    break;

                case 'h':

                    if (m_hour == 0)
                    {
                        str += "12";
                    }
                    else if (m_hour < 13)
                    {
                        if (m_hour < 10)
                        {
                            str += '0';
                        }

                        str += CString::fromNumber(m_hour);
                    }
                    else
                    {
                        if (m_hour < 22)
                        {
                            str += '0';
                        }

                        str += CString::fromNumber(m_hour - 12);
                    }

                    break;

                case 'H':

                    if (m_hour < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_hour);

                    break;

                case 'i':

                    if (m_minute < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_minute);

                    break;

                case 'j':
                    str += CString::fromNumber(m_day);
                    break;

                case 'l':
                    str += longDayName(dayOfWeek(m_year, m_month, m_day));
                    break;

                case 'L':
                    str += (isLeapYear(m_year) ? '1' : '0');
                    break;

                case 'm':

                    if (m_month < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_month);

                    break;

                case 'M':
                    str += shortMonthName(m_month);
                    break;

                case 'n':
                    str += CString::fromNumber(m_month);
                    break;

                case 'N':
                    str += dayOfWeek(m_year, m_month, m_day);
                    break;

                case 's':

                    if (m_second < 10)
                    {
                        str += '0';
                    }

                    str += CString::fromNumber(m_second);

                    break;

                case 't':
                    str += CString::fromNumber(daysInMonth(m_month, m_year));
                    break;

                case 'U':
                    str += CString::fromNumber(timestamp(m_year, m_month, m_day, m_hour, m_minute, m_second));
                    break;

                case 'W':
                    str += CString::fromNumber(weekOfYear(m_year, m_month, m_day));
                    break;

                case 'y':

                    if (m_year < 0)
                    {
                        str += CString::fromNumber(m_year);
                    }
                    else
                    {
                        unsigned int tmp = m_year % 100;

                        if (tmp < 10)
                        {
                            str += "0";
                        }

                        str += CString::fromNumber(tmp);
                    }

                    break;

                case 'Y':

                    if (m_year < 0)
                    {
                        str += CString::fromNumber(m_year);
                    }
                    else
                    {
                        unsigned int tmp = m_year % 10000;

                        if (tmp < 10)
                            str += "000";
                        if (tmp < 100)
                            str += "00";
                        if (tmp < 1000)
                            str += "000";

                        str += CString::fromNumber(tmp);
                    }

                    break;

                case 'z':
                    str += dayOfYear(m_year, m_month, m_day);
                    break;
            }

            special = false;
        }
        else
        {
            str += format[i];
        }
    }

    return str;
}
ExtDate ExtDate::fromString( const QString& s, Qt::DateFormat f )
{
	ExtDate dt = ExtDate();  //initialize invalid date
	if ( s.isEmpty() ) { return dt; }
	if ( f == Qt::LocalDate ) { //can't use LocalFormat
#if defined(QT_CHECK_RANGE)
		qWarning( "QDate::fromString: Parameter out of range" );
#endif
		return dt;
	}

	switch( f ) {
		case Qt::ISODate :
		{
			int year( s.mid( 0, 4 ).toInt() );
			int month( s.mid( 5, 2 ).toInt() );
			int day( s.mid( 8, 2 ).toInt() );

			if ( year && month && day )
				return ExtDate( year, month, day );
		}
		break;

		default :
#ifndef QT_NO_TEXTDATE
		case Qt::TextDate :
		{
			//Three possible date formats:
			//dd mth yyyy; mth dd yyyy; wkd mth dd yyyy
			//"mth" is the word for the month (long or short form)
			QStringList ss = QStringList::split( " ", s );
			bool ok = false;
			int month = -1;
			uint imonth = 0;
			uint iyear = 0;

			//If neither of the first two words is a number, then we'll assume
			//the first word is a superfluous "weekday" string
			int day = ss[0].toInt( &ok );
			if ( ! ok ) {
				day = ss[1].toInt( &ok );
				if ( ! ok ) {
						day = ss[2].toInt( &ok );
						if ( !ok ) return dt;  //could not find a valid day number in first three words
						imonth = 1;  //the month must be the second word
						iyear = 3;  //the year must be the fourth word
				} else {
					//the month is either the first word, or the third.
					imonth = 0;
					iyear = 2;
				}
			} else {
				//month is the second word
				imonth = 1;
				iyear = 2;
			}

			for ( uint i = 0; i < 12; i++ ) {
				if ( ss[imonth] == shortMonthName( i+1 ) || ss[imonth] == longMonthName( i+1 ) ) {
						month = i + 1;
						break;
				}
			}

			if ( month == -1 && imonth == 0 ) { //try the third word
				imonth = 2;
				iyear = 3;
				for ( uint i = 0; i < 12; i++ ) {
					if ( ss[imonth] == shortMonthName( i+1 ) || ss[imonth] == longMonthName( i+1 ) ) {
							month = i + 1;
							break;
					}
				}
			}

			if ( month > -1 ) ok = true;
			if ( ! ok ) return dt; //could not parse month; return invalid

			int year = ss[iyear].toInt( &ok );
			if ( ! ok ) return dt; //could not parse year; return invalid

			return ExtDate( year, month, day );

			break;
		}
#else
		break;
#endif  //ifndef QT_NO_TEXTDATE
	}

	return dt;
}
Exemple #5
0
bool WDate::writeSpecial(const std::string& f, unsigned&i,
			 std::stringstream& result) const
{
  char buf[30];

  switch (f[i]) {
  case 'd':
    if (f[i + 1] == 'd') {
      if (f[i + 2] == 'd') {
	if (f[i + 3] == 'd') {
	  // 4 d's
	  i += 3;
	  result << longDayName(dayOfWeek()).toUTF8();
	} else {
	  // 3 d's
	  i += 2;
	  result << shortDayName(dayOfWeek()).toUTF8();
	}
      } else {
	// 2 d's
	i += 1;
	result << Utils::pad_itoa(day_, 2, buf);
      }
    } else {
      // 1 d
      result << Utils::itoa(day_, buf);
    }

    return true;
  case 'M':
    if (f[i + 1] == 'M') {
      if (f[i + 2] == 'M') {
	if (f[i + 3] == 'M') {
	  // 4 M's
	  i += 3;
	  result << longMonthName(month_).toUTF8();
	} else {
	  // 3 M's
	  i += 2;
	  result << shortMonthName(month_).toUTF8();
	}
      } else {
	// 2 M's
	i += 1;
	result << Utils::pad_itoa(month_, 2, buf);
      }
    } else {
      // 1 M
      result << Utils::itoa(month_, buf);
    }

    return true;
  case 'y':
    if (f[i + 1] == 'y') {
      if (f[i + 2] == 'y' && f[i + 3] == 'y') {
	// 4 y's
	i += 3;
	result << Utils::itoa(year_, buf);

	return true;
      } else {
	// 2 y's
	i += 1;
	result << Utils::pad_itoa(year_ % 100, 2, buf);

	return true;
      }
    }
  default:
    return false;
  }
}