File: WorldTime.cpp

package info (click to toggle)
cyphesis-cpp 0.5.16-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,084 kB
  • ctags: 3,627
  • sloc: cpp: 30,418; python: 4,812; xml: 4,674; sh: 4,118; makefile: 902; ansic: 617
file content (133 lines) | stat: -rw-r--r-- 4,387 bytes parent folder | download
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
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000,2001 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

// $Id: WorldTime.cpp,v 1.13 2006-10-26 00:48:06 alriddoch Exp $

#include "WorldTime.h"

#include "common/debug.h"

#include <iostream>

#include <list>
#include <map>
#include <string>
#include <stdio.h>

//timedata time2type(const std::string & t) {
    //return timeInfo[t][1];
//}


// time2importance=time2type;

static const bool debug_flag = false;

static inline double minutes(const double & n) { return 60.0*n; }
static inline double hours(const double & n) { return minutes(60)*n; }
static inline double days(const double & n) { return hours(24)*n; }
static inline double months(const double & n) { return days(30)*n; }
static inline double years(const double & n) { return months(12)*n; }

inline const std::string seconds2string(double seconds) {
    char buffer[ 100 ];
    snprintf( buffer, 100, "%f", seconds );
    return buffer;
}

void WorldTime::initTimeInfo()
{
    m_timeInfo["always"] = Period(crange(0,12), "seasonal");
    m_timeInfo["spring"] = Period(crange(2,4), "seasonal");
    m_timeInfo["summer"] = Period(crange(5,7), "seasonal");
    m_timeInfo["autumn"] = Period(crange(8,10), "seasonal");
    Range winter = crange(0,1);
    winter.push_back(11);
    m_timeInfo["winter"] = Period(winter, "seasonal");
    m_timeInfo["dawn"] = Period(Range(1,8), "daily");
    m_timeInfo["midday"] = Period(Range(1,12), "daily");
    m_timeInfo["evening"] = Period(Range(1,20), "daily");
    Range night = crange(0,7);
    night.push_front(23); night.push_front(22); night.push_front(21);
    m_timeInfo["night"] = Period(night,"daily");
    m_timeInfo["day"] = Period(crange(9,18), "daily");
    m_timeInfo["morning"] = Period(crange(9,11), "daily");
    m_timeInfo["afternoon"] = Period(crange(13,18), "daily");
    m_timeInfo["now"] = Period(crange(0,23), "daily");
    std::list<std::string> seasons(1, "summer");
    seasons.push_back("autumn");
    seasons.push_back("winter");
    seasons.push_back("spring");
    std::list<std::string>::const_iterator I = seasons.begin();
    std::list<std::string>::const_iterator Iend = seasons.end();
    for (; I != Iend; ++I) {
        Range & months = m_timeInfo[*I].first;
        Range::const_iterator Jend = months.end();
        for(Range::const_iterator J = months.begin(); J != Jend; ++J) {
            m_monthToSeason[*J] = *I;
        }
    }
}


//WorldTime::WorldTime(char * date_time="1-1-1 0:0:0")
//{
    //// time=DateTime(date_time);
//}

std::string WorldTime::operator[](const std::string & name)
{
    if (name == "season") {
        SeasonInfo::const_iterator I = m_monthToSeason.find(m_time.month());
        if (I != m_monthToSeason.end()) {
            return I->second;
        }
    }
    return "";
}

bool WorldTime::operator==(const WorldTime & other) const
{
    return m_time == other.m_time;
}

bool WorldTime::operator==(const std::string & when) const
{
    debug(std::cout << "Checking whether it is " << when << " when the date is "
                    << m_time << std::endl << std::flush;);
            
    TimeInfo::const_iterator I = m_timeInfo.find(when);
    if (I == m_timeInfo.end()) {
        return false;
    }
    int check;
    if (I->second.second == "seasonal") {
        check = m_time.month();
    } else if (I->second.second == "daily") {
        check = m_time.hour();
    } else {
        return false;
    }
    std::list<int>::const_iterator J = I->second.first.begin();
    std::list<int>::const_iterator Jend = I->second.first.end();
    for (; J != Jend; ++J) {
        if (check == *J) {
            return true;
        }
    }
    return false;
}