I have a custom solution that checks if a web-site is online, every 1 hour.
The result is something like this: (there is one entry per day)
There are 24 entries in a day (every hour). And every hour has a availability percentage (100%, 95%, 0% ... and so forth).
My questions are:
how can I get up-time and downtime in percentage?
how can I calculate up-time and downtime using these values between 2 dates DateTime and get the result in seconds?
The up-time is easy:
sum of all values divided by 24 (entries) which is 99.67%
Then for downtime, can I just do 100 - 99.67 = 0.33 % ?
I have some issues in getting the up-time/downtime in seconds between 2 dates DateTime
For example: if the range dates are 20.04.2015 - 26.05.2015
I think I should do like this:
For each day calculate the up-time as above (sum of all daily values divided by 24 entries) and assuming the following values:
20.04.2015: 96.67%
21.04.2015: 100.00%
22.04.2015: 92.00%
23.04.2015: 96.67%
24.04.2015: 100.00%
25.04.2015: 100.00%
26.04.2015: 100.00%
Now the sum of the above values divided by the number of days (7 days between 20.04 and 26.04) which makes 97.91 % availability between 20.04 and 26.04.
I assume to get 97.91 % up-time in seconds I should do like this:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime first = DateTime.ParseExact("20.04.2015", "dd.MM.yyyy", provider);
DateTime last = DateTime.ParseExact("26.04.2015", "dd.MM.yyyy", provider);
TimeSpan time = last - first;
decimal secondsUptime = ((time.Days * 86400)/100) * 97.91m; //97.91 is the uptime calculated above for the time period.
// The problem is how to get this?
decimal secondsDowntime = ?
What I have done so far, is this the correct approach?
How to get the secondsDowntime?
If you already have the Up time percent, All you have to do is very simple math:
decimal PercentDownTime = 100 - PercentUpTime;
decimal SecondsUpTime = (time.Seconds / 100) * PercentUpTime;
decimal SecondsDownTime = (time.Seconds / 100) * PercentDownTime;
Thanks to Robert for reminding us the TotalSeconds property of TimeSpan, but In this case it's ok to use the Seconds property.
Hold up... Ur input numbers are wrong. If u measure once per hour and display the hourly results than how does ur 11:00 have 97%? It should be 100% or 0% its either up or down that hour.... Unless u actually do more calculations in that hour... But even then to get 97 u would have to do at least 100 tests that hour and 3 of them were down.
But if ur numbers are correct the formulas are this...
Uptime % = (sum of the uptime percentages) / (number of measurements) × 100
Uptime % = (sum of uptime) / 24 × 100
Downtime % = 100 - (uptime %)
Seconds of uptime = (number of hours measured or to consider) × 3600 × (uptime %) / 100
Seconds of downtime = (number of hours measure or to consider × 3600) - (seconds of uptime)
Note 3600 is the number of sec in an hour so we multiply the hours by that to get us to the wanted time unit of seconds.
Note Replace uptime % with a number between 0 and 100. Not 0 and 1. If u use 0 to 1 then dont do the last division by 100.
Related
I'm trying to find better way to convert DateTime to unix timestamp in C#
I find out that there is a DateTimeOffset.ToUnixTimeMilliseconds method:
public long ToUnixTimeMilliseconds()
{
return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
What this method means? What the constants are used?
UPD:
I guess 10000L is converting from FRAME to Milliseconds. But what about 62135596800000L?
To explain this method:
public long ToUnixTimeMilliseconds()
{
return this.UtcDateTime.Ticks / 10000L - 62135596800000L;
}
DateTime.Ticks units are 100 nanosecond intervals.
Dividing this by 10_000 yields milliseconds, which explains the division by 10000L.
This is because one nanosecond is one billionth of a second, or one millionth of a millisecond.
To convert a nanosecond to a millisecond you would therefore divide by 1_000_000.
However, the ticks are 100 nanosecond units, so instead of dividing by 1_000_000 you would have to divide by 1_000_000/100 = 10_000. That's why you divide the 100 nanosecond units by 10_000 to get milliseconds.
The Unix epoch (which corresponds to a Unix time of zero) is midnight on 1st January 1970.
The DateTime epoch (which corresponds to a DateTime.Ticks value of zero) is 1st January 0001.
The number of milliseconds between 1st January 0001 and 1st January 1970 is 62135596800000. This explains the subtraction of 62135596800000.
And there you have it!
Note: You can compute an approximate value for the number of milliseconds as follows:
Approximate number of days per year = 365.24219
Number of years between 0001 and 1970 = 1969
Thus, total approx milliseconds = 1969 * 365.24219 * 24 * 60 * 60 * 1000
= 62135585750000
The exact figure is much harder to calculate, but it comes out to 62135596800000 as used in the formula above.
In fact, from inspection of the source code we can find the following:
public long ToUnixTimeSeconds() {
// Truncate sub-second precision before offsetting by the Unix Epoch to avoid
// the last digit being off by one for dates that result in negative Unix times.
//
// For example, consider the DateTimeOffset 12/31/1969 12:59:59.001 +0
// ticks = 621355967990010000
// ticksFromEpoch = ticks - UnixEpochTicks = -9990000
// secondsFromEpoch = ticksFromEpoch / TimeSpan.TicksPerSecond = 0
//
// Notice that secondsFromEpoch is rounded *up* by the truncation induced by integer division,
// whereas we actually always want to round *down* when converting to Unix time. This happens
// automatically for positive Unix time values. Now the example becomes:
// seconds = ticks / TimeSpan.TicksPerSecond = 62135596799
// secondsFromEpoch = seconds - UnixEpochSeconds = -1
//
// In other words, we want to consistently round toward the time 1/1/0001 00:00:00,
// rather than toward the Unix Epoch (1/1/1970 00:00:00).
long seconds = UtcDateTime.Ticks / TimeSpan.TicksPerSecond;
return seconds - UnixEpochSeconds;
}
// Number of days in a non-leap year
private const int DaysPerYear = 365;
// Number of days in 4 years
private const int DaysPer4Years = DaysPerYear * 4 + 1; // 1461
// Number of days in 100 years
private const int DaysPer100Years = DaysPer4Years * 25 - 1; // 36524
// Number of days in 400 years
private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097
// Number of days from 1/1/0001 to 12/31/1600
private const int DaysTo1601 = DaysPer400Years * 4; // 584388
// Number of days from 1/1/0001 to 12/30/1899
private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
// Number of days from 1/1/0001 to 12/31/1969
internal const int DaysTo1970 = DaysPer400Years * 4 + DaysPer100Years * 3 + DaysPer4Years * 17 + DaysPerYear; // 719,162
Which we can now use to calculate the number of milliseconds to 1970:
719162 (DaysTo1970) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds)
= 62135596800000
621355968000000000 is the correct amount of Ticks defined by DateTime
The accepted answer makes it appear like DateTime may produce undefined output different from what you can expect if you actually use it.
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks
// 621355968000000000
Run it online yourself: https://dotnetfiddle.net/jU0SIp
/// <summary>
/// Convert Ticks to Unix Timestamp
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static long ToUnixTimestamp(long time)
{
return (time - 621355968000000000) / TimeSpan.TicksPerMillisecond;
}
You could replace TimeSpan.TicksPerMillisecond with 10000 if you wanted (like your example), It is just a constant for There is 10,000 ticks in a millisecond
I have completed Loan Amortization/ Loan Repayment calculation for monthly. Now i required for weekly calculation formula in c#.
Can any one please help me to answer this?
Here loan amount is 10000, Interest Rate is 10% and Terms 7 Months
This is my monthly calculation. but i need weekly calculation like this
loanAmount = 10000;
var periods = 7; //Months
var monthlyRate = (Convert.ToDouble(10) / 100) / 12;
var monthlyPayment = Math.Round(monthlyRate / (1 - Math.Pow((1 + monthlyRate), -(periods))) * loanAmount ,2);
Thanks in advance,
Mani
Without seeing exactly how you are calculating it, it should be as simple as switching out months for weeks. For example, instead of something like this:
monrthly payments = (balance * APR) / 12 months
Do this:
weekly payments = (balance * APR) / 52 weeks
Obviously you'll change it depending on what you're calculating and how you're calculating it. You've already done the hard part now you just have to change the time frame, does that make sense?
I am trying to figure out final price based on wage multiplied by hours.
Here is my code
Why if the hours are 02:30:00 and the wage: $14 is my total 2?
TimeSpan duration = TimeSpan.Parse("02:30:00");
int wage = 14;
var result = (duration.Hours + (duration.Minutes / 60) * wage);
First, the expression is actually evaluated as:
duration.Hours + ((duration.Minutes / 60) * cleaner.Price)
Second, you are doing integer division, so 30/60 will result in 0, leaving you with the value 2 i.e. the Hours part.
To fix the issue, you can do something like the below:
(duration.Hours + ((decimal)duration.Minutes / 60)) * 14;
Alternatively, you can skip this component-wise calculation, and just use the TotalHours property instead:
duration.TotalHours * 14
I want to know how to calculate different pay rates between certain hours.
For example: If someone works from 9am - 5pm they will recieve 1x rate.
if that same person works from 9am - 7pm they will get 1x rate from 9am-5pm and then 1.5x rate from 5pm - 7pm.
However, I am currently using a DateTime.Now for when they start work and a DateTime.Now when they finish the using a time span to calculate the hours in between.
I haven't tried anything because I dunno how to do it.
Here's a try:
static double Rate = 20.0; // 20$ per hour 9am to 5pm
static double TotalPayment(DateTime startTime, DateTime endTime)
{
if (startTime > endTime)
throw new ArgumentException("start must be before end");
if (startTime.Date != endTime.Date)
throw new NotImplementedException("start and end must be on same day");
double totalHours = (endTime - startTime).TotalHours;
double startOfOrdinaryRate = Math.Max(9.0, startTime.TimeOfDay.TotalHours);
double endOfOrdinaryRate = Math.Min(17.0, endTime.TimeOfDay.TotalHours);
double ordinaryHours;
if (startOfOrdinaryRate > endOfOrdinaryRate)
ordinaryHours = 0.0;
else
ordinaryHours = endOfOrdinaryRate - startOfOrdinaryRate;
return 1.0 * Rate * ordinaryHours
+ 1.5 * Rate * (totalHours - ordinaryHours);
}
Do test if it works.
If I have 50% weight on 6/3/2011 and 50% weight on 6/1/2011, the weighted average will be 6/2/2011.
Now, I can't seem to figure out how I can do this with uneven weights, since it's not like you can multiply a DateTime by a double, and sum up the results (or can you?).
DateTime dateA = ...;
DateTime dateB = ...;
TimeSpan difference = dateA - dateB;
double units = difference.Ticks;
// Do your weighted logic here on 'units'.
DateTime average = dateA + new TimeSpan(units);
Something like the above (you get the idea - basically need to normalise the difference into a format you can work with, i.e. ticks, etc.).
You can find the difference between two dates(start and end) in terms of days. Apply the weight on the difference_days and get the final output date by startdate + weightedDays
You can't multiply a datetime by a double, but you can set a value for date1 and date2 on a scale (1 to 100) and figure out where the value you would be in the middle. The 1 versus 100 ends up on 50 in your 50/50 scenario.
You then have to figure the number of days in the range. You can then multiply by the weighted decimal (as a percent) and turn that into number of days. Then add that number of days to the first value.
Since you can turn dates into numbers, this gives some pretty interesting other means of accomplishing this. A TimeSpan is one way of setting this up as a number.
Use the long timestamps of the DateTime Objects.
Can you use the Ticks property of DateTime? Something like:
DateTime firstDate = new DateTime(2011, 6, 5);
DateTime secondDate = new DateTime(2011, 6, 1);
double weight1 = 0.4;
double weight2 = 0.6;
var averageTicks = (firstDate.Ticks * weight1) + (secondDate.Ticks * weight2) / 2;
DateTime averageDate = new DateTime(Convert.ToInt64(averageTicks));
I think the weighted average formula would be sumproduct (Weights*Dates)/sum (Weights)
Where sumproduct means the sum of all factors. Factors being the multiplication of weights and date.ticks. If sum of weights=1 then only the numerator remains.
i had the same problem, only with money(cashflows) which means there is a compounding\discounting rate of growth\decay of value known as time value of money)...
In order to enable you to avoid that mistake i composed a spreadsheet found at:
https://1drv.ms/x/s!AqGuYeJW3VHggc9ARWCxcHIeodd2Pg
You just have to experiment with it!!!...And understand that at 0% interest rate the results are identical to the weighted average described in other posts but as rate increases there is deviation. Then you gotta build an XNPV function for your programming language and fuse it with your app.
also there are a number of examples with growth in algebra books.maybe there is an intersection with them and datetime-offsets found in cashflows