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

page last uploaded: 2018-12-04, 10:47