iCal Creating Time Offset - c#

When trying to create an iCal file I get a very strange issue that I cannot trace. The following is the code used and an example of an event that is set to start at 08:00 and end at 11:00. The file creates with relevant information, but when tryting to add it to Outlook an hour has been added to the end time.
DateTime eventDate = DateTime.Parse("19/06/2014");
DateTime startTime = DateTime.Parse("09:00:00");
DateTime endTime = DateTime.Parse("11:00:00");
string location = "Test Location";
string title = "Test Title";
context.Response.ContentType = "text/x-vcalendar";
string filename = String.Format("attachment; filename={0}.ics", eventname.Replace(" ", "-"));
context.Response.AddHeader("Content-disposition", filename);
context.Response.Write("BEGIN:VCALENDAR" + Environment.NewLine);
context.Response.Write("VERSION:2.0" + Environment.NewLine);
context.Response.Write("METHOD:PUBLISH" + Environment.NewLine);
context.Response.Write("BEGIN:VTIMEZONE" + Environment.NewLine);
context.Response.Write("TZID:Europe/London" + Environment.NewLine);
context.Response.Write("X-LIC-LOCATION:Europe/London" + Environment.NewLine);
context.Response.Write("BEGIN:DAYLIGHT" + Environment.NewLine);
context.Response.Write("TZOFFSETFROM:+0000" + Environment.NewLine);
context.Response.Write("TZOFFSETTO:+0100" + Environment.NewLine);
context.Response.Write("TZNAME:BST" + Environment.NewLine);
context.Response.Write("DTSTART:19700329T010000" + Environment.NewLine);
context.Response.Write("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" + Environment.NewLine);
context.Response.Write("END:DAYLIGHT" + Environment.NewLine);
context.Response.Write("BEGIN:STANDARD" + Environment.NewLine);
context.Response.Write("TZOFFSETFROM:+0100" + Environment.NewLine);
context.Response.Write("TZOFFSETTO:+0000" + Environment.NewLine);
context.Response.Write("TZNAME:GMT" + Environment.NewLine);
context.Response.Write("DTSTART:19701025T020000" + Environment.NewLine);
context.Response.Write("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU" + Environment.NewLine);
context.Response.Write("END:STANDARD" + Environment.NewLine);
context.Response.Write("END:VTIMEZONE" + Environment.NewLine);
context.Response.Write("BEGIN:VEVENT" + Environment.NewLine);
context.Response.Write("ORGANIZER:MAILTO: test#domain.com" + Environment.NewLine);
context.Response.Write("UID: test2#domain.com" + Environment.NewLine);
context.Response.Write("DTSTART:" + startDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
context.Response.Write("DTEND:" + GetMeetingEndDate(startDate, endDate).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
context.Response.Write("DTSTAMP:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
context.Response.Write("SUMMARY:" + subject + Environment.NewLine);
context.Response.Write("DESCRIPTION:" + description + Environment.NewLine);
context.Response.Write("LAST-MODIFIED:" + DateTime.Now.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
context.Response.Write("PRIORITY:5" + Environment.NewLine);
context.Response.Write("LOCATION;ENCODING=QUOTED-PRINTABLE:" + location + Environment.NewLine);
context.Response.Write("CLASS:PUBLIC" + Environment.NewLine);
context.Response.Write("END:VEVENT" + Environment.NewLine);
context.Response.Write("END:VCALENDAR");
The outcome of this is for the appointment to be added to Outlook with a start time of 09:00 and an end time of 12:00. The end time has increased by one hour.
Please note that the code about is intended for British/GMT use.
I have debugged this procedure and checked all the dates as they are being set and everything is correct. Is there anything that I am missing with this? I really don't want to have to force a reduction in the end hour just so it adds properly to Outlook.
Edit:
The following is the GetMeetingEndDate function.
DateTime GetMeetingEndDate(DateTime startDate, DateTime endDate)
{
DateTime newDate = new DateTime();
if (endDate < startDate)
{
newDate = endDate.AddHours(12);
}
else if (endDate == startDate)
{
newDate = startDate.AddDays(1).AddHours(-1);
}
else
{
newDate = endDate;
}
return newDate;
}
Thank you.

In the below code:-
context.Response.Write("DTSTART:" + startDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
context.Response.Write("DTEND:" + GetMeetingEndDate(startDate, endDate).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
the DTSTART is set to value startDate.ToUniversalTime() whereas in function GetMeetingEndDate the dates are passed without converting into UTC. Because of this startdate is always correct but iCal is treating your local enddate as UTC date. That might be causing the problem of adding extra hour. The solution is to change the below code block as
context.Response.Write("DTEND:" + GetMeetingEndDate(startDate.ToUniversalTime(), endDate.ToUniversalTime()).ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);

Related

Results in descending order

I've got a block of code which sums up time togged for various tasks in a project and returns the total hours logged per project (intMinutesLogged). How do I get my results n descending order?
static async void NotifyEntriesByWorkSpace(Dictionary<string, List<TimeEntry>> dicEntriesByWorkspace, string strChatURL)
{
string strMessage = "";
foreach (var kvpEntry in dicEntriesByWorkspace)
{
var lstTimeEntries = kvpEntry.Value;
string strTitle = "";
var intMinutesLogged = 0;
var intMinutesBillable = 0;
var intMinutesNonBillable = 0;
foreach (var objTimeEntry in lstTimeEntries)
{
if (objTimeEntry.Billable)
{
intMinutesBillable += objTimeEntry.TimeInMinutes;
}
else
{
intMinutesNonBillable += objTimeEntry.TimeInMinutes;
}
}
strTitle = Workspaces.getWorkspaceFromCache(kvpEntry.Key).Title;
//Console.WriteLine(intMinutesLogged + ": " + strTitle + "m");
intMinutesLogged = intMinutesBillable + intMinutesNonBillable;
Console.WriteLine(TimeLoggedMessage(intMinutesLogged) + ": " + strTitle + " " + "(Billable: " + TimeLoggedMessage(intMinutesBillable) + ";" + " " + "Non-Billable: " + TimeLoggedMessage(intMinutesNonBillable) + ")");
strMessage += TimeLoggedMessage(intMinutesLogged) + ": " + strTitle + " " + "(Billable: " + TimeLoggedMessage(intMinutesBillable) + ";" + " " + "Non-Billable: " + TimeLoggedMessage(intMinutesNonBillable) + ")" + "\n";
}
await SendMessage(strChatURL, strMessage);
}
static string TimeLoggedMessage(int intMinutesLogged)
{
return intMinutesLogged / 60 + "h" + " " + intMinutesLogged % 60 + "m";
}
You could use LINQ for this: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderbydescending?view=net-6.0
You could create a simple class or anonymous type to hold the integer values you're summing up (total minutes, billable minutes, non-billable minutes). Then you could populate a collection of this type within the code you shared and afterwards call OrderByDescending on it. You could order based on any of the three integer values.

How to convert time only using DateTime.Parse?

I have a string variable with it's value formatted to look like "1:23:45 AM"
I need to convert that into a DateTime variable for time without a date component.
I am receiving an "Unhandled exception" error that is being caused by the formatting. If I remove the "AM", it works fine but the "AM/PM" component is needed for what I am trying to do.
I am currently using the following code to attempt this...
"temp" is the name of the variable I am using until I come up with a more meaningful variable name...
public string PlusMinus12(string hour, string minute, string second, string ampm)
{
string PlusMinus;
int rndmTmp1 = Random1();
int rndmTmp2 = Random2();
if (rndmTmp1 == 0)
{
PlusMinus = hour + ":" + minute + ":" + second + ": " + ampm;
return PlusMinus;
}
else if (rndmTmp1 == 1)
{
string temp = hour + ":" + minute + ":" + second +": " + ampm;
**DateTime subtract = DateTime.Parse(temp);**
subtract.AddSeconds(-rndmTmp2);
PlusMinus = subtract.ToString("h:mm:ss tt");
return PlusMinus;
}
else
{
DateTime subtract = DateTime.Parse(temp); is the line causing the error
The error is:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: String was not recognized as a valid DateTime.
Most of the information I have found so far on this topic include solutions that depend on the existence of the Date component, which I am not using.
Any ideas?
Thank you
You should try parsing it using DateTime.ParseExact using a custom format from here: Custom Date and Time Format Strings
Your problem is this
PlusMinus = hour + ":" + minute + ":" + second + ": " + ampm;
Change it to
PlusMinus = Int32.Parse(hour).ToString("00") + ":" + Int32.Parse(minute).ToString("00") + ":" + second.ToString("00") + " " + ampm;
A little convoluted since strings are passed in as input. No colon after the seconds and zero pad the numbers. Also, since they are input variables, at some point you should verify that they are between 0-59.
If you're only interested in returning the time part, add a extra variable that holds the date part and append it to the front of temp when you parse it?
like:
string tempDate = "2008-05-01 "
This works for me:
string timeString = "1:23:45 AM";
string format = "h:mm:ss tt";
CultureInfo provider = CultureInfo.InvariantCulture;
try
{
DateTime result = DateTime.ParseExact(timeString, format, provider);
Console.WriteLine("{0} converts to {1}.", timeString, result.ToString());
}
catch (FormatException)
{
Console.WriteLine("{0} is not in the correct format.", timeString);
}
Probably your issue is with a particular time.
UPDATE: You need to check you are passing minutes and seconds to this function with a 0 for values like 1-2..9. Because standard provider will not accept a string like '1:1:2 AM'.
public string PlusMinus12(string hour, string minute, string second, string ampm)
{
string PlusMinus;
int rndmTmp1 = Random1();
int rndmTmp2 = Random2();
if (rndmTmp1 == 0)
{
PlusMinus = hour + ":" + minute + ":" + second + ": " + ampm;
return PlusMinus;
}
else if (rndmTmp1 == 1)
{
string temp = hour + ":" + minute + ":" + second +": " + ampm;
DateTime subtract = DateTime.Parse("2000-01-01 " + temp);
subtract.AddSeconds(-rndmTmp2);
PlusMinus = subtract.ToString("h:mm:ss tt");
return PlusMinus;
}
else
{

Properly formatted text output to .txt file

I'am exporting some data to a .txt file as follows:
String content;
String path=#"e:\coding\";
String name="test.txt";
path+=name;
System.IO.File.Delete(path);
for (i=0;i<row-1;i++)
{
try
{
if (r[i].points.Count() > 2)
{
content = "Route " + (i + 1).ToString() +" Truck_id:"+trk[i].truck_name.ToString()+ " Max_load="+trk[i].capacity.ToString()+ "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
System.IO.File.AppendAllText(path, "Points Load Reached_AT Max_load" + Environment.NewLine);
System.IO.File.AppendAllText(path, "========================================" + Environment.NewLine);
for (int j = 0; j < (r[i].points.Count()); j++)
{
content = r[i].points[j].ToString() + " " + c[r[i].points[j]].load.ToString() +" "+ r[i].time_list[j].ToString()+" "+c[r[i].points[j]].max_load.ToString()+"\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
}
content = "Total " + r[i].ld.ToString() + "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine );
content = "Route Complete: " + r[i].reach_at.ToString();
System.IO.File.AppendAllText(path, content + Environment.NewLine+Environment.NewLine);
}
}
catch (IndexOutOfRangeException e)
{
break;
}
}
As expected the output I get is not properly formatted:
The spaces are causing the text to be jumbled and not arranged. My reputation does'nt allow me to post a screenshot but I guess It can be understood what is happening.
Is there way so that the text is properly formatted neatly column wise without looking jumbled.
If you need a text, you can use tabs:
System.IO.File.AppendAllText(path, "Points\t\tLoad\t\tReached_AT\t\tMax_load" + Environment.NewLine);
// ...
content = r[i].points[j].ToString() + "\t\t " + c[r[i].points[j]].load.ToString() +"\t\t"+ r[i].time_list[j].ToString()+"\t\t"+c[r[i].points[j]].max_load.ToString()+"\n";
Just play with amount of tabs (\t for one, \t\t for two, etc...). Hope it can help.
Another solution would be to use commas:
System.IO.File.AppendAllText(path, "Points,Load,Reached_AT,Max_load" + Environment.NewLine);
and save to CSV-file (comma-separated values). Then you can import the data to Microsoft Excel or to other software.
You can find bunch full of good information on how to format the string contents in the The format item MSDN but for quick answer, an example for your string
content = "Route " + (i + 1).ToString() + " Truck_id:" + trk[i].truck_name.ToString() + " Max_load=" + trk[i].capacity.ToString() + "\n";
If we assume,
i maximum 10 digits,
Truck_name max 45 characters
capacity max 10 digits
content = String.Format("{0,-20}{1,55}{2,20} " + Environment.NewLine, "Route " + (i + 1).ToString(), " Truck_id:" + trk[i].truck_name.ToString(), " Max_load=" + trk[i].capacity.ToString());

ASP.NET C# Form Submission Problems

I have built a form with jqueryui-date picker - basically if the end date is less than or equal to start time it needs to display a message saying it must be greater than the start time before allowing the user to submit the form. Cannot see where i am going wrong.
Code Below on Submit
protected void btnSubmit_Click(object sender, EventArgs e)
{
DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);
if (startDate >= DateTime.Now)
{
if (endDate <= startDate)
{
usrComment.Visible = true;
//usrComment.Text = "Return time needs to be greater than pickup time IF same day";
usrComment.Text = "Date =" + startDate + "Date 2 =" + endDate;
}
else
{
if (Page.IsValid)
{
string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);
string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
string EmailToUser = txtEmail.Text;
string EmailSubject = "Quote Form submitted";
****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);
StringBuilder html = new StringBuilder();
html.AppendLine("<ul>");
html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
html.AppendLine("</ul>");
m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());
//Response.Redirect("/contact-us/quote-form-submitted.aspx");
}
usrComment.Text = "SUBMIT IT NOW!!!!!";
}
}
}
jQuery for the date picker
$(function () {
function getDiff() {
var from = $(".start").val();
var till = $(".fin").val();
var c = from.split("/");
beg = new Date(c[2], c[1] - 1, c[0]);
var d = till.split("/");
en = new Date(d[2], d[1] - 1, d[0]);
var rest = (en - beg) / 86400000;
var txt = rest == 0 ? "" : rest + " days"
$("#res").text(txt);
}
$(".start").datepicker({
changeMonth: false,
changeYear: false,
showAnim: "fadeIn",
gotoCurrent: true,
minDate: 0, //change this to +3 to start 3 days from now
dateFormat: "dd/mm/yy",
onSelect: function (dateText, inst) {
$(".fin").val(dateText);
$(".fin").datepicker("option", "minDate", dateText);
getDiff();
}
});
$(".fin").datepicker({
dateFormat: "dd/mm/yy",
changeMonth: true,
changeYear: true,
showAnim: "fadeIn",
onSelect: getDiff
});
//Disabling Copy, Paste, Cut
$('.start').bind('paste', function (e) {
e.preventDefault();
//alert("You cannot paste text into this textbox!");
window.alert = function () { };
});
$('.fin').bind('paste', function (e) {
e.preventDefault();
//alert("You cannot paste text into this textbox!");
window.alert = function () { };
});
});
So if you have a pickup date of 17/09/2013 and pickup time as 08:00 and the same for return date and time it should alert you with the message and if the return date is greater than or equal to start time the return pickup time needs to be greater than 08:00 if that makes sense?
It would be nice to have a useful, constructive comment. It doesn't matter if this is the "right way" to do it. I believe this is what you're trying to do. I've just added an else to the initial if statement to inform the user to choose a Start date later than now and altered the text of the other else statement to inform the user to pick a return date later than the start date.
protected void btnSubmit_Click(object sender, EventArgs e)
{
DateTime startDate = Convert.ToDateTime(txtStartDate.Text + " " + ddlTime.SelectedValue);
DateTime endDate = Convert.ToDateTime(txtEndDate.Text + " " + ddlTime2.SelectedValue);
if (startDate >= DateTime.Now)
{
if (endDate <= startDate)
{
usrComment.Visible = true;
usrComment.Text = "Please enter a Return date later than " + startDate;
}
else
{
if (Page.IsValid)
{
string EmailServer = WebConfigurationManager.AppSettings["Email.Server"];
int ServerPort = Int32.Parse(WebConfigurationManager.AppSettings["Email.ServerPort"]);
string EmailServerUser = (WebConfigurationManager.AppSettings["Email.UserName"]);
string EmailServerPass = (WebConfigurationManager.AppSettings["Email.Password"]);
string EmailFrom = (WebConfigurationManager.AppSettings["Email.From"]);
string EmailTo = (WebConfigurationManager.AppSettings["Email.To"]);
string EmailToUser = txtEmail.Text;
string EmailSubject = "Quote Form submitted";
****.****.*****.Email m = new ****.****.Helpers.Email(EmailServer, ServerPort, EmailServerUser, EmailServerPass);
StringBuilder html = new StringBuilder();
html.AppendLine("<ul>");
html.AppendLine("<li>" + lblName.Text + ": " + txtName.Text + "</li>");
html.AppendLine("<li>" + lblEmail.Text + ": " + txtEmail.Text + "</li>");
html.AppendLine("<li>" + lblPhone.Text + ": " + txtPhone.Text + "</li>");
html.AppendLine("<li>" + lblVehicleType.Text + ": " + ddlVehicleType.SelectedValue + "</li>");
html.AppendLine("<li>" + lblPickupDate.Text + ": " + txtStartDate.Text + "</li>");
html.AppendLine("<li>" + ddlTime.SelectedValue + "</li>");
html.AppendLine("<li>" + lblReturnDate.Text + ": " + txtEndDate.Text + "</li>");
html.AppendLine("<li>" + ddlTime2.SelectedValue + "</li>");
html.AppendLine("</ul>");
m.SendHTMLEmail(EmailFrom, EmailTo, EmailSubject, html.ToString());
//Response.Redirect("/contact-us/quote-form-submitted.aspx");
}
usrComment.Text = "SUBMIT IT NOW!!!!!";
}
}
else
{
usrComment.Visible = true;
usrComment.Text = "Please enter a Start date later than " + DateTime.Now;
}
}
I suggest you not to use any kind of custom function.
JquerUI-date picker have inbuilt functionality for comparing end date with start date.
Please try this
http://jqueryui.com/datepicker/#date-range

New line in textbox

I have a textbox1 that lets the user input a text, a button that adds the text to textbox2.
this is my code but it doesnt create a new line when I add another text.
string date = DateTime.Now.ToString();
txt_details.Text = date + " " + txt_summary.Text.ToString() + Environment.NewLine + Environment.NewLine ;
Notice the += operator.
txt_details.Text += "\n" + date + " " + txt_summary.Text.ToString();
Looks like you should be appending (use +=); instead you are overwriting.
string date = DateTime.Now.ToString();
txt_details.Text += date + " " + txt_summary.Text.ToString() + Environment.NewLine + Environment.NewLine
Make sure Multiline is enabled.
Ensure that TextBox.Multiline property is set to true
txt_details.Multiline = true;

Categories