Monday, April 12, 2010

Daylight Savings Notice


Our office does not observe Daylight Savings. I wanted to display a notice on our order form to make users aware when daylight savings was changing. After spending hours not finding a way, I found code here . I added a method to it adapted from his code. I just wanted the dates daylight saving start and ends for a given TimeZone. I had already tried:

DaylightTime daylight = TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Today.Year);

But that only gives it for the time zone of the server. I needed to get a System.Globalization.DaylightTime for a timezone that observes Daylight Saving.

public static DaylightTime GetDaylightChanges(TimeZoneInfo InTimeZoneInfo, int InYear)
  {
   TimeZoneInfo.AdjustmentRule ruleFound = null;


   TimeZoneInfo.AdjustmentRule[] adjustments = InTimeZoneInfo.GetAdjustmentRules();
   if (adjustments.Length == 0)
   {
    throw new Exception(InTimeZoneInfo.StandardName + " has no adjustment rules");
   }
   //Find the correct adjustment rule
   foreach (TimeZoneInfo.AdjustmentRule adjustment in adjustments)
   {
    if (adjustment.DateStart.Year <= InYear && adjustment.DateEnd.Year >= InYear)
     ruleFound = adjustment;
   }
   if (ruleFound == null)
   {
    throw new Exception("No TimeZoneInfo.AdjustmentRule found for TimeZoneInfo "
     + InTimeZoneInfo.StandardName +" for year " + InYear);
   }


   DaylightTime outDaylightTime = new DaylightTime(
      GetDateTime(InYear, ruleFound.DaylightTransitionStart)
     , GetDateTime(InYear, ruleFound.DaylightTransitionEnd)
     , ruleFound.DaylightDelta);


   return outDaylightTime;
  }



public static DateTime GetDateTime(int Year, TimeZoneInfo.TransitionTime transactionTime)
{
//Create a datetime to begin with 1st of the transition month
DateTime dt = new DateTime(Year, transactionTime.Month,
1, transactionTime.TimeOfDay.Hour,
transactionTime.TimeOfDay.Minute, transactionTime.TimeOfDay.Second);


//If the dayofweek of 1st is same as the transition day then exit
//otherwise 
if (dt.DayOfWeek != transactionTime.DayOfWeek)
{
//If transition dayofweek is greater than 1st dayofweek then we need to move further
//Eg : Transition dayofweek is tuesday and 1st day of week is monday then we need to move 1 day ahead to point to 
//the transition day
if (dt.DayOfWeek < transactionTime.DayOfWeek)
{
dt.AddDays(transactionTime.DayOfWeek - dt.DayOfWeek);
}
else
{
//else its not in the 1st week so we move 7 days ahead and move back again
dt = dt.AddDays(7 - (dt.DayOfWeek - transactionTime.DayOfWeek));
}
}


//Since we are already pointing to the first week of the transition date
//Add remaining no of weeks to the datetime
return dt.AddDays((transactionTime.Week - 1) * 7);
}


Here is the usage.

TimeZoneInfo PacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DaylightTime daylight = CustomTimeZone.GetDaylightChanges(PacificTimeZone, 2010);
Console.WriteLine("daylight.Start = " + daylight.Start);
Console.WriteLine("daylight.End = " + daylight.End);
Console.WriteLine("daylight.Delta = " + daylight.Delta);

Saturday, January 09, 2010

Setting Up Gmail and Outlook on iPod Touch

On the iPod Touch, you can only set up one Exchange account. It is this kind of account Google recommends you set up to synch your contacts with the iPod. I want to be able to access both my Gmail and my work Exchange accounts on my iPod. I also want my Gmail contacts on the iPod. This is how I did it.

I created a sub folder named "Personal Contacts" to my contacts in Outlook. Following these steps I exported my Gmail contacts to a file and then imported them into "Personal Contacts". On my iPod, I set up Gmail as an IMAP account and my work email as my Exchange account.

I now have both email accounts and both sets of contacts available in my iPod.

Tuesday, December 22, 2009

Tabs or Spaces

Some programmers have strong opinions about whether to use tabs or spaces to indent code.
This is what I use. In Visual Studio 2017 choose
Tools\Options\Text Editor\All Languages\Tabs
Select Smart Indenting
Tab size and Indent size = 4
Select "Insert spaces"

If you use Shift+Tab when in front of 4 spaces, it will delete the 4 spaces the same as if you press backspace in front of a tab character.
To convert a file one way or another there is "Tabify Selected Lines" and "Untabify Selected Lines" under the Edit\Advanced menu.

View White Space

Use the following Menu in Visual Studio to view the white space characters
Edit->Advanced->View White Space
This allows you to see whether the white space is tabs or spaces.

For me these characters are too prominent. So I dim them by choosing
Tools->Options->
Environment->Fonts and Colors
Select "Visible White Space" from the Display Items.
Select Silver. Then Click the Custom Button and use the darkness scale to choose the right amount of darkness.

In Beyond Compare you can select the following from the View menu:
Show White Space
Ignore Unimportant Differences

They are useful when dealing with differences in white space.

Wednesday, December 16, 2009

Tech Support Cheat Sheet

From xkcd, here is one great secret to computer tech support. Click on the picture to enlarge the flowchart.



I have written out some of the diagram so I can more easily find this post when I need it.
Google the name of the program plus a few words related to what you want to do. Follow any instructions.

Thursday, November 19, 2009

Covariance and Contravariance

My colleague directed me to some programming terms, covariance and contravariance.

Eric Lippert describes them quite well.

Wednesday, November 04, 2009

Reporting Services 2008 Custom Security

In MS SQL Server Reporting Services 2008 there is no longer an easy way to allow for anonymous access. I followed the steps here to get a custom security implemented. I modified it further to integrate with our existing security model.

You could also modify it to achieve anonymous access to your report server