2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/10/07 16:57:21 $
32 #include <algorithm> // std::max
35 #include "stg_timer.h"
37 //-----------------------------------------------------------------------------
38 TARIFF & TARIFF::operator=(const TARIFF_DATA & td)
43 //-----------------------------------------------------------------------------
44 TARIFF & TARIFF::operator=(const TARIFF & t)
46 tariffData = t.tariffData;
49 //-----------------------------------------------------------------------------
50 double TARIFF::GetPriceWithTraffType(uint64_t up,
55 return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
57 //-----------------------------------------------------------------------------
58 int64_t TARIFF::GetTraffByType(uint64_t up, uint64_t down) const
60 switch (tariffData.tariffConf.traffType)
69 return std::max(up, down);
71 default: //TRAFF_UP_DOWN:
75 //-----------------------------------------------------------------------------
76 int TARIFF::GetThreshold(int dir) const
78 return tariffData.dirPrice[dir].threshold;
80 //-----------------------------------------------------------------------------
81 void TARIFF::PrintTariff() const
83 //printfd(__FILE__, "Traiff name: %s\n", tariffConf.name.c_str());
84 //printfd(__FILE__, "Price: %8.3f %8.3f \n", dirPrice[0].GetPrice(0, 0), dirPrice[0].GetPrice(1, 0));
85 //printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[1].GetPrice(0), dirPrice[1].GetPrice(1), dirPrice[1].GetThreshold());
86 //printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[2].GetPrice(0), dirPrice[2].GetPrice(1), dirPrice[2].GetThreshold());
87 //printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[3].GetPrice(0), dirPrice[3].GetPrice(1), dirPrice[3].GetThreshold());
88 //printfd(__FILE__, "Free: %8.3f\n", tariffConf.free);
90 //-----------------------------------------------------------------------------
91 void TARIFF::GetTariffData(TARIFF_DATA * td) const
95 //-----------------------------------------------------------------------------
96 int TARIFF::Interval(int dir, time_t t) const
98 // Start of the day (and end of the night) in sec from 00:00:00
99 int s1 = tariffData.dirPrice[dir].hDay * 3600 +
100 tariffData.dirPrice[dir].mDay * 60;
101 // Start of the night (and end of the day) in sec from 00:00:00
102 int s2 = tariffData.dirPrice[dir].hNight * 3600 +
103 tariffData.dirPrice[dir].mNight * 60;
109 // Position of time t in sec from 00:00:00
110 // Ignoring seconds due to minute precision
111 int lts = lt->tm_hour * 3600 + lt->tm_min * 60;
115 // Normal situation (00:00:00 is a night)
116 if (lts > s1 && lts < s2)
123 // Not so common but possible situation (00:00:00 is a day)
124 if (lts < s1 && lts > s2)
130 //-----------------------------------------------------------------------------
131 double TARIFF::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const
133 int interval = Interval(dir, t);
142 bool nd = tariffData.dirPrice[dir].noDiscount;
143 bool sp = tariffData.dirPrice[dir].singlePrice;
144 bool th = (interval == TARIFF_NIGHT);
145 bool tr = (mb > tariffData.dirPrice[dir].threshold);
147 if (!nd && !sp && th && tr)
148 return tariffData.dirPrice[dir].priceNightB;
150 return tariffData.dirPrice[dir].priceDayB;
152 return tariffData.dirPrice[dir].priceNightA;
154 return tariffData.dirPrice[dir].priceDayA;
156 /*if (tariffData.dirPrice[dir].noDiscount && tariffData.dirPrice[dir].singlePrice)
158 return tariffData.dirPrice[dir].priceDayA;
162 if (tariffData.dirPrice[dir].noDiscount)
165 if (interval == TARIFF_DAY)
166 return tariffData.dirPrice[dir].priceDayA;
168 return tariffData.dirPrice[dir].priceNightA;
171 if (tariffData.dirPrice[dir].singlePrice)
174 if (mb < tariffData.dirPrice[dir].threshold)
175 return tariffData.dirPrice[dir].priceDayA;
177 return tariffData.dirPrice[dir].priceDayB;
180 if (mb < tariffData.dirPrice[dir].threshold)
182 if (interval == TARIFF_DAY)
183 return tariffData.dirPrice[dir].priceDayA;
185 return tariffData.dirPrice[dir].priceNightA;
189 if (interval == TARIFF_DAY)
190 return tariffData.dirPrice[dir].priceDayB;
192 return tariffData.dirPrice[dir].priceNightB;
196 //-----------------------------------------------------------------------------