aboutsummaryrefslogtreecommitdiffstats
path: root/MPD/TimeResolver.cpp
blob: 8ce044aea8f82acb2fa80b440ce9ab272e7ef737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * TimeResolver.cpp
 *****************************************************************************
 * Copyright (C) 2012, bitmovin Softwareentwicklung OG, All Rights Reserved
 *
 * Email: libdash-dev@vicky.bitmovin.net
 *
 * This source code and its use and distribution, is subject to the terms
 * and conditions of the applicable license agreement.
 *****************************************************************************/

#include "TimeResolver.h"
#include "sstream"

using namespace libdash::framework::mpd;

bool TimeResolver::checkTimeInterval(std::string availabilityStartTime, std::string availabilityEndTime)
{
    struct tm* startTime    = TimeResolver::resolveUTCDateTime(availabilityStartTime);
    struct tm* currentTime  = TimeResolver::getCurrentUTCTime();
    struct tm* endTime      = TimeResolver::resolveUTCDateTime(availabilityEndTime);

    if (!startTime)
    {
        if (!endTime)
            return true;

        if (difftime(mktime(endTime),mktime(currentTime)) > 0)
            return true;
    }
    else 
    {
        if (difftime(mktime(currentTime),mktime(startTime)) > 0)
        {
            if (!endTime)
                return true;

            if (difftime(mktime(endTime),mktime(currentTime)) > 0)
                return true;
        }
    }

    return false;
}

uint32_t TimeResolver::getCurrentTimeInSec()
{
    return mktime(TimeResolver::getCurrentUTCTime());
}
uint32_t TimeResolver::getUTCDateTimeInSec(const std::string& datetime)
{
    return mktime(TimeResolver::resolveUTCDateTime(datetime));
}
double TimeResolver::getDurationInSec(const std::string& duration)
{
    /* no check for duration with yyyy,dd,mm */
    if (duration == "" || duration.substr(0, 2) != "PT")
        return 0;

    size_t      startPos    = 2;
    size_t      endPos      = std::string::npos;
    uint32_t    hours       = 0;
    uint32_t    mins        = 0;
    double      secs        = 0;

    char designators[] = { 'H', 'M', 'S' };

    endPos = duration.find(designators[0], startPos);
    if (endPos != std::string::npos)
    {
        hours = strtol(duration.substr(startPos, endPos - startPos).c_str(), NULL, 10);
        startPos = endPos + 1;
    }
    
    endPos = duration.find(designators[1], startPos);
    if (endPos != std::string::npos)
    {
        mins = strtol(duration.substr(startPos, endPos - startPos).c_str(), NULL, 10);
        startPos = endPos + 1;
    }

    endPos = duration.find(designators[2], startPos);
    if (endPos != std::string::npos)
        secs = strtod(duration.substr(startPos, endPos - startPos).c_str(), NULL);

    return hours*3600 + mins*60 + secs;
}

struct tm* TimeResolver::resolveUTCDateTime(const std::string& dateTimeString)
{
    if (dateTimeString == "")
        return NULL;

    time_t rawtime;
    struct tm* timeInfo;
    time ( &rawtime );
    timeInfo = gmtime ( &rawtime );

    std::string timeString = dateTimeString.substr();

    timeString = timeString.substr(0, timeString.size()-1);

    std::vector<std::string> dateTime   = splitToStr(timeString, 'T');
    std::vector<int>         dateChunks = splitToI(dateTime.at(0), '-');
    std::vector<int>         timeChunks = splitToI(dateTime.at(1), ':');

    timeInfo->tm_year = dateChunks.at(0) - 1900;
    timeInfo->tm_mon  = dateChunks.at(1) - 1;
    timeInfo->tm_mday = dateChunks.at(2);

    timeInfo->tm_hour = timeChunks.at(0);
    timeInfo->tm_min  = timeChunks.at(1);
    timeInfo->tm_sec  = timeChunks.at(2);

    return timeInfo;
}

struct tm* TimeResolver::getCurrentUTCTime()
{
    time_t      rawTime;

    time(&rawTime);
    return gmtime(&rawTime);
}

std::vector<int> TimeResolver::splitToI(const std::string &s, char delim)
{
    std::stringstream   ss(s);
    std::string         item;
    std::vector<int>    integers;

    while(std::getline(ss, item, delim))
        integers.push_back((int)strtol(item.c_str(), NULL, 10));

    return integers;
}

std::vector<std::string> TimeResolver::splitToStr(const std::string &s, char delim)
{
    std::stringstream           ss(s);
    std::string                 item;
    std::vector<std::string>    strings;

    while(std::getline(ss, item, delim))
        strings.push_back(item);

    return strings;
}