This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreCode.cpp
executable file
·109 lines (84 loc) · 2.01 KB
/
PreCode.cpp
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
#include "PreCode.hpp"
namespace gg = boost::gregorian;
namespace pt = boost::posix_time;
//PreCode
PreCode::PreCode (std::string URL, pt::time_duration Delay,
std::string Cache, std::string Lock) :
m_URL (URL), m_Cache (Cache, Lock), m_Delay (Delay)
{
}//PreCode
//Get
std::string PreCode::Get ()
{
AutoUpdate (this);
return GetCache ();
}//Get
//IsOutdated
bool PreCode::IsOutdated () const
{
try
{
return (pt::second_clock::local_time () >= GetExpiry ());
}
catch (Err)
{
return true;
}
}//IsOutdated
//Update
void PreCode::Update ()
{
GetData Data (m_URL);
m_Cache.Set (&Data);
}//Update
//GetCache
std::string PreCode::GetCache () const
{
if (!IsCacheValid ()) throw Err ("There is no valid cache value");
return m_Cache.Get ()[1];
}//GetCache
//GetCreated
gg::date PreCode::GetCreated () const
{
if (!IsCacheValid ()) throw Err ("Invalid creation date");
gg::date Min (gg::min_date_time);
return Min + gg::days (STN<long> (m_Cache.Get ()[0]));
}//GetCreated
//GetExpiry
pt::ptime PreCode::GetExpiry () const
{
gg::date Created (GetCreated ());
//is creation date on another month as the expiry
if (pt::ptime (Created) >= ExpiryOfMonth (Created))
Created += gg::months (1);
return ExpiryOfMonth (Created);
}//GetExpiry
//AutoUpdate
void PreCode::AutoUpdate (PreCode *Val)
{
if (Val->IsOutdated ()) Val->Update ();
}//AutpUpdate
//IsCacheValid
bool PreCode::IsCacheValid () const
{
return (m_Cache.Get ().size () > 1);
}//IsCacheValid
//ExpiryOfMonth
pt::ptime PreCode::ExpiryOfMonth (gg::date Month) const
{
return pt::ptime (gg::date (Month.year (), Month.month (), 1), m_Delay);
}//ExpiryOfMonth
//GetData
GetData::GetData (std::string URL) :
m_URL (URL)
{
}//GetData
//operator()
std::vector<std::string> GetData::operator() ()
{
std::vector<std::string> Data (2);
gg::date Cur = gg::day_clock::local_day ();
Data[0] = NTS<char> ((Cur - gg::date (gg::min_date_time)).days ());
Data[1] = GetFreshPreCode (m_URL);
return Data;
}//operator()