Exemple #1
0
//! Get the current time as a string.
std::string GetLocalTimeString()
{
    Poco::LocalDateTime *time = new Poco::LocalDateTime();
    std::stringstream ss;
    
    ss << std::setw(2) << time->hour() << std::setfill('0') << ":" <<
        std::setw(2) << time->minute() << std::setfill('0') << ":" <<
        std::setw(2) << time->second() << std::setfill('0');
        
    SAFE_DELETE(time);
    
    return ss.str();
}
TimeSchedule TimerStringParser::parseWeekly(const Poco::LocalDateTime& now, const std::string& timerString)
{
    const int nowDay = now.dayOfWeek(); // 0 = Sunday, 6 = Saturday
    int sourceDay = extractDay(timerString); // 0 = Sunday, 6 = Saturday

    if(sourceDay < 0)
    {
        LOGE("Invalid day of week in timerString " + timerString);
    }

    if(sourceDay < nowDay) // wait until next week...
        sourceDay += 7;

    int daysToWait = sourceDay - nowDay;

    std::pair<int, int> hourMinutes = parseTime(extractTime(timerString));
    const int hour = hourMinutes.first;
    const int minutes = hourMinutes.second;

    // If the timer runs today, check if it already ran or if it will
    // still run today
    if(daysToWait == 0)
    {
        Poco::LocalDateTime ifToday(
            now.year(),
            now.month(),
            now.day(),
            hour,
            minutes);

        if(ifToday < now)
        {
            LOGI("It's to late to run " + timerString + " today; running next week.");
            daysToWait = 7;
        }
    }

    // At this point, I'd just like to add daysToWait to now and combine
    // this with hour and minutes to know when to run the next time.
    // Unfortunately, unless I'm missing something, this isn't that easy.
    // Since the Poco LocalDateTime and DateTime constructors refuse to do
    // date arithmetic (e.g. day 35 will be somewhere in the next month),
    // I'll have to construct a Timespan that spans the correct number of
    // days, add that to now and then use the day, month & year info in
    // the resulting date. Which can then be combined with hour & minute
    // to find when the next run will be.
    Poco::Timespan daysToWaitTimespan(daysToWait*Poco::Timespan::DAYS);

    Poco::LocalDateTime correctDay = now + daysToWaitTimespan;
    Poco::LocalDateTime correctEverything(
        correctDay.year(),
        correctDay.month(),
        correctDay.day(),
        hour,
        minutes);

    Poco::Timespan wait = correctEverything - now;

    return TimeSchedule(wait, Poco::Timespan(7*Poco::Timespan::DAYS));
}
TimeSchedule TimerStringParser::parseDaily(const Poco::LocalDateTime& now, const std::string& timerString)
{
    std::pair<int, int> hourMinutes = parseTime(extractTime(timerString));
    const int hour = hourMinutes.first;
    const int minutes = hourMinutes.second;

    int dayOffset = 0;
    Poco::LocalDateTime ifToday(
        now.year(),
        now.month(),
        now.day(),
        hour,
        minutes);

    if(ifToday < now)
    {
        LOGI("It's to late to run " + timerString + " today; running next week.");
        dayOffset = 1;
    }

    Poco::LocalDateTime tmp(
        now.year(),
        now.month(),
        now.day(),
        hour,
        minutes);

    const Poco::Timespan daysToWaitTimespan(dayOffset*Poco::Timespan::DAYS);
    const Poco::LocalDateTime source = tmp + daysToWaitTimespan;

    const Poco::Timespan wait = source - now;

    return TimeSchedule(wait, Poco::Timespan(1*Poco::Timespan::DAYS));
}
void CalendarWidget::draw()
{
    ofFill();
    ofSetColor(0, 200);

    ofDrawRectangle(_window);

    std::string formatStringHourMin = "%h:%M %A";
    std::string formatStringHour = "%h:%M";
    std::string formatStringHourMinSec = "%h:%M:%S %A";

    Poco::LocalDateTime minTime(_windowInterval.getStart());
    Poco::LocalDateTime maxTime(_windowInterval.getEnd());

    Poco::LocalDateTime startQuarter = Utils::ceiling(minTime, Poco::Timespan::MINUTES * 5);

    ofPushMatrix();
    ofTranslate(_window.getPosition());

    std::vector<Poco::Timestamp> hours = Utils::getInstances(startQuarter.utc().timestamp(),
                                                             maxTime.utc().timestamp(),
                                                             Period(Period::MINUTE, 5));

    std::vector<Poco::Timestamp>::const_iterator hourIter = hours.begin();

    while (hourIter != hours.end())
    {
        Poco::DateTime time = Poco::DateTime(*hourIter);

        int y = _window.getHeight() * _windowInterval.map(time.timestamp());

        int minute = time.minute();

        float alpha = ofMap(std::abs(_windowInterval.map(time.timestamp()) - 0.5), 0, .2, .25, 1, true);

        if (0 == minute)
        {
            ofSetColor(255, 80 * alpha);
            ofDrawLine(0, y, _window.getWidth(), y);
        }
        else if (0 == minute % 15)
        {
            ofSetColor(255, 255, 0, 80 * alpha);
            ofDrawLine(0, y, _window.getWidth(), y);
        }
        else
        {
            ofSetColor(127, 80 * alpha);
            ofDrawLine(0, y, _window.getWidth(), y);
        }

        std::string label = Utils::format(Poco::LocalDateTime(time), formatStringHourMinSec);

        int width = _font.stringWidth(label);
        int height = _font.stringHeight(label);

        if (y - height - 4 > 0)
        {
            _font.drawString(label, _window.getWidth() - width - 4, y - 4);
        }

        ++hourIter;
    }

    int y = _window.getHeight() * _windowInterval.map(_now);

    ofSetColor(255);
    ofDrawLine(0, y, _window.getWidth(), y);

    std::string label = Utils::format(Poco::LocalDateTime(_now), formatStringHourMinSec);

    int width = _font.stringWidth(label);

    _font.drawString(label, _window.getWidth() - width - 4, y - 4);

    std::sort(_currentEvents.begin(), _currentEvents.end());


    ICalendar::EventInstances::const_iterator iter = _currentEvents.begin();

    int x = 0;

    while (iter != _currentEvents.end())
    {
        const ICalendarEvent& event = (*iter).getEvent();
        const Interval& interval = (*iter).getInterval();

        if (_windowInterval.intersects(interval))
        {
            int y0 = _window.getHeight() * _windowInterval.map(interval.getStart());
            int y1 = _window.getHeight() * _windowInterval.map(interval.getEnd());

            ofFill();
            ofSetColor(255, 50);

            if (interval.contains(_now))
            {
                ofSetColor(255, 255, 0, 50);
            }
            else
            {
                ofSetColor(255, 50);
            }


            ofDrawRectRounded(x, y0, 80, y1 - y0, 5);

            ofNoFill();
            ofSetColor(127);
            ofDrawRectRounded(x, y0, 80, y1 - y0, 5);

            ofSetColor(255);
            ofDrawRectRounded(x-1, y0-1, 80+2, y1 - y0+2, 5);


            std::string startLabel = Utils::format(Poco::LocalDateTime(interval.getStart()), formatStringHour);
            std::string endLabel = Utils::format(Poco::LocalDateTime(interval.getEnd()), formatStringHour);

            ofFill();
            ofSetColor(255);
            _font.drawString(event.getSummary(), x + 5, y0 + 10);
            _font.drawString(startLabel,         x + 5, y0 + 20);
            _font.drawString(endLabel,           x + 5, y0 + 30);
            
            x+= 84;
            
            if (x > _window.getWidth() - 160) x = 0;
            
        }
        
        ++iter;
    }
    
    ofPopMatrix();
}
//--------------------------------------------------------------
void ofApp::draw(){
    
    Poco::LocalDateTime now; //(2015,5,29,17,38);
    
    if(ofGetKeyPressed(OF_KEY_ALT)) {
        // auto step the time of day to proof changes
        int total_min = fabs(sin(ofGetElapsedTimef()*.05)) * 1440; // 1440 == mins per day  60 * 24
        int hr = floor(total_min/60.0);
        int mn = total_min - (hr*60); //now.minute();
        now.assign(now.year(), now.month(), now.day(), hr, mn);
    }
    
    float sun_brightness = ofxSunCalc::getSunBrightness(todayInfo, now);
    
    if(ofGetKeyPressed(OF_KEY_COMMAND)) {
        sun_brightness = fabs(sin(ofGetElapsedTimef()*.1));
    }
    
    // draw background gradient based on sun_brightness
    
    ofColor nightBG(ofColor::black);
    ofColor nightFG(64);
    
    ofColor dayBG(ofColor::skyBlue);
    ofColor dayFG(ofColor::paleGoldenRod);
    
    ofColor background = nightBG.lerp(dayBG, sun_brightness);
    ofColor foreground = nightFG.lerp(dayFG, sun_brightness);
    
    ofBackgroundGradient(foreground, background);
    
    ofDrawBitmapStringHighlight(date_str, 15, 20, ofColor::paleGoldenRod, ofColor::black);
    
    ofDrawBitmapStringHighlight(min_info_str, 15, 45, ofColor::salmon, ofColor::white);
    
    ofDrawBitmapStringHighlight(max_info_str, 15, 125, ofColor::darkorange, ofColor::white);
    
    ofDrawBitmapStringHighlight(latlon_str, 180, 20, ofColor::gold, ofColor::black);
    
    ofDrawBitmapStringHighlight(pos_str, 180, 45, ofColor::cornsilk, ofColor::black);
    
    ofDrawBitmapStringHighlight("Current Brightness " + ofToString(sun_brightness, 3), 180, 70, ofColor::goldenRod, ofColor::white);
    
    float tx = 10 + 110;
    float ty = 320;
    for(int i = 0; i<timelines.size(); i++) {
        
        ofSetColor(255);
        timelines[i].draw(tx, ty);
        
        ofDrawBitmapStringHighlight(labels[i], 10, ty+13);
        
        if(i == 2) { // today
            ofNoFill();
            ofSetLineWidth(1.0);
            ofSetColor(255);
            ofRect(tx, ty, timelines[i].getWidth(), timelines[i].getHeight());
            
            // Draw a current time mark
            float pixels_per_min = (timelines[i].getWidth() / 24) / 60.0;
            float nx = tx + pixels_per_min * (now.hour() * 60 + now.minute());
            ofSetColor(255, 0, 0);
            ofSetLineWidth(2.0);
            ofLine(nx, ty, nx, ty+timelines[i].getHeight());
        }
        
        ty += timelines[i].getHeight() + 25;
    }
    
}