Timespan Arithmetics to predict tide - c#

I am trying to predict tide in c# using a formula given below:
The program was already done in excel, however when translating it to c# I am encountering some problems.
This is the formula in excel:
=PI()*((C18)/(C19)+1)
Cells C18 and C19 just contain subtraction of two date cells each
In C# I am having trouble finding A.
There are 2 problematic areas in finding A.
Problem1: When I do (t - t1)/(t2 - t1) the answer in C# is 0, but in excel the answer is 1.
Problem 2: the +1 at the end of equation. How do you add a number to timeSpan(in excel this works, maybe because they parse it as a datetime value?)
Here is my code:
DateTime t = new DateTime(2016, 6, 21, 13, 41, 00);
DateTime t1 = new DateTime(2016, 6, 21, 7, 13, 00);
DateTime t2 = new DateTime(2016, 6, 21, 13, 57, 00);
double h1 = 1.421;
double h2 = 2.337;
double h2_minus_h1 = h2 - h1;
TimeSpan t_minus_t1 = TimeSpan.FromTicks(t.Subtract(t1).Ticks);
TimeSpan t2_minus_t1 = TimeSpan.FromTicks(t2.Subtract(t1).Ticks);
TimeSpan MainCalculationForA = TimeSpan.FromTicks((t.Subtract(t1).Ticks) / (t2.Subtract(t1).Ticks));
MessageBox.Show(MainCalculationForA.ToString());
The answer for this which is (t - t1)/(t2 - t1) is: 00:00:00, while in excel it is just 1
Note:The formula is taken from here
tide prediction pdf
note:(t - t1) and (t2 - t1) both have exactly the same answer in both C# and excel. So why the difference in answer upon division?

From the PDF provided, t, t1 and t2 are in decimal hours, so you need .TotalHours instead
DateTime t = new DateTime(2016, 6, 21, 13, 41, 00);
DateTime t1 = new DateTime(2016, 6, 21, 7, 13, 00);
DateTime t2 = new DateTime(2016, 6, 21, 13, 57, 00);
double h1 = 1.421;
double h2 = 2.337;
double t_min_t1 = (t - t1).TotalHours;
double t2_min_t1 = (t2 - t1).TotalHours;
double A = Math.PI*(t_min_t1/t2_min_t1 + 1);
double h = h1 + (h2 - h1)*((Math.Cos(A) + 1)/2);
MessageBox.Show(h.ToString());
The end result is 2.33345960154238.

Related

c# detect when day changes

In my app I've a ticker which runs every 5 seconds. I've also an internal clock and I want to detect when the day chages.
To test, I have tried the following code without success:
DateTime A = new DateTime(2019, 6, 20, 23, 58, 29);
DateTime B = new DateTime(2019, 6, 21, 00, 01, 12);
Int32 dd = (B-A).Days; // it returns 0
double dd = (B-A).TotalDays; // it return 0.00002136213
If I check if TotalDays > 0 I succesfully detect the day change but in the follwing case (with a difference of a minute)
DateTime C = new DateTime(2019, 6, 20, 12, 58, 29);
DateTime D = new DateTime(2019, 6, 20, 12, 59, 29);
the compare fails. Since I need to call a method when day changes, with the example above its called every time and I do not want this behevior. Any hint?
compare Date part of DateTime directly
bool isSameDay = (A.Date == B.Date);
Look at only the Date parts
DateTime A = new DateTime(2019, 6, 20, 23, 58, 29);
DateTime B = new DateTime(2019, 6, 21, 00, 01, 12);
Int32 dd = (B.Date-A.Date).Days;
For your ticker why not use TimeSpan variables to complete your comparison. You set 1 static timespan variable to 24 hours (1 day) and then create a secondary one to store the values. You then set your second timespan variable equal to the subtraction of your two days and this would let you know if a day had gone by.
`
TimeSpan newDayReference = new TimeSpan(24,0,0);
TimeSpan comparison;
//These two variables set to show difference.
DateTime A= DateTime.Now.AddDays(-1);
DateTime B = DateTime.Now;
comparison = B - A;
if(comparison > newDayReference){ //success }
`

Algorithm to find the closest time

I have a list of DateTime values with dates that contain hours and minutes:
List<DateTime> times = times = new List<DateTime>()
{
new DateTime(2019, 01, 01, 17, 00, 00),
new DateTime(2019, 01, 01, 18, 45, 00),
new DateTime(2019, 01, 01, 19, 00, 00),
new DateTime(2019, 01, 01, 19, 30, 00),
new DateTime(2019, 01, 01, 22, 30, 00)
};
DateTime current = DateTime.Now;
I put them all in a ComboBox, and I want to make some sort of algorithm so when I load my form, it will check for the current time and find the closest value to the current time and select the ComboBox item that contains that hour.
How can I achieve this? I tried to loop through them all and check for the least hour, but that doesn't seem to work. Is there a smarter way to do it?
For example: If the current time is 17:32, it will choose 17:00, because that's the closest. But, if the current time is 18:20, it will choose 18:45 and so on.
Compare to the Ticks property of DateTime (MSDN). It can be seen as a linear representation of the whole date and timestamp and is sortable.
Do something like
comboBox.SelectedItem = times.OrderBy(t => Math.Abs(t.Ticks - current.Ticks)).First()
You could take the difference with DateTime.Now for all your datetimes, order by this difference and take the first result.
times.OrderBy(m => Math.Abs((DateTime.Now - m).TotalMilliseconds)).First();
You would have to select an instance of DateTime which minimizes the temporal distance to the current time. You could use an extension method for IEnumerable<T> to do that as follows.
public static T ArgMin<T, R>(T t1, T t2, Func<T, R> f)
where R : IComparable<R>
{
return f(t1).CompareTo(f(t2)) > 0 ? t2 : t1;
}
public static T ArgMin<T, R>(this IEnumerable<T> Sequence, Func<T, R> f)
where R : IComparable<R>
{
return Sequence.Aggregate((t1, t2) => ArgMin<T, R>(t1, t2, f));
}
var iNow = DateTime.Now;
var iResult = times.ArgMin(iTime => Math.Abs((iTime - iNow).Ticks));
Although very generic, this implementation does not involve any sorting.
You are looking for ArgMax which is not implemented in standard Linq, but can be emulated via Aggreagte
List<DateTime> times = new List<DateTime>() {
new DateTime(2019, 01, 01, 17, 00, 00),
new DateTime(2019, 01, 01, 18, 45, 00),
new DateTime(2019, 01, 01, 19, 00, 00),
new DateTime(2019, 01, 01, 19, 30, 00),
new DateTime(2019, 01, 01, 22, 30, 00),
};
DateTime toFind = new DateTime(2019, 5, 8, 18, 20, 0);
var closestTime = times
.Aggregate((best, item) => Math.Abs((item.TimeOfDay - toFind.TimeOfDay).Ticks) <
Math.Abs((best.TimeOfDay - toFind.TimeOfDay).Ticks)
? item
: best);
Please, note, that if we should find the closest time, we have to get rid of date part - TimeOfDay. If date part should be count, just remove TimeOfDay -
var closestDateAndTime = times
.Aggregate((best, item) => Math.Abs((item - toFind).Ticks) <
Math.Abs((best - toFind).Ticks)
? item
: best);
One option is to use MoreLinq's MinBy:
var actualNow = DateTime.Now;
// set `current` up however you need it
var current = new DateTime(2019, 01, 01, actualNow.Hour, actualNow.Minute, actualNow.Minute, actualNow.Millisecond); // set this up however you need it
var min = times.MinBy(z => Math.Abs((current - z).Ticks)).First();
It avoids the memory pressure of the OrderBy based solutions (since it avoids allocating a list to store the entire set of times).
Note you may want to check whether times is empty before using this (or other) solutions. If you don't wish to do that, consider using:
var min = times.MinBy(z => Math.Abs((current - z).Ticks)).Cast<DateTime?>().FirstOrDefault();
which will return null (rather than default(DateTime), or throw an exception) if times is empty.

Round DateTime nearest nth minute

I'm trying to round a DateTime to the nearest 7 minute.
I've seen many rounding functions for c#, but for some reason, I'm getting different results to what I'm expecting.
Given the following time:
var d = new DateTime(2019, 04, 15, 9, 40, 1, 0);
If I want to round to the nearest 7th minute then I would expect the answer to be
2019-04-15 9:42:00 // 0, 7, 14, 21, 28, 35, 42 ?
Input / Expected result
new DateTime(2019, 04, 15, 9, 40, 0, 0); // 9:42
new DateTime(2019, 04, 15, 9, 03, 0, 0); // 9:07
new DateTime(2019, 04, 15, 9, 31, 0, 0); // 9:35
new DateTime(2019, 04, 15, 9, 21, 0, 0); // 9:21
new DateTime(2019, 04, 15, 9, 0, 0, 0); // 9:00
new DateTime(2019, 04, 15, 9, 58, 0, 0); // 10:00 (start again)
Various DateTime rounding functions that I've seen show the following answers, which I can't understand why unless I'm missing something
9:41 or
9:43
Example of rounding functions
public static DateTime RoundUp(this DateTime dt, TimeSpan d)
{
var modTicks = dt.Ticks % d.Ticks;
var delta = modTicks != 0 ? d.Ticks - modTicks : 0;
return new DateTime(dt.Ticks + delta, dt.Kind);
}
DateTime RoundUp(DateTime dt, TimeSpan d)
{
return new DateTime((dt.Ticks + d.Ticks - 1) / d.Ticks * d.Ticks, dt.Kind);
}
static DateTime RoundUpNMinute(DateTime dt, int n)
{
var minute = dt.Minute;
if (minute % n == 0)
return dt;
var minuteRoundUp = minute / n * n + n;
if(minuteRoundUp > 60)
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind).AddHours(1);
else
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, minuteRoundUp, 0, dt.Kind);
}
Got same results for all your examples.

Count number of including weeks between 2 dates

I need to get the number of weeks between 2 dates.
A week for me is monday to sunday.
So if the first date is on a saturday than this week should be included.
if the second date is on a monday than this week should be included.
What is the most efficient way to do this ?
example :
startdate enddate nbr of weeks
17/09/2016 26/09/2016 3 weeks
17/09/2016 25/09/2016 2 weeks
19/09/2016 26/09/2016 2 weeks
12/09/2016 25/09/2016 2 weeks
I found much answers for this, like this one for example how to calculate number of weeks given 2 dates? but they all end up with dividing the days with 7 and that does not gives the result I need.
The simplest way is probably to write a method to get the start of a week. Then you can subtract one date from another, divide the number of days by 7 and add 1 (to make it inclusive).
Personally I'd use Noda Time for all of this, but using DateTime:
// Always uses Monday-to-Sunday weeks
public static DateTime GetStartOfWeek(DateTime input)
{
// Using +6 here leaves Monday as 0, Tuesday as 1 etc.
int dayOfWeek = (((int) input.DayOfWeek) + 6) % 7;
return input.Date.AddDays(-dayOfWeek);
}
public static int GetWeeks(DateTime start, DateTime end)
{
start = GetStartOfWeek(start);
end = GetStartOfWeek(end);
int days = (int) (end - start).TotalDays;
return (days / 7) + 1; // Adding 1 to be inclusive
}
Complete example:
using System;
class Program
{
static void Main (string[] args)
{
ShowWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 26));
ShowWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 25));
ShowWeeks(new DateTime(2016, 9, 19), new DateTime(2016, 9, 26));
ShowWeeks(new DateTime(2016, 9, 12), new DateTime(2016, 9, 25));
}
static void ShowWeeks(DateTime start, DateTime end)
{
int weeks = GetWeeks(start, end);
Console.WriteLine($"{start:d} {end:d} {weeks}");
}
// Always uses Monday-to-Sunday weeks
public static DateTime GetStartOfWeek(DateTime input)
{
// Using +6 here leaves Monday as 0, Tuesday as 1 etc.
int dayOfWeek = (((int) input.DayOfWeek) + 6) % 7;
return input.Date.AddDays(-dayOfWeek);
}
public static int GetWeeks(DateTime start, DateTime end)
{
start = GetStartOfWeek(start);
end = GetStartOfWeek(end);
int days = (int) (end - start).TotalDays;
return (days / 7) + 1; // Adding 1 to be inclusive
}
}
Output (in my UK locale):
17/09/2016 26/09/2016 3
17/09/2016 25/09/2016 2
19/09/2016 26/09/2016 2
12/09/2016 25/09/2016 2
A bit "simplified", because the Monday is needed just for the start date:
static int weeks(DateTime d1, DateTime d2) {
var daysSinceMonday = ((int)d1.DayOfWeek + 6) % 7;
return ((d2 - d1).Days + daysSinceMonday) / 7 + 1;
}
See my method below, I "strechted" the weeks to monday untill sunday, and then calculated the total days / 7
public static void Main(string[] args)
{
Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 26)));
Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 17), new DateTime(2016, 9, 25)));
Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 19), new DateTime(2016, 9, 26)));
Console.WriteLine(CalculateWeeks(new DateTime(2016, 9, 12), new DateTime(2016, 9, 25)));
}
public static double CalculateWeeks(DateTime from, DateTime to)
{
if (to.DayOfWeek != DayOfWeek.Sunday)
to = to.Add(new TimeSpan(7- (int) to.DayOfWeek, 0, 0, 0)).Date;
return Math.Ceiling((to - from.Subtract(new TimeSpan((int)from.DayOfWeek - 1, 0, 0, 0)).Date).TotalDays / 7);
}

How to check whether C# DateTime is within a range

In C# I have from and to DateTime vales and want to check whether a value DateTime is within the range, how can I do this?
lowerBound = "01-Dec-2011 09:45:58"
upperBound = "01-Dec-2011 09:38:58"
value = "01-Dec-2011 09:49:58"
Just use the comparison operators as you would for numbers:
DateTime lowerBound = new DateTime(2011, 12, 1, 9, 38, 58);
DateTime upperBound = new DateTime(2011, 12, 1, 9, 49, 58);
DateTime value = new DateTime(2011, 12, 1, 9, 45, 58);
// This is an inclusive lower bound and an exclusive upper bound.
// Adjust to taste.
if (lowerBound <= value && value < upperBound)
You'll need to be careful that the values are all the same "kind" (UTC, local, unspecific). If you're trying to compare instants in time, (e.g. "did X happen before Y") it's probably best to use UTC.

Categories

Resources