vdr-plugin-tvguide/timemanager.c

179 lines
4.4 KiB
C
Raw Normal View History

2019-07-11 11:07:13 +02:00
#include <time.h>
#include <vdr/tools.h>
#include "config.h"
2019-07-11 14:40:41 +02:00
#include "timemanager.h"
2019-07-11 11:07:13 +02:00
2020-01-06 12:51:49 +01:00
cTimeManager::cTimeManager(void) {
if (config.displayMode == eVertical) {
displaySeconds = (geoManager.osdHeight - geoManager.statusHeaderHeight - geoManager.channelHeaderHeight - geoManager.channelGroupsHeight - geoManager.footerHeight) / geoManager.minutePixel * 60;
} else if (config.displayMode == eHorizontal) {
displaySeconds = (geoManager.osdWidth - geoManager.channelHeaderWidth - geoManager.channelGroupsWidth) / geoManager.minutePixel * 60;
}
}
2019-07-11 14:34:31 +02:00
cTimeManager::~cTimeManager(void) {
2019-07-11 11:07:13 +02:00
}
2019-07-11 14:34:31 +02:00
cString cTimeManager::printTime(time_t displayTime) {
2019-07-11 11:07:13 +02:00
struct tm *ts;
ts = localtime(&displayTime);
cString strTime = cString::sprintf("%d.%d-%d:%d.%d", ts->tm_mday, ts->tm_mon+1, ts->tm_hour, ts->tm_min, ts->tm_sec);
2019-07-11 11:07:13 +02:00
return strTime;
}
2019-07-11 14:34:31 +02:00
void cTimeManager::Now() {
2019-07-11 11:07:13 +02:00
t = time(0);
tStart = t;
tStart = GetRounded();
2020-01-06 12:51:49 +01:00
tEnd = tStart + displaySeconds;
2019-07-11 11:07:13 +02:00
}
2019-07-11 14:34:31 +02:00
void cTimeManager::AddStep(int step) {
2020-01-06 12:51:49 +01:00
tStart += step * 60;
tEnd = tStart + displaySeconds;
2019-07-11 11:07:13 +02:00
}
2020-01-06 12:51:49 +01:00
void cTimeManager::DelStep(int step) {
if ((tStart - step * 60) + 30 * 60 < t) {
Now();
} else {
tStart -= step * 60;
tEnd = tStart + displaySeconds;
2019-07-11 11:07:13 +02:00
}
}
2019-07-11 14:34:31 +02:00
void cTimeManager::SetTime(time_t newTime) {
2019-07-11 11:07:13 +02:00
tStart = newTime;
2020-01-06 12:51:49 +01:00
tEnd = tStart + displaySeconds;
2019-07-11 11:07:13 +02:00
}
2019-07-11 14:34:31 +02:00
time_t cTimeManager::getPrevPrimetime(time_t current) {
2019-07-11 11:07:13 +02:00
tm *st = localtime(&current);
if (st->tm_hour < 21) {
2020-01-06 12:51:49 +01:00
current -= 24 * 60 * 60;
2019-07-11 11:07:13 +02:00
st = localtime(&current);
}
st->tm_hour = 20;
st->tm_min = 0;
2019-07-11 11:07:13 +02:00
time_t primeTime = mktime(st);
return primeTime;
}
2019-07-11 14:34:31 +02:00
time_t cTimeManager::getNextPrimetime(time_t current){
2019-07-11 11:07:13 +02:00
tm *st = localtime(&current);
if (st->tm_hour > 19) {
2020-01-06 12:51:49 +01:00
current += 24 * 60 * 60;
2019-07-11 11:07:13 +02:00
st = localtime(&current);
}
st->tm_hour = 20;
st->tm_min = 0;
2019-07-11 11:07:13 +02:00
time_t primeTime = mktime(st);
return primeTime;
}
2019-07-11 14:34:31 +02:00
bool cTimeManager::tooFarInPast(time_t current) {
2019-07-11 11:07:13 +02:00
if (current < t) {
return true;
}
return false;
}
2019-07-11 14:34:31 +02:00
cString cTimeManager::GetCurrentTime() {
2019-07-11 11:07:13 +02:00
char buf[25];
t = time(0);
tm *st = localtime(&t);
//snprintf(text, sizeof(text), "%d:%02d", st->tm_hour, st->tm_min);
2019-07-11 11:28:11 +02:00
if (config.timeFormat == e12Hours) {
2019-07-11 11:07:13 +02:00
strftime(buf, sizeof(buf), "%I:%M %p", st);
2019-07-11 11:28:11 +02:00
} else if (config.timeFormat == e24Hours)
2019-07-11 11:07:13 +02:00
strftime(buf, sizeof(buf), "%H:%M", st);
return buf;
}
2019-07-11 14:34:31 +02:00
cString cTimeManager::GetDate() {
2019-07-11 11:07:13 +02:00
char text[6];
tm *st = localtime(&tStart);
snprintf(text, sizeof(text), "%d.%d", st->tm_mday, st->tm_mon+1);
return text;
}
2019-07-11 14:34:31 +02:00
cString cTimeManager::GetWeekday() {
2019-07-11 11:07:13 +02:00
return WeekDayName(tStart);
}
2019-07-11 14:34:31 +02:00
int cTimeManager::GetTimelineOffset() {
2019-07-11 11:07:13 +02:00
tm *st = localtime(&tStart);
int offset = st->tm_hour*60;
2019-07-11 11:07:13 +02:00
offset += st->tm_min;
return offset;
}
2019-07-11 14:34:31 +02:00
time_t cTimeManager::GetRounded() {
2019-07-11 11:07:13 +02:00
tm *rounded = localtime ( &tStart );
rounded->tm_sec = 0;
if (rounded->tm_min > 29)
rounded->tm_min = 30;
else
rounded->tm_min = 0;
return mktime(rounded);
}
2019-07-11 14:34:31 +02:00
bool cTimeManager::NowVisible(void) {
2019-07-11 11:07:13 +02:00
if (t > tStart)
return true;
return false;
}
2019-07-11 14:34:31 +02:00
void cTimeManager::debug() {
2019-07-11 11:07:13 +02:00
esyslog("t: %s, tStart: %s, tEnd: %s", *TimeString(t), *TimeString(tStart), *TimeString(tEnd));
}
// --- cTimeInterval -------------------------------------------------------------
cTimeInterval::cTimeInterval(time_t start, time_t stop) {
this->start = start;
this->stop = stop;
}
cTimeInterval::~cTimeInterval(void) {
}
cTimeInterval *cTimeInterval::Intersect(cTimeInterval *interval) {
time_t startIntersect, stopIntersect;
if ((stop <= interval->Start()) || (interval->Stop() <= start)) {
return NULL;
}
if (start <= interval->Start()) {
startIntersect = interval->Start();
} else {
startIntersect = start;
}
if (stop <= interval->Stop()) {
stopIntersect = stop;
} else {
stopIntersect = interval->Stop();
}
return new cTimeInterval(startIntersect, stopIntersect);
}
cTimeInterval *cTimeInterval::Union(cTimeInterval *interval) {
time_t startUnion, stopUnion;
if (start <= interval->Start()) {
startUnion = start;
} else {
startUnion = interval->Start();
}
if (stop <= interval->Stop()) {
stopUnion = interval->Stop();
} else {
stopUnion = stop;
}
return new cTimeInterval(startUnion, stopUnion);
}