Am 21.12.2013 01:37, schrieb Oliver Rath:
Hi List,
Ive just seen, that there is a new methon in cxxtools called
cxxtools::Timespan. What is it good for? Ive the code and maybe it is
good for solving an actuel problem i have:
Ich want to send some (static) files, if there are requested. If now the
sent file is requested again within a certein time-span (i.e. 5
minutes), the request should be rejected.
Do I search in the right direction?
Regards,
Oliver
Hi,
the class cxxtools::Timespan is not that new. As its name suggests it is
a representation of a timespan between 2 points in time. As such it is
e.g. the result of the operator- for 2 cxxtools::DateTime objects.
Recently I saw some interesting helper functions somewhere, which I
implemented in cxxtools now.
It has been quite irritating to create a Timespan object correctly. The
main constructor takes a int64_t. It specifies the number of
microseconds. So to get a timespan of one second, you have to use
Timespan(1000000), which is not really obvious. As an alternative there
is a constructor with 2 parameters: seconds and microseconds. A second
is then Timespan(1, 0). A millisecond is either Timespan(1000) or
Timespan(0, 1000). Neither obvious.
The helper functions are: microseconds(int64_t), milliseconds(double),
seconds(double), minutes(double), hours(double) and days(double). They
return a Timespan. So now it is possible to specify a timestamp of one
second by just saying cxxtools::seconds(1) or half a second is
cxxtools::seconds(0.5).
It helps in your situation but it is just part of the solution. Another
class you need is cxxtools::Clock, which is able to return the current
time as a cxxtools::DateTime. Now you can e.g. store the access time by
saying:
cxxtools::DateTime lastAccessTime = cxxtools::Clock::getLocalTime();
If in a later request you want to deny the access before 5 minutes are
gone, you say:
if (cxxtools::Clock::getLocalTime()- lastAccessTime <
cxxtools::minutes(5))
{
// too early
}
Previously the condition was a little different:
if (cxxtools::Clock::getLocalTime()- lastAccessTime <
cxxtools::TimeSpan(5*60*1000, 0))
which is harder to read.
Tommi
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general