Powered by GeSHi

Source code for isdst.cs

(view source code of isdst.cs as plain text)

  1. using System;
  2. using System.Globalization;
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	class isDST
  7. 	{
  8. 		static int Main( string[] args )
  9. 		{
  10. 			try
  11. 			{
  12. 				// Defaults
  13. 				bool verbose = false;
  14. 				DateTime checkDate = DateTime.Now;
  15.  
  16. 				#region command line parsing
  17.  
  18. 				if ( args.Length > 2 )
  19. 				{
  20. 					return WriteError( "Invalid command line argument(s)." );
  21. 				}
  22.  
  23. 				foreach ( string arg in args )
  24. 				{
  25. 					switch ( arg.ToLower( ) )
  26. 					{
  27. 						case "/?":
  28. 						case "/h":
  29. 						case "-h":
  30. 						case "--h":
  31. 						case "/help":
  32. 						case "-help":
  33. 						case "--help":
  34. 							return WriteError( string.Empty );
  35. 						case "/v":
  36. 						case "-v":
  37. 						case "--v":
  38. 						case "/verbose":
  39. 						case "-verbose":
  40. 						case "--verbose":
  41. 							verbose = true;
  42. 							break;
  43. 						default:
  44. 							// Check if the argument is a valid date/time
  45. 							try
  46. 							{
  47. 								checkDate = System.Convert.ToDateTime( arg );
  48. 							}
  49. 							catch ( Exception e )
  50. 							{
  51. 								return WriteError( e );
  52. 							}
  53. 							break;
  54. 					}
  55. 				}
  56.  
  57. 				// Two command line arguments are allowed, but only if one of these is the /Verbose switch
  58. 				if ( args.Length == 2 && !verbose )
  59. 				{
  60. 					return WriteError( "Invalid command line argument(s)." );
  61. 				}
  62.  
  63. 				#endregion command line parsing
  64.  
  65. 				// Display result on screen
  66. 				if ( verbose )
  67. 				{
  68. 					Console.WriteLine( "{0} {1} in Daylight Saving Time", checkDate.ToLongDateString( ).ToString( ), ( TimeZone.CurrentTimeZone.IsDaylightSavingTime( checkDate ) ? "IS" : "is NOT " ) );
  69. 				}
  70.  
  71. 				// Return result as 'errorlevel'
  72. 				if ( TimeZone.CurrentTimeZone.IsDaylightSavingTime( checkDate ) )
  73. 				{
  74. 					return 0;
  75. 				}
  76. 				else
  77. 				{
  78. 					return 2;
  79. 				}
  80. 			}
  81. 			catch ( Exception e )
  82. 			{
  83. 				return WriteError( e );
  84. 			}
  85. 		}
  86.  
  87. 		public static int WriteError( Exception e )
  88. 		{
  89. 			return WriteError( e == null ? null : e.Message );
  90. 		}
  91.  
  92. 		public static int WriteError( string errorMessage )
  93. 		{
  94. 			/*
  95. 			isDST,  Version 1.01
  96. 			Check if a date is in Daylight Saving Time
  97.  
  98. 			Usage:  ISDST  [ date ]  /Verbose
  99.  
  100. 			Where:  date       is an optional date/time to check (default: today/now)
  101. 					/Verbose   tells the program to display the result on screen
  102.  
  103. 			Notes:  An "errorlevel" 0 is returned if the date is in DST, 2 if the date
  104. 					is not in DST, or 1 in case of (command line) errors.
  105. 					This program uses local date/time formats and timezone settings.
  106. 			        /Verbose switch may be abbreviated to /V.
  107.  
  108. 			Written by Rob van der Woude
  109. 			http://www.robvanderwoude.com
  110. 			*/
  111.  
  112. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  113. 			{
  114. 				Console.Error.WriteLine( );
  115. 				Console.ForegroundColor = ConsoleColor.Red;
  116. 				Console.Error.Write( "ERROR:  " );
  117. 				Console.ForegroundColor = ConsoleColor.White;
  118. 				Console.Error.WriteLine( errorMessage );
  119. 				Console.ResetColor( );
  120. 			}
  121. 			Console.Error.WriteLine( );
  122. 			Console.Error.WriteLine( "isDST,  Version 1.01" );
  123. 			Console.Error.WriteLine( "Check if a date is in Daylight Saving Time" );
  124. 			Console.Error.WriteLine( );
  125. 			Console.Error.Write( "Usage:  " );
  126. 			Console.ForegroundColor = ConsoleColor.White;
  127. 			Console.Error.WriteLine( "ISDST  [ date ]  /Verbose" );
  128. 			Console.ResetColor( );
  129. 			Console.Error.WriteLine( );
  130. 			Console.Error.Write( "Where:  " );
  131. 			Console.ForegroundColor = ConsoleColor.White;
  132. 			Console.Error.Write( "date" );
  133. 			Console.ResetColor( );
  134. 			Console.Error.WriteLine( "       is an optional date/time to check (default: today/now)" );
  135. 			Console.ForegroundColor = ConsoleColor.White;
  136. 			Console.Error.Write( "        /V" );
  137. 			Console.ResetColor( );
  138. 			Console.Error.WriteLine( "erbose   tells the program to display the result on screen" );
  139. 			Console.Error.WriteLine( );
  140. 			Console.Error.WriteLine( "Notes:  An \"errorlevel\" 0 is returned if the date is in DST, 2 if the date" );
  141. 			Console.Error.WriteLine( "        is not in DST, or 1 in case of (command line) errors." );
  142. 			Console.Error.WriteLine( "        This program uses local date/time formats and timezone settings." );
  143. 			Console.ForegroundColor = ConsoleColor.White;
  144. 			Console.Error.Write( "        /Verbose" );
  145. 			Console.ResetColor( );
  146. 			Console.Error.Write( " switch may be abbreviated to " );
  147. 			Console.ForegroundColor = ConsoleColor.White;
  148. 			Console.Error.Write( "/V" );
  149. 			Console.ResetColor( );
  150. 			Console.Error.WriteLine( "." );
  151. 			Console.Error.WriteLine( );
  152. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  153. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  154. 			return 1;
  155. 		}
  156.  
  157. 	}
  158. }