I'm setting up a calendar invite email using ical.net. Sending out a non-recurring event seems to work perfectly: I set the start and end date like this
iCalEvent.DtStart = new CalDateTime(DateTime.SpecifyKind(model.EventTime.Value, DateTimeKind.Utc));
iCalEvent.DtEnd = new CalDateTime(DateTime.SpecifyKind(model.EventTime.Value.AddMinutes(model.DurationMins.Value), DateTimeKind.Utc));
when the email arrives, the time zone has been converted to the recipients timezone (The timezone is -7, the eventTime is 4pm and the duration is 3 hours)
However, when I take this same exact code and add this line to it
IRecurrencePattern recurrence = new RecurrencePattern(FrequencyType.Daily, 1)
{
Until = DateTime.SpecifyKind(model.endDate.Value.AddDays(1), DateTimeKind.Utc)
};
iCalEvent.RecurrenceRules = new List<IRecurrencePattern> { recurrence };
Suddenly my timezone is no longer converted when the email is received (The timezone is -7, the eventTime is 4pm and the duration is 3 hours. The endDate is on the 28th)
I need the DateTime to be displayed to the user in their own timezone and I need to display recurring events from the eventTime to the endDate
It may also be useful to note that I do not have a timezone property specified on Calendar as it was causing the sent email to show a "not supported calendar message" in outlook. Before I removed it, it looked like this
iCal.AddTimeZone(new VTimeZone("UTC"));
when I opened ical files that had this time zone specified, they seemed to work correctly for multi day events, but as I need them to appear in outlook with the accept/decline/tentative buttons, it is out of the question to add it back
i've also tried specifying datetimes like this
iCalEvent.DtStart = new CalDateTime(leEvent.EventTime.Value, "UTC");
but nothing changed
EDIT: I now understand that the issue is due to a recurring event needing a timezone as specified here, but I'm not quite sure where the timezone needs to be specified. I went back to adding the vTimeZone back in and validating it through this site, and it appears that the iCal file is missing the standard/daylight section inside of the timezone block
I have also tried specifying the timezone as GMT and specifying the timezone as "\"America/Phoenix\"" so that the tzid came out as TZID:"America/Phoenix" (with quotes in the ical file.
This is my code at the moment that causes the issue.
iCalEvent.DtStart = new CalDateTime(DateTime.SpecifyKind(model.EventTime.Value, DateTimeKind.Utc));
iCalEvent.DtEnd = new CalDateTime(iCalEvent.DtStart.Value.AddMinutes(model.DurationMins.Value));
if (model.EndDate.HasValue)
{
IRecurrencePattern recurrence = new RecurrencePattern(FrequencyType.Daily, 1)
{
Until = DateTime.SpecifyKind(model.MaxDate.Value, DateTimeKind.Utc).ToLocalTime()
};
iCalEvent.RecurrenceRules = new List<IRecurrencePattern> { recurrence };
iCalEvent.DtStart = new CalDateTime(iCalEvent.DtStart.Value.ToLocalTime(), "America/Phoenix");
iCalEvent.DtEnd = new CalDateTime(iCalEvent.DtEnd.Value.ToLocalTime(), "America/Phoenix");
iCal.AddTimeZone(new VTimeZone("America/Phoenix"));
}
I'm not quite sure what needs to happen to correct the standard/daylight ical error from this point.
FINAL EDIT:
after reading through this post, I found that this issue has already been solved as of last november. I checked the version we had in our project and it turned out some genius just copied the dll straight in without setting it up through nuget (and a version from several years ago no less). I grabbed the latest version and this time specifying the timezone caused no issues in outlook. I'm still experimenting with addTimeZone and addLocalTimeZone but I'm definitely on the right track. Thank you to rianjs for this extremely useful library. I don't know how I would possible work with this crazy calendar standard without it.
A recurring event is always relative to the sender's timezone (or rather to the event location), and not to the recipient's timezone, because of daylight saving changes which may happen at different time between the organiser and the various recipients.
So in most cases you want to use a meaningful timezone in the event (i.e. not UTC).
Then Outlook is simply showing that the event is indeed happening according to the given timezone. This is an indication that the event may not always be at the same time of day for the recipient.
Since this took me so long to figure out, I might as well share my solution in case anyone else runs into this problem and finds this.
I defined the DtStart/DtEnd with two datetimes that had kind utc
calendarEvent.DtStart = new CalDateTime(model.EventTime.Value);
calendarEvent.DtEnd = new CalDateTime(model.EventTime.Value.AddMinutes(model.DurationMins.Value));
which worked great for single day events, but with multidays I ended up doing this
if (model.EndDate.HasValue)
{
RecurrencePattern recurrence = new RecurrencePattern(FrequencyType.Daily, 1)
{
Until = model.EndDate.Value //has kind: Utc
};
var timezoneOffsetString = "Etc/GMT";
if (timezoneOffset > 0) //client time zone offset, calculated through js
{
timezoneOffsetString += "-" + timezoneOffset;
}
else if (timezoneOffset < 0)
{
timezoneOffsetString += "+" + (-1 * timezoneOffset);
}
calendar.AddTimeZone(timezoneOffsetString);
calendarEvent.DtStart = calendarEvent.DtStart.ToTimeZone(timezoneOffsetString);
calendarEvent.DtEnd = calendarEvent.DtEnd.ToTimeZone(timezoneOffsetString);
calendarEvent.RecurrenceRules = new List<RecurrencePattern> { recurrence };
}
It's not fullproof as some places may have weird dst problems, but nodatime was treating the "America/Phoenix" tzid as an LMT time, which was giving me a time that was like 28 minutes off... Besides, treating all dates as GMT is closer to the implementation we have in the rest of our app so it worked for my situation
This solution gave me the idea to piece together the Etc/GMT stuff.
Related
I'm creating a C# MVC project that uses Oracle Database. While trying to connect to the DB, I get following error:
ORA-00604: error occurred at recursive SQL level 1
ORA-01882: timezone region not found.
When I try to connect via SQL developer it works fine. Is there any way that I can solve this problem ?
NOTE: I'm using IIS express for testing
I was facing the same issue using ODP.NET for .NET Core on Pivotal Cloud Foundry. The core issue is missing timezone information.
Added a timezone by setting "TZ" environment variable for my instance. That fixed the issue for me.
Actually, this error because of some issue with application Oracle driver which you use and Oracle version of the DB.
NOTE: Please check Both of them and use Updated Oracle versions for your application.
ORA-01882: timezone region not found
Had the same problem in .Net Core 3.1 API with controllers.
had the error on debug, and solved it adding TZ on \Properties\launchSettings.json:
"Docker": {
...
"environmentVariables": {
"TZ": "WET"
}
}
This code does not provide an answer but it may help to find your problem.
The code is too big for a comment.
Please execute this program. What is the output? Where does it fail?
static void Main(string[] args)
{
Console.WriteLine("OracleGlobalization.TimeZone = {0}", Oracle.DataAccess.Client.OracleGlobalization.GetClientInfo().TimeZone);
Console.WriteLine(String.Empty);
Console.WriteLine("TimeZone.CurrentTimeZone.StandardName = {0}", TimeZone.CurrentTimeZone.StandardName);
Console.WriteLine("TimeZone.CurrentTimeZone.DaylightName = {0}", TimeZone.CurrentTimeZone.DaylightName);
Console.WriteLine(String.Empty);
Console.WriteLine("TimeZoneInfo.Local.DisplayName = {0}", TimeZoneInfo.Local.DisplayName);
Console.WriteLine("TimeZoneInfo.Local.Id = {0}", TimeZoneInfo.Local.Id);
Console.WriteLine("TimeZoneInfo.Local.StandardName = {0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("TimeZoneInfo.Local.DaylightName = {0}", TimeZoneInfo.Local.DaylightName);
Console.WriteLine(String.Empty);
var str = new Oracle.DataAccess.Client.OracleConnectionStringBuilder();
str.UserID = "<username>";
str.Password = "<password>";
str.DataSource = "<database name>";
using ( var con = new Oracle.DataAccess.Client.OracleConnection(str.ConnectionString) ) {
con.Open();
Console.WriteLine("Oracle.DataAccess: OracleConnection -> SessionInfo.TimeZone = {0}", con.GetSessionInfo().TimeZone);
Console.WriteLine("Oracle.DataAccess: Version = {0}", FileVersionInfo.GetVersionInfo(con.GetType().Assembly.Location).FileVersion.ToString());
var tz = new Oracle.DataAccess.Client.OracleCommand("SELECT SESSIONTIMEZONE FROM dual", con).ExecuteScalar();
Console.WriteLine("Oracle.DataAccess: SESSIONTIMEZONE = {0}", tz.ToString());
con.Close();
}
Console.WriteLine(String.Empty);
var strm = new Oracle.ManagedDataAccess.Client.OracleConnectionStringBuilder();
str.UserID = "<username>";
str.Password = "<password>";
str.DataSource = "<database name>";
using ( var con = new Oracle.ManagedDataAccess.Client.OracleConnection(str.ConnectionString) ) {
con.Open();
Console.WriteLine("Oracle.ManagedDataAccess: OracleConnection -> SessionInfo.TimeZone = {0}", con.GetSessionInfo().TimeZone);
Console.WriteLine("Oracle.ManagedDataAccess: Version = {0}", FileVersionInfo.GetVersionInfo(con.GetType().Assembly.Location).FileVersion.ToString());
var tz = new Oracle.ManagedDataAccess.Client.OracleCommand("SELECT SESSIONTIMEZONE FROM dual", con).ExecuteScalar();
Console.WriteLine("Oracle.ManagedDataAccess: SESSIONTIMEZONE = {0}", tz.ToString());
con.Close();
}
}
Update
According to your profile you are based in Sri Lanka. Time zone of Sri Lanka seems to be fairly "volatile", see text from IANA TimeZone Database:
Sri Lanka
From Paul Eggert (2013-02-21):
Milne says "Madras mean time use from May 1, 1898. Prior to this Colombo
mean time, 5h. 4m. 21.9s. F., was used." But 5:04:21.9 differs considerably
from Colombo's meridian 5:19:24, so for now ignore Milne and stick with
Shanks and Pottenger.
From Paul Eggert (1996-09-03):
"Sri Lanka advances clock by an hour to avoid blackout"
(http://www.virtual-pc.com/lankaweb/news/items/240596-2.html, 1996-05-24,
no longer available as of 1999-08-17)
reported "the country's standard time will be put forward by one hour at
midnight Friday (1830 GMT) 'in the light of the present power crisis'."
From Dharmasiri Senanayake, Sri Lanka Media Minister (1996-10-24), as quoted
by Shamindra in Daily News - Hot News Section
(1996-10-26):
With effect from 12.30 a.m. on 26th October 1996
Sri Lanka will be six (06) hours ahead of GMT.
From Jesper Nørgaard Welen (2006-04-14), quoting Sri Lanka News Online
http://news.sinhalaya.com/wmview.php?ArtID=11002 (2006-04-13):
0030 hrs on April 15, 2006 (midnight of April 14, 2006 +30 minutes)
at present, become 2400 hours of April 14, 2006 (midnight of April 14, 2006).
From Peter Apps and Ranga Sirila of Reuters (2006-04-12) in:
http://today.reuters.co.uk/news/newsArticle.aspx?type=scienceNews&storyID=2006-04-12T172228Z_01_COL295762_RTRIDST_0_SCIENCE-SRILANKA-TIME-DC.XML
[The Tamil Tigers] never accepted the original 1996 time change and simply
kept their clocks set five and a half hours ahead of Greenwich Mean
Time (GMT), in line with neighbor India.
From Paul Eggert (2006-04-18):
People who live in regions under Tamil control can use [TZ='Asia/Kolkata'],
as that zone has agreed with the Tamil areas since our cutoff date of 1970.
From Sadika Sumanapala (2016-10-19):
According to http://www.sltime.org (maintained by Measurement Units,
Standards & Services Department, Sri Lanka) abbreviation for Sri Lanka
standard time is SLST.
From Paul Eggert (2016-10-18):
"SLST" seems to be reasonably recent and rarely-used outside time
zone nerd sources. I searched Google News and found three uses of
it in the International Business Times of India in February and
March of this year when discussing cricket match times, but nothing
since then (though there has been a lot of cricket) and nothing in
other English-language news sources. Our old abbreviation "LKT" is
even worse. For now, let's use a numeric abbreviation; we can
switch to "SLST" if it catches on.
I assume your database does not recognize your current local time zone of your computer. There might be three possible solutions:
Change your computer locale settings to a more stable one, e.g. India
Update database with latest version of time zone file, see Upgrading the Time Zone File and Timestamp with Time Zone Data
In case you use the ODP.NET Unmanaged driver Oracle.DataAccess.dll (unfortunately you did not tell us) you can set ORA_SDTZ environment variable or Registry key HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_<Oracle home name>\ORA_SDTZ / HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ORACLE\KEY_<Oracle home name>\ORA_SDTZ to desired time zone value. Note, the ODP.NET Managed Driver does not read this Registry values.
According to this straight from an Oracle employee, set UseHourOffsetForUnsupportedTimezone property for the Connection to true, before opening it.
con.UseHourOffsetForUnsupportedTimezone = true;
con.Open();
This worked for me as I did not have control on the OracleDB server, and in my crossplatform application, the environment variable usage was not being consistent across linux distros. I had ran Weinfried's snippet on all of them to test and there was no timezone descrepency with OracleDB in my case(both on UTC, no DST) but was still failing on the same plank.
According to this and this:
In C:\Program Files\datamodeler 3\datamodeler\bin\datamodeler.conf add
AddVMOption -Duser.timezone="+02:00"
Hi I am migrating from DDay.ical to Ical.Net nuget pacakages but I get stuck in the following code which add Timezone in DDay.Ical calendar please help
Previous code:
List<DOAppointment> lst = objResponse.Appointments;
string timeZoneName = objResponse.UserTimezone;
iCalendar calendar = new DDay.iCal.iCalendar();
var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
calendar.AddTimeZone(iCalTimeZone.FromSystemTimeZone(timeZone));
Migrating into Ical.Net:
List<DOAppointment> lst = objResponse.Appointments;
string timeZoneName = objResponse.UserTimezone;
Ical.Net.Calendar calendar = new Ical.Net.Calendar();
var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
ITimeZone tzID = timeZone;
calendar.AddTimeZone(tzID);
Here I know that calendar.AddTimezone will take ITimezone but how to pass it I am not getting please help.
VTimeZone is the concrete implementation of the ITimeZone interface. The ical.net example would look like this:
var calendar = new Ical.Net.Calendar();
// ical.net supports BCL time zones as well as IANA time zones
calendar.AddTimeZone(new VTimeZone(objResponse.UserTimezone));
In the future, I may change the VTimeZone constructor to throw an exception if the time zone string doesn't match anything known. Right now, though, it's pretty dumb. Behind the scenes, ical.net will default to the system time zone that the code is running on, if all else fails. That's probably not good.
I also added a wiki page with an adaptation of your question:
https://github.com/rianjs/ical.net/wiki/Working-with-time-zones
I am logging time in many places
If Request.DynamicSettings.AirlineSettings.AirlineGeneralSettings.TimeLogEnabled Then
StartTime = DateTime.Now
LogTime(Reflection.MethodBase.GetCurrentMethod.DeclaringType.FullName, Reflection.MethodBase.GetCurrentMethod.Name, StartTime, DateTime.Now, "AB-SCR(I)", 0,)
End If
all places i have used
DateTime.Now
I am facing an issue now,
I am currently hosting this in a gulf server, GMT +4:00
I need to host this same project for another country at Gmt +3Gmt
for this hosting i need time to be logged using that country's local time.
Is there any way to do this, without having to modify each and every line of my code.
i have seen this article timzone with asp.net but as my service is already up i have a lot of codes to change, i am looking for a simpler solution.
thanks.
A few things:
You cannot change the time zone in the IIS configuration or web.config. This is not an IIS problem, but rather a problem in your application code.
DateTime.Now should never be used in a server side application, such as an ASP.Net web application. Read The case against DateTime.Now.
If you are just timing how long something takes to run, don't use DateTime at all. Instead, use System.Diagnostics.Stopwatch.
Stopwatch sw = Stopwatch.StartNew();
// ...do some work ...
sw.Stop();
TimeSpan elapsed = sw.Elapsed; // how long it took will be in the Elapsed property
If you actually want the current time in a specific time zone, then you need to know the time zone identifier. (GMT+4 and GMT+3 are not time zones, but rather time zone offsets see "Time Zone != Offset" in the timezone tag wiki.) You can see a list of Windows time zones by using TimeZoneInfo.GetSystemTimeZones(), or by calling tzutil /l on the command line.
Then in your application:
string tz = "Arabian Standard Time";
DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);
You should probably refactor your code such that this is done inside your LogTime method. Then you will have only one place to set the time zone for your application.
I´m having trouble when working with dates in my Web MVC project. It is running in my local IIS.
The problem is, I live in Brazil, Rio de Janeiro, so according to a this my TimeZone is
E. South America Standard Time
That means Brasilia with GMT-3. And it is perfect and correct.
But when running my unit test in NCrunch, for the code above:
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
var zone = TimeZone.CurrentTimeZone;
var time = zone.GetDaylightChanges(DateTime.Today.Year);
var start = time.Start;
var end = time.End;
var offset = zone.GetUtcOffset(DateTime.Now);
I ´m receiving the following results:
zone.DaylightName = "E. South America Daylight Time" (CORRECT)
time.Start = {18/10/2014 23:59:59} (CORRECT FOR 2014)
time.End = {15/02/2014 23:59:59} (CORRECT FOR 2014)
offset = {-2:00:00} (WRONT, it is -3) -> it would be right if there was no daylight saving time
TimeZone.IsDaylightSavingTime(DateTime.Now) = true
The problem is that when I send it to Postgres DB it saves as timestampZ so, it saves correct
for example 01/01/01 01:01:01 -3
But then when retrieving from database, it comes the mistake, because of the wrong offset I get
01/01/01 02:01:01 -3
It is runnin in local IIS but it should use the local computer time, right? If I go to my computer settings is is correct (Brasilia Time, GMT-3)
How should I proceed?
Thanks in regards,
Edit
Actually the problem is in my PostgresSQL. It is saving the current time with offset -3 instead of -2 and that is all the problem. When i get it back in server it adds one hour...
How do I enable DST in Postgres?!?
I am working with MPXJ framework to generate an XML file with i import into project 2007. I have run in to allot of problems but i can't seem to find an answer to the following.
I have a fixed Work and duration on a task, but when i add a resource and import it in project i get an error message:
"The resource is assigned outside the original dates for task 2 in project. The duration of this fixed-duration task will change to accommodate the resource assignment".
Project then changes the work and or duration value. that is not what i want. i want the resource to use the 24 hours calendar but i can't seem to attach the project 2007 standard calendar, so i thought lets make my own 24 hours calendar and attach that to resource. now i can't seem to set the 0:00:00 to 0:00:00 time in a work day.
ProjectCalendar calendar = projectFile.addResourceCalendar();
calendar.setName("24 Hours");
calendar.setUniqueID(Count);
calendar.setWorkingDay(Day.MONDAY, true);
calendar.setWorkingDay(Day.TUESDAY, true);
calendar.setWorkingDay(Day.WEDNESDAY, true);
calendar.setWorkingDay(Day.TUESDAY, true);
calendar.setWorkingDay(Day.FRIDAY, true);
Resource resource = projectFile.addResource();
resource.setUniqueID(Count);
resource.setName("Painters");
resource.setResourceCalendar(calendar);
Any one know of a solution to one get the default 24 hour calendar set to the resource of make my own.
The code below illustrates how to create a 24 hour calendar. My apologies for the long-winded way each day is set up, I need to improve the way the Day class works so that it is easier to iterate.
The key point to note is that the DateRange instance is set up to start at 00:00 hours on one date, and finish at 00:00 hours on the following day. The actual date used is irrelevant, the calendar is only using the time element of the date.
Hope that makes sense!
//
// Use this date formatter to make it simple to specific the range
// start and wne dates
//
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//
// This is an immutable date range, so we can share it without
// worrying about it being changed
//
DateRange range = new DateRange(format.parse("2000-01-01 00:00"), format.parse("2000-01-02 00:00"));
//
// Add the calendar and name it
//
ProjectCalendar test = file.addBaseCalendar();
test.setName("Test 24 Hours");
//
// Mark each day as working
//
test.setWorkingDay(Day.SUNDAY, true);
test.setWorkingDay(Day.MONDAY, true);
test.setWorkingDay(Day.TUESDAY, true);
test.setWorkingDay(Day.WEDNESDAY, true);
test.setWorkingDay(Day.THURSDAY, true);
test.setWorkingDay(Day.FRIDAY, true);
test.setWorkingDay(Day.SATURDAY, true);
//
// Add a working hours range to each day
//
ProjectCalendarHours hours;
hours = test.addCalendarHours(Day.SUNDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.MONDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.TUESDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.WEDNESDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.THURSDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.FRIDAY);
hours.addRange(range);
hours = test.addCalendarHours(Day.SATURDAY);
hours.addRange(range);