void RelativeDateUnitTest::test_lifecycle_valid() { //test: RelativeDateUnit::RelativeDateUnit(); /**< Defaults to 1D*/ RelativeDateUnit date1; CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Day, date1.getUnit()); CPPUNIT_ASSERT_EQUAL(0, date1.getQuantity()); //test: RelativeDateUnit::RelativeDateUnit(const RelativeDate& other); RelativeDateUnit date2(date1); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Day, date2.getUnit()); CPPUNIT_ASSERT_EQUAL(0, date2.getQuantity()); //test: RelativeDateUnit::RelativeDateUnit(const String& stringRep); RelativeDateUnit date3("1Y"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Year, date3.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date3.getQuantity()); RelativeDateUnit date4("1y"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Year, date4.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date4.getQuantity()); RelativeDateUnit date5("1M"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Month, date5.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date3.getQuantity()); RelativeDateUnit date6("1m"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Month, date6.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date4.getQuantity()); RelativeDateUnit date7("1W"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Week, date7.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date7.getQuantity()); RelativeDateUnit date8("1w"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Week, date8.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date8.getQuantity()); RelativeDateUnit date9("1D"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Day, date9.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date7.getQuantity()); RelativeDateUnit date10("1d"); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Day, date10.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date8.getQuantity()); //test: RelativeDateUnit::RelativeDateUnit(Integer quantity, RelativeDateUnit::eRelativeDateUnit unit); RelativeDateUnit date11(1, RelativeDateUnitType::Year); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Year, date11.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date11.getQuantity()); RelativeDateUnit date12(1, RelativeDateUnitType::Month); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Month, date12.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date12.getQuantity()); RelativeDateUnit date13(1, RelativeDateUnitType::Week); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Week, date13.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date13.getQuantity()); RelativeDateUnit date14(1, RelativeDateUnitType::Day); CPPUNIT_ASSERT_EQUAL(RelativeDateUnitType::Day, date14.getUnit()); CPPUNIT_ASSERT_EQUAL(1, date14.getQuantity()); }
void test() { cout << " zDate Class Demo \n\n"; // default constructor, Jan 1 0000 zDate a; cout << a << endl; // Various versions of the constructors zDate x(zDate::oct,20,1962); cout << x << endl; // constructor with a julian zDate z( 2450000L ); cout << z << endl; // make a date with system date (tests copy constructor) zDate s(zDate::Today()); cout << s << endl; // init with the day of year zDate y(33, 1996); cout << y << endl; // init from current system time time_t secs_now = time(NULL); zDate n(localtime(&secs_now)); cout << n << endl; // using date addition and subtraction zDate adder = x + 10; cout << adder << endl; adder = adder - 25; cout << adder << endl; //using subtraction of two date objects zDate a1(zDate::Today()); zDate a2 = a1 + 14; cout << (a1 - a2) << endl; cout << (a2 += 10) << endl; a1++; cout << "Tommorrow= " << a1 << endl; a1 = zDate(zDate::jul, 14, 1991); cout << "a1 (7-14-91) < a2 (" << a2 << ")? ==> " << ((a1 < a2) ? "TRUE" : "FALSE") << endl; cout << "a1 (7-14-91) > a2 ("<< a2 << ")? ==> " << ((a1 > a2) ? "TRUE" : "FALSE") << endl; cout << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < zDate(zDate::aug, 1, 1991)) ? "TRUE" : "FALSE") << endl; cout << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > zDate(zDate::aug, 1, 1991)) ? "TRUE" : "FALSE") << endl; cout << "a1 (7-14-91) == 7-14-91 ? ==> " << ((a1==zDate(zDate::jul, 14, 1991)) ? "TRUE" : "FALSE") << endl; zDate a3 = a1; cout << "a1 (" << a1 << ") == a3 (" << a3 << ") ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << endl; zDate a4 = a1; ++a4; cout << "a1 ("<< a1 <<") == a4 (" << a4 << ") ? ==> " << ((a1==a4) ? "TRUE" : "FALSE") << endl; zDate a5(zDate::Today()); cout << "Today is: " << a5 << endl; a4 = zDate::Today(); cout << "Today (a4) is: " << a4 << endl; cout << "Today + 4 is: " << (a4 += 4) << endl; a4 = zDate::Today(); cout << "Today - 4 is: " << (a4 -= 4) << endl; cout << "=========== Leap Year Test ===========\n"; a1 = zDate(zDate::jan, 15, 1992); cout << a1 << "\t" << ((a1.IsLeapYear()) ? "Leap" : "non-Leap"); cout << "\t" << "day of year: " << a1.DayOfYear() << endl; a1 = zDate(zDate::feb, 16, 1993); cout << a1 << "\t" << ((a1.IsLeapYear()) ? "Leap" : "non-Leap"); cout << "\t" << "day of year: " << a1.DayOfYear() << endl; zDate v4(zDate::Today()); cout << "---------- Add Stuff -----------\n"; cout << "Start => " << v4 << endl; cout << "Add 4 Weeks => " << v4.AddWeeks(4) << endl; cout << "Sub 52 Weeks => " << v4.AddWeeks(-52) << endl; cout << "Add 2 Years => " << v4.AddYears(2) << endl; cout << flush; cout << "---------- Misc Stuff -----------\n"; cout << "The date aboves' day of the month is => " << v4.Day() << endl; cout << "There are " << v4.DaysInMonth() << " days in this month.\n"; cout << "This day happens to be " << v4.DayOfWeek() << " day of week" << endl; cout << "on the " << v4.WeekOfYear() << " week of the year," << endl; cout << "on the " << v4.WeekOfMonth() << " week of the month, " << endl; cout << "which is the "<< (int)v4.Month() << "nth month in the year.\n"; cout << "The year alone is " << v4.Year() << endl; cout << "And this is the " << v4.DayOfYear() << " day of year" << endl; cout << "of a year with " << v4.DaysInYear() << " days in it" << endl; cout << "which makes exatcly " << v4.WeeksInYear() << " weeks" << endl; zDate birthday(zDate::jul, 16, 1973); cout << "The age test: i was born on " << birthday << " which makes me " << v4.Age(birthday) << " years old" << endl; zDate D2(zDate::jul, 4, 1776); int I1 = 4; cout << "Before: I1 = " << I1 << ", D2 = " << D2 << endl; cout << "---------- Postfix '++' test -----------\n"; cout << "Test : I1++ = " << I1++ << ", D2++ = " << D2++ << endl; cout << "After: I1 = " << I1 << ", D2 = " << D2 << endl; cout << "---------- Prefix '++' test -----------\n"; cout << "Test : ++I1 = " << ++I1 << ", ++D2 = " << ++D2 << endl; cout << "After: I1 = " << I1 << ", D2 = " << D2 << endl; cout << "---------- Postfix '--' test -----------\n"; cout << "Test : I1-- = " << I1-- << ", D2-- = " << D2-- << endl; cout << "After: I1 = " << I1 << ", D2 = " << D2 << endl; cout << "---------- Prefix '--' test -----------\n"; cout << "Test : --I1 = " << --I1 << ", --D2 = " << --D2 << endl; cout << "After: I1 = " << I1 << ", D2 = " << D2 << endl; cout << "Last day of this year is dayno " << zDate(zDate::dec, 31, 1996).DayOfYear() << endl; cout << "Last day of prev year is dayno " << zDate(zDate::dec, 31, 1995).DayOfYear() << endl; cout << "Today the moon is " << zDate::Today().MoonPhase() << endl; zDate today = zDate::Today(); cout << "DST for " << today.Year() << " starts on " << today.BeginDST() << " and ends on " << today.EndDST() << endl; cout << "Today, " << today << ", DST is " << (today.IsDST() ? "" : "not") << "in effect" << endl; zDate date1(zDate::aug, 31, 1996); cout << "Adding 6 months to " << date1 << " results in " << date1.AddMonths(6) << endl; zDate date2(zDate::mar, 31, 1996); cout << "Subtracting 1 month from " << date2 << " results in " << date2.AddMonths(-1) << endl; zDate date3(zDate::jul, 4, 1776); cout << "Adding 2400 months to " << date3 << " results in " << date3.AddMonths(2400) << endl; cout << "Today's day number is " << zDate::Today().DayNumber() << endl; zDate date4(zDate::feb, 29, 1996); cout << date4 << " subtract two years = " << date4.AddYears(-2) << endl; cout << "In 1996, DST began on " << zDate::BeginDST(1996) << endl; zDate date5(zDate::sep, 26, 1996); cout << "Moon phase on " << date5 << " was " << date5.MoonPhase() << endl; zDate date6(zDate::oct, 3, 1996); cout << date6 << " + 55 days is " << (date6 + 55) << endl; zDate date7(zDate::oct, 4, 1996); cout << date7 << " + 217 days is "; date7 += 217; cout << date7 << endl; date7 = zDate(zDate::oct, 4, 1996); cout << "Same date - (-217) days is "; date7 -= -217; cout << date7 << endl; cout << "For 1996, Easter is on " << zDate::Easter(1996) << endl; }
void DateTimeTextMatchPlugin::doGetPossibleMatches( const QString& text ) { // // check for years (numbers between 1900 and 2020 for example) // check for stuff like "June 22" or even "June 22-26th" (the latter can be used as start and end time already) // check for "2 o'clock" // check for "14:23" // check for 12.6.2009 and 6/12/2009 and 6/12/09 and 6.12.09 // m_years.clear(); m_dates.clear(); m_times.clear(); m_dateRanges.clear(); m_text = text; // we are english-only here! QStringList longMonthNames; QStringList shortMonthNames; for ( int i = 1; i <= 12; ++i ) { longMonthNames << m_enLocale.monthName( i, QLocale::LongFormat ); shortMonthNames << m_enLocale.monthName( i, QLocale::ShortFormat ); } // // most of the dates and times can be checked with QRegExp // // DD.MM.YYYY QRegExp date1( "\\b\\d{1,2}\\.\\d{1,2}\\.\\d{4,4}\\b" ); // DD.MM.YY QRegExp date2( "\\b\\d{1,2}\\.\\d{1,2}\\.\\d{2,2}\\b" ); // MM/DD/YYYY QRegExp date3( "\\b\\d{1,2}/\\d{1,2}/\\d{4,4}\\b" ); // MM/DD/YY QRegExp date4( "\\b\\d{1,2}/\\d{1,2}/\\d{2,2}\\b" ); // January MM [YYYY] (no word boundry at the end for 'st' or 'nd' or 'th') (also excluding ranges) QRegExp date5( QString( "\\b(%1)\\s\\d{1,2}(?!(\\d|\\s?-\\s?\\d))(\\s\\d{4,4})?" ).arg( longMonthNames.join( "|" ) ) ); // January, MM [YYYY] (no word boundry at the end for 'st' or 'nd' or 'th') (also excluding ranges) QRegExp date6( QString( "\\b(%1),\\s?\\d{1,2}(?!(\\d|\\s?-\\s?\\d))(\\s\\d{4,4})?" ).arg( longMonthNames.join( "|" ) ) ); // FIXME: date ranges 1-4 QRegExp dateRange5( QString( "(\\b(?:%1)\\s\\d{1,2})\\s?-\\s?(\\d{1,2})(\\s\\d{4,4})?" ).arg( longMonthNames.join( "|" ) ) ); QRegExp dateRange6( QString( "(\\b(?:%1),\\s?\\d{1,2})\\s?-\\s?(\\d{1,2})(\\s\\d{4,4})?" ).arg( longMonthNames.join( "|" ) ) ); // YYYY (exclude those in full dates matched by the above) QRegExp year( "[^\\./]\\d{4,4}\\b" ); // hh:mm[pm|am] QRegExp time1( "\\b\\d{1,2}\\:\\d{2,2}\\s?(pm|am|AM|PM)?\\b" ); // hh:mm QRegExp time2( "\\b\\d{1,2}\\:\\d{2,2}\\b(?!\\s?(pm|am|AM|PM))\\b" ); // hh o'clock // QRegExp time3( "\\b\\d{1,2}\\so'clock\\b" ); lookForYears( year ); lookForDates( date1, "d.M.yyyy" ); lookForDates( date2, "d.M.yy" ); lookForDates( date3, "M/d/yyyy" ); lookForDates( date4, "M/d/yy" ); lookForDates( date5, "MMMM d", true ); lookForDates( date5, "MMMM d yyyy" ); lookForDates( date6, "MMMM, d", true ); lookForDates( date6, "MMMM,d", true ); lookForDates( date6, "MMMM, d yyyy" ); lookForDates( date6, "MMMM,d yyyy" ); lookForDateRanges( dateRange5, "MMMM d", 1, 2, 3, true ); lookForDateRanges( dateRange5, "MMMM d yyyy", 1, 2, 3, false ); lookForDateRanges( dateRange6, "MMMM,d", 1, 2, 3, true ); lookForDateRanges( dateRange6, "MMMM, d", 1, 2, 3, true ); lookForDateRanges( dateRange6, "MMMM,d yyyy", 1, 2, 3, false ); lookForDateRanges( dateRange6, "MMMM, d yyyy", 1, 2, 3, false ); lookForTimes( time1, "h:map" ); lookForTimes( time1, "h:m ap" ); lookForTimes( time2, "h:m" ); // FIXME: do a date and time proximity search to create combined datetime objects // // Now use the dates and times to create statements // for ( QHash<int, QPair<QDate, int> >::const_iterator it = m_dates.constBegin(); it != m_dates.constEnd(); ++it ) { // FIXME: this is not great: 1. dtstart has range dateTime, 2. we do not know that it is a start // better use something else or even create Scribo::Literal as alternative to Entity Scribo::Statement s( Nepomuk::Vocabulary::PIMO::dtstart(), Nepomuk::Variant( it.value().first ), Soprano::Graph() ); Scribo::TextOccurrence oc; oc.setStartPos( it.key() ); oc.setLength( it.value().second ); // oc.setRelevance( 0.9 ); ????????? s.addOccurrence( oc ); addNewMatch( s ); } for ( QHash<int, QPair<QTime, int> >::const_iterator it = m_times.constBegin(); it != m_times.constEnd(); ++it ) { // FIXME: this is not great: 1. dtstart has range dateTime, 2. we do not know that it is a start // better use something else or even create Scribo::Literal as alternative to Entity Scribo::Statement s( Nepomuk::Vocabulary::PIMO::dtstart(), Nepomuk::Variant( it.value().first ), Soprano::Graph() ); Scribo::TextOccurrence oc; oc.setStartPos( it.key() ); oc.setLength( it.value().second ); // oc.setRelevance( 0.9 ); ????????? s.addOccurrence( oc ); addNewMatch( s ); } for ( QHash<int, QPair<QPair<QDate, QDate>, int> >::const_iterator it = m_dateRanges.constBegin(); it != m_dateRanges.constEnd(); ++it ) { // FIXME: this is not great: 1. dtstart has range dateTime, 2. we do not know that it is a start // better use something else or even create Scribo::Literal as alternative to Entity Scribo::Statement s1( Nepomuk::Vocabulary::PIMO::dtstart(), Nepomuk::Variant( it.value().first.first ), Soprano::Graph() ); Scribo::TextOccurrence oc1; oc1.setStartPos( it.key() ); oc1.setLength( it.value().second ); // oc.setRelevance( 0.9 ); ????????? s1.addOccurrence( oc1 ); addNewMatch( s1 ); Scribo::Statement s2( Nepomuk::Vocabulary::PIMO::dtend(), Nepomuk::Variant( it.value().first.second ), Soprano::Graph() ); Scribo::TextOccurrence oc2; oc2.setStartPos( it.key() ); oc2.setLength( it.value().second ); // oc.setRelevance( 0.9 ); ????????? s2.addOccurrence( oc2 ); addNewMatch( s2 ); } emitFinished(); }
int main(int argc, const char *argv[]) { { // 日期创建 boost::gregorian::date date1(boost::gregorian::from_string("2016-2-9")); boost::gregorian::date date2(boost::gregorian::from_simple_string("2016-2-9")); boost::gregorian::date date3(boost::gregorian::from_simple_string("2016-Feb-9")); boost::gregorian::date date4(boost::gregorian::from_undelimited_string("20160209")); boost::gregorian::date date5(boost::gregorian::date_from_iso_string("20160209")); boost::gregorian::date date6(2016, 2, 9); boost::gregorian::date date7(2016, boost::gregorian::Feb, 9); std::cout << "date1: " << date1 << std::endl; std::cout << "date2: " << date2 << std::endl; std::cout << "date3: " << date3 << std::endl; std::cout << "date4: " << date4 << std::endl; std::cout << "date5: " << date5 << std::endl; std::cout << "date6: " << date6 << std::endl; std::cout << "date7: " << date7 << std::endl; } { // 当前日期 boost::gregorian::date today(boost::gregorian::day_clock::local_day()); std::cout << "today: " << today << std::endl; } { // 日期运算 boost::gregorian::date today(boost::gregorian::day_clock::local_day()); boost::gregorian::date date1 = today + boost::gregorian::days(100); std::cout << "today: " << date1 << std::endl; std::cout << "duration: " << (date1 - today).days() << std::endl; std::cout << "date1 > today: " << (date1 > today) << std::endl; } { // 日期字符串 boost::gregorian::date today(boost::gregorian::day_clock::local_day()); std::cout << "today: " << boost::gregorian::to_iso_string(today) << std::endl; std::cout << "today: " << boost::gregorian::to_iso_extended_string(today) << std::endl; } { // 日期属性 boost::gregorian::date today(boost::gregorian::day_clock::local_day()); std::cout << "year: " << today.year() << std::endl; std::cout << "month: " << today.month() << std::endl; // Jan, Feb std::cout << "month: " << today.month().as_number() << std::endl; // month of year 1-12 std::cout << "day: " << today.day() << std::endl; // day of month 1-31 std::cout << "day of year: " << today.day_of_year() << std::endl; // day of year 1-366 std::cout << "day of week: " << today.day_of_week() << std::endl; // Mon, Tue std::cout << "day of week: " << today.day_of_week().as_number() << std::endl; // 1-7 std::cout << "week number: " << today.week_number() << std::endl; // 1-53 std::cout << "day number: " << today.day_number() << std::endl; // total day std::cout << "end of month: " << today.end_of_month() << std::endl; // 2016-Feb-29 } { // 日期循环 boost::gregorian::date t1(boost::gregorian::from_simple_string("2016-2-9")); boost::gregorian::date t2(boost::gregorian::from_simple_string("2016-3-9")); for (boost::gregorian::date d = t1; d != t2; d += boost::gregorian::days(1)) { std::cout << d << std::endl; } } { // ptime boost::posix_time::ptime t1(boost::gregorian::date(2016, 2, 9), boost::posix_time::hours(18)); boost::posix_time::ptime t2(boost::gregorian::date(2016, 2, 9), boost::posix_time::time_duration(19, 1, 1, 0)); auto d = t2 - t1; std::cout << d.total_seconds() << std::endl; } return 0; }