Does DDay Ical library calculate recurring rules correctly? - c#

I've run some tests. Here is my code:
var systemTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var icalTimeZone = iCalTimeZone.FromSystemTimeZone(systemTimeZone);
var startTimeSearch = new DateTime(2015, 9, 8, 0, 0, 0, DateTimeKind.Utc);
var endTimeSearch = new DateTime(2015, 12, 1, 00, 0, 0, DateTimeKind.Utc);
var iCalendar = new iCalendar();
var pacificTimeZone = _iCalendar.AddTimeZone(icalTimeZone);
var event = new Event
{
Summary = "This is an event at 2015-09-08 10:30 PST (2015-09-08 17:30 UTC)",
DTStart = new iCalDateTime(2015, 9, 8, 10, 30, 0, pacificTimeZone.TZID, iCalendar),
Duration = new TimeSpan(0, 1, 0, 0)
};
var rp = new RecurrencePattern("FREQ=WEEKLY;UNTIL=20151112T080000Z;WKST=SU;BYDAY=TU");
event.RecurrenceRules.Add(rp);
iCalendar.Events.Add(_event);
var occurrences = iCalendar.GetOccurrences(startTimeSearch, endTimeSearch);
Here's the result:
10 occurences - here are the occurences' Period.StartTime.Value and UTC properties
Value - 9/8/2015 10:30:00 - UTC 9/8/2015 17:30:00
Value - 9/15/2015 10:30:00 - UTC 9/15/2015 17:30:00
Value - 9/22/2015 10:30:00 - UTC 9/22/2015 17:30:00
Value - 9/29/2015 10:30:00 - UTC 9/29/2015 17:30:00
Value - 10/6/2015 10:30:00 - UTC 10/6/2015 17:30:00
Value - 10/13/2015 10:30:00 - UTC 10/13/2015 17:30:00
Value - 10/20/2015 10:30:00 - UTC 10/20/2015 17:30:00
Value - 10/27/2015 10:30:00 - UTC 10/27/2015 17:30:00
Value -11/3/2015 10:30:00 - UTC 11/3/2015 17:30:00 (THIS SHOULD BE 18:30:00!)
Value - 11/10/2015 10:30:00 - UTC 11/10/2015 17:30:00 (THIS SHOULD BE 18:30:00!)
As you can see, the last two should have a UTC time of 18:30:00. So I'm wondering if this is just the way DDay Ical works and I shouldn't count on it getting the UTC datetime right, or if I'm doing something wrong.

You are getting this time difference due to daylight savings. One possible solution is not to get the time zone by name Pacific Standard Time
// First load a file containing time zone information for Pacific Standard Time
var systemTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
In place of this use system time zone like
var systemTimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault();
Or simply add the local timezone:
iCalendar iCalendar= new iCalendar();
iCalendar.AddLocalTimeZone();
To find all registered timezones, click here
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The local system has the following {0} time zones", zones.Count);
foreach (TimeZoneInfo zone in zones)
Console.WriteLine(zone.Id);
I simply use GetSystemTimeZones() in my code like
public static void Test1()
{
var systemTimeZone = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault();
//TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var icalTimeZone = iCalTimeZone.FromSystemTimeZone(systemTimeZone);
var startTimeSearch = new DateTime(2015, 9, 8, 0, 0, 0, DateTimeKind.Utc);
var endTimeSearch = new DateTime(2015, 12, 1, 00, 0, 0, DateTimeKind.Utc);
var iCalendar = new iCalendar();
var pacificTimeZone = iCalendar.AddTimeZone(icalTimeZone);
var _event =
new Event
{
Summary = "This is an event at 2015-09-08 10:30 PST (2015-09-08 17:30 UTC)",
DTStart = new iCalDateTime(2015, 9, 8, 10, 30, 0, pacificTimeZone.TZID, iCalendar),
Duration = new TimeSpan(0, 1, 0, 0)
};
var rp = new RecurrencePattern("FREQ=WEEKLY;UNTIL=20151112T080000Z;WKST=SU;BYDAY=TU");
_event.RecurrenceRules.Add(rp);
iCalendar.Events.Add(_event);
var occurrences = iCalendar.GetOccurrences(startTimeSearch, endTimeSearch);
}
when I debug it got got output like
you can change the time zone accordingly and get the result. Hope it helps you.

Don't use dday.ical; use ical.net.
This should get you started:
var iCalendar = new Calendar();
var start = new DateTime(2015, 9, 8, 10, 30, 0);
var e = new Event
{
Summary = "This is an event at 2015-09-08 10:30 PST (2015-09-08 17:30 UTC)",
DtStart = new CalDateTime(start, "Pacific Standard Time"),
Duration = TimeSpan.FromHours(1)
};
var rp = new RecurrencePattern("FREQ=WEEKLY;UNTIL=20151112T080000Z;WKST=SU;BYDAY=TU");
e.RecurrenceRules.Add(rp);
iCalendar.Events.Add(e);
var startTimeSearch = new DateTime(2015, 9, 8, 0, 0, 0, DateTimeKind.Utc);
var endTimeSearch = new DateTime(2015, 12, 1, 00, 0, 0, DateTimeKind.Utc);
var occurrences = iCalendar.GetOccurrences(startTimeSearch, endTimeSearch);
Results:

I could resolve this. It turns out that you shouldn't specify the time in the ExceptionDates, just add the date component only,
That seems to work, GetOccurances now properly recognizes the exception dates. Hopefully this will help someone going through the same pain.

Related

convert 1+year midnight time into unix milliseconds

i am trying to convert current year + 1 midnight date time to unix timestamp.
for that i have tried
DateTime currentTime = DateTime.Today;
DateTime yearEnd = new DateTime( currentTime.Year, 1,1,currentTime.Minute,currentTime.Hour,currentTime.Second,DateTimeKind.Local);
yearEnd = yearEnd.AddYears(1);
double t = (yearEnd.ToUniversalTime() - new DateTime(1970, 1, 1,0,0,0)).TotalMilliseconds;
and above code is returning 1514782800000 i.e. Mon Jan 01 2018 05:00:00 UTC in and Mon Jan 01 2018 10:30:00 Local (india)
what i am expecting is it converts time to Mon Jan 01 2018 00:00:00 local time
By default DateTime creates Unspecified DateTimeKind, so using UTC explicitly helps to avoid confusion. I've tried to rewrite in this way
DateTime currentTime = DateTime.UtcNow;
DateTime yearEnd = new DateTime( currentTime.Year, 1,1,0,0,0, DateTimeKind.Utc);
yearEnd = yearEnd.AddYears(1); // output DateTime has Utc Kind
var unixTimestamp = (yearEnd.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))).TotalMilliseconds;
Console.WriteLine(unixTimestamp);
The output is 1514764800000 which converts to GMT: Monday, 1 January 2018 00:00:00
Update:
In case you need to convert timestamp back to DateTime you may use the following:
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp);
return dtDateTime; // still Utc Kind
}
Usage example if you need to convert to other timezone:
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time (Mexico)"); // specify your desired timezone here
Console.WriteLine(TimeZoneInfo.ConvertTimeFromUtc(UnixTimeStampToDateTime(unixTimestamp), infotime));
if i understand you right you want the following:
double result = new DateTime(currentTime.Year + 1, 1, 1, 0, 0, 0, DateTimeKind.Local).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
Or improved for readablity
DateTime newYear = new DateTime(currentTime.Year + 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime uTSBegin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
double result2 = newYear.Subtract(uTSBegin).TotalSeconds;

C# - Searching Time between 2 DateTime Objects of different days

i have a list of "event" objects.
In every event i have "EventStartTime" and "EventEndTime" declared as DateTime objects.
I want to be able to search "events" by time , for example 10:00,
the "event" you see below shows that the festival starts at 22:00 on Feb 17th,
and ends at 15:00 the following day. i have a couple more like these.
new EventsManager.Event() //3
{
EventType = EventsManager.EventType.Festival,
EventName = "Twistival",
EventPlace = placeList[4],
EventStartTime =new DateTime(2017,02,17,22,0,0),
EventEndTime = new DateTime(2017,02,18,15,0,0),
EventNumberOfParticipants = 8000
},
So when i search for event that occur, or still occurring at at 10:00
i should get this event.
any suggestions?
Assuming that you have a specific time of day that you want to determine if the event covers regardless of the date it covers it on then there are 4 cases you need to consider. First if the dates are more than 1 day apart they cover all times of day. If the start is before the time of day and the end is after the time of day it will cover the time. The last two cases require that the end date be on the next day from the start date, then either the start date is before the time of day, or the end date is after the time of day. Note that this also assumes that the start date is before the end date.
var events = new List<Tuple<DateTime, DateTime>>
{
// start and end after time of day but on different days
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 15, 0, 0)),
// start and end before time of day but on different days
Tuple.Create(
new DateTime(2017, 02, 17, 9, 0, 0),
new DateTime(2017, 02, 18, 7, 0, 0)),
// start before and end after same day
Tuple.Create(
new DateTime(2017, 02, 17, 9, 0, 0),
new DateTime(2017, 02, 17, 11, 0, 0)),
// covers more than 1 day
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 22, 0, 1)),
// start after and end before on different days
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 10, 0, 0)),
// start and end before on same day
Tuple.Create(
new DateTime(2017, 02, 17, 7, 0, 0),
new DateTime(2017, 02, 17, 8, 0, 0)),
// start and end after on same day
Tuple.Create(
new DateTime(2017, 02, 17, 11, 0, 0),
new DateTime(2017, 02, 17, 12, 0, 0)),
};
var timeOfDay = new TimeSpan(0, 10, 0 ,0);
foreach (var x in events)
{
if (x.Item2 - x.Item1 > TimeSpan.FromDays(1)
|| (x.Item1.TimeOfDay < timeOfDay && x.Item2.TimeOfDay > timeOfDay)
|| (x.Item1.Date < x.Item2.Date
&& (x.Item1.TimeOfDay < timeOfDay || x.Item2.TimeOfDay > timeOfDay)))
{
Console.WriteLine(x);
}
}
Will output
(2/17/2017 10:00:00 PM, 2/18/2017 3:00:00 PM)
(2/17/2017 9:00:00 AM, 2/18/2017 7:00:00 AM)
(2/17/2017 9:00:00 AM, 2/17/2017 11:00:00 AM)
(2/17/2017 10:00:00 PM, 2/18/2017 10:00:01 PM)
Let's say you have a
List<Event> Events;
of your Events. You can create a simple LINQ query to get all events running at a special time with a simple method like
private IEnumerable<Event> GetRunningEvents(DateTime time)
{
return Events.Where(E => E.EventStartTime <= time && E.EventEndTime >= time);
}
Dont forget to add
using System.Linq;
to your file.
EDIT: Without LINQ a possible approach is
private List<Event> GetRunningEvents(DateTime time)
{
List<Event> RunningEvents = new List<Event>();
foreach(Event E in Events)
{
if (E.EventStartTime <= time && E.EventEndTime >= time)
{
RunningEvents.Add(E);
}
}
return RunningEvents;
}
Try Linq Where:
var list = new List<Event>();
var searchTime = DateTime.Now;
var result = list.Where(e => e.EventStartTime <= searchTime && searchTime <= e.EventEndTime).ToList();

how i set lookupedit display format

Tables dt like this:
index StartDate EndDate
1 2015/03/23 22:00 2015/03/23 23:00
2 2015/03/23 22:00 2015/03/23 22:00
3 2015/03/23 22:00 2015/03/23 22:00
I have set:
lookupedit1.Properties.ValueMember = "StartDate";
lookupedit1.Properties.DisplayMember = "StartDate";
So, the value has show OK, long date Type, but i want the DisplayMemeber like short date type.
I have try any of below, but it's havn't work expectly.
lookupedit1.Properties.Mask.EditMask = "yyyy-MM-dd";
lookupedit1.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";
lookupedit1.Properties.EditFormat.FormatString = "yyyy-MM-dd";
How can i resolve my questions?
i want the DisplayMemeber like short date type
You can use the standard d display-format string for short date (culture specific, described in the Standard Date and Time Format Strings document in MSDN.). To specify formatting-behavior you should add the specific column into the LookUp edit:
lookUpEdit1.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo()
{
FieldName = "StartDate",
FormatType = DevExpress.Utils.FormatType.DateTime,
FormatString = "d" // short date
});
lookUpEdit1.Properties.DataSource = new List<Order> {
new Order(){ StartDate = new DateTime(2015, 03, 23, 23, 0, 0) },
new Order(){ StartDate = new DateTime(2015, 03, 24, 23, 0, 0) },
new Order(){ StartDate = new DateTime(2015, 03, 25, 23, 0, 0) },
};
To setup display-behavior while editing you can use the editor's Mask :
lookUpEdit1.Properties.Mask.EditMask = "d"; // short date
lookUpEdit1.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime;
lookUpEdit1.Properties.Mask.UseMaskAsDisplayFormat = true;

Convert UTC DateTime to DateTimeOffset

I need to convert UTC date strings to DateTimeOffsets.
This must work with a timezone which differs from the computers timezone.
E.g. current computer timezone is +02:00, but I want to create a DateTimeOffset with offset -4:00.
I already read lot of questions here on stackoverflow, but none of them solved my problem.
That is what I need to do:
Input: "2012-11-20T00:00:00Z"
Output: DateTimeOffset with:
UtcDateTime of 2012-11-20 00:00
the correct Utc offset for the defined timezone (01:00 in this example)
LocalDateTime: 2012-11-20 01:00 (= UtcDateTime + Offset)
Of course daylight saving must be taken into account.
edit:
To make things even clearer, please try to complete the following code snippet:
DateTimeOffset result;
const string dateString = "2012-11-20T00:00:00Z";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date
//do conversion here
Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0)); //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.LocalDateTime, new DateTime(2012, 11, 20, 1, 0, 0));
Here is the solution you are looking for:
const string dateString = "2012-11-20T00:00:00Z";
TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date
DateTimeOffset utc = DateTimeOffset.Parse(dateString);
DateTimeOffset result = TimeZoneInfo.ConvertTime(utc, timezone);
Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0)); //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.DateTime, new DateTime(2012, 11, 20, 1, 0, 0));
Note that you were incorrectly testing the .LocalDateTime property - which is always going to convert the result to the local time zone of the computer. You simply need the .DateTime property instead.
Is this what you want:
[Test]
public void ParseUtcDateTimeTest()
{
DateTime dateTime = DateTime.Parse("2012-11-20T00:00:00Z");
Assert.AreEqual(new DateTime(2012, 11, 20, 01, 00, 00), dateTime);
DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime);
Assert.AreEqual(new TimeSpan(0, 1, 0, 0), dateTimeOffset.Offset);
}
Note that my asserts are valid in Sweden (CET)
There are a couple of overloads on DateTime.Parse()
Is this useful for your conversion:
[Test]
public void ConvertTimeTest()
{
    DateTime dateTime = DateTime.Parse("2012-11-20T00:00:00Z");
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTime convertedTime = TimeZoneInfo.ConvertTime(dateTime, cstZone);
    Assert.AreEqual(new DateTime(2012, 11, 19, 18, 00, 00), convertedTime);
    TimeSpan baseUtcOffset = cstZone.BaseUtcOffset;
    Assert.AreEqual(new TimeSpan(0, -6, 0, 0), baseUtcOffset);
}
const String dateString = "2012-11-20T00:00:00Z";
var offsetDate = DateTimeOffset.Parse(dateString);
var offsetDate2 = DateTime.Parse(dateString);
Output is
offsetDate {20-11-2012 0:00:00 +00:00} System.DateTimeOffset
offsetDate2 {20-11-2012 1:00:00} System.DateTime

C# seconds since specific date

In C# 3.0, how do I get the seconds since 1/1/2010?
Goes like this:
TimeSpan test = DateTime.Now - new DateTime(2010, 01, 01);
MessageBox.Show(test.TotalSeconds.ToString());
For one liner fun:
MessageBox.Show((DateTime.Now - new DateTime(2010, 01, 01))
.TotalSeconds.ToString());
You can substract 2 DateTime instances and get a TimeSpan:
DateTime date = new DateTime(2010,1,1);
TimeSpan diff = DateTime.Now - date;
double seconds = diff.TotalSeconds;
Just to avoid timezone issues
TimeSpan t = (DateTime.UtcNow - new DateTime(2010, 1, 1));
int timestamp = (int) t.TotalSeconds;
Console.WriteLine (timestamp);
It's really a matter of whose 2010-Jan-01 you're using and whether or not you wish to account for daylight savings.
//I'm currently in Central Daylight Time (Houston, Texas)
DateTime jan1 = new DateTime(2010, 1, 1);
//days since Jan1 + time since midnight
TimeSpan differenceWithDaylightSavings = DateTime.Now - jan1;
//one hour less than above (we "skipped" those 60 minutes about a month ago)
TimeSpan differenceWithoutDaylightSavings = (DateTime.UtcNow - jan1.ToUniversalTime());
//difference for those using UTC and 2010-Jan-01 12:00:00 AM UTC as their starting point
// (today it's 5 hours longer than differenceWithDaylightSavings)
TimeSpan utcDifference = (DateTime.UtcNow - new DateTime(2010, 1, 1));
Difference with Daylight Savings: 105.15:44:09.7003571
Difference without Daylight Savings: 105.14:44:09.7003571
UTC Difference: 105.20:44:09.7003571
To get the seconds, use the TotalSeconds property off the TimeSpan object.
protected void Page_Load(object sender, EventArgs e)
{
SecondsSinceNow(new DateTime(2010, 1, 1, 0, 0, 0));
}
private double SecondsSinceNow(DateTime compareDate)
{
System.TimeSpan timeDifference = DateTime.Now.Subtract(compareDate);
return timeDifference.TotalSeconds;
}
DateTime t1 = DateTime.Now;
DateTime p = new DateTime(2010, 1, 1);
TimeSpan d = t1 - p;
long s = (long)d.TotalSeconds;
MessageBox.Show(s.ToString());

Categories