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.Collections.Generic;
  3.  
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class isDST
  8. 	{
  9. 		static string progver = "1.03";
  10.  
  11.  
  12. 		static int Main( string[] args )
  13. 		{
  14. 			// Defaults
  15. 			bool verbose = false;
  16. 			int rc = 1;
  17. 			DateTime checkDate = DateTime.Now;
  18.  
  19.  
  20. 			#region Parse Command Line
  21.  
  22. 			if ( args.Length > 2 )
  23. 			{
  24. 				return ShowHelp( "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 ShowHelp( );
  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 = Convert.ToDateTime( arg );
  52. 						}
  53. 						catch ( FormatException )
  54. 						{
  55. 							return ShowHelp( "Invalid date \"{0}\"", arg );
  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 ShowHelp( "Invalid command line argument(s)." );
  65. 			}
  66.  
  67. 			#endregion Parse Command Line
  68.  
  69.  
  70. 			// Check if date is in DST
  71. 			bool result = TimeZone.CurrentTimeZone.IsDaylightSavingTime( checkDate );
  72.  
  73. 			// Display result on screen
  74. 			if ( verbose )
  75. 			{
  76. 				Console.Write( checkDate.ToLongDateString( ).ToString( ) );
  77. 				if ( result )
  78. 				{
  79. 					rc = 0;
  80. 					Console.ForegroundColor = ConsoleColor.Red;
  81. 					Console.Write( " IS" );
  82. 					Console.ResetColor( );
  83. 				}
  84. 				else
  85. 				{
  86. 					rc = 2;
  87. 					Console.Write( " is" );
  88. 					Console.ForegroundColor = ConsoleColor.Green;
  89. 					Console.Write( " NOT" );
  90. 					Console.ResetColor( );
  91. 				}
  92. 				Console.WriteLine( " in Daylight Saving Time" );
  93. 			}
  94.  
  95. 			// Return result as 'errorlevel'
  96. 			return rc;
  97. 		}
  98.  
  99.  
  100. 		public static int ShowHelp( params string[] errmsg )
  101. 		{
  102. 			#region Help Text
  103.  
  104. 			/*
  105. 			isDST,  Version 1.03
  106. 			Check if a date is in Daylight Saving Time
  107.  
  108. 			Usage:  ISDST  [ date ]  [ /Verbose ]
  109.  
  110. 			Where:  date       is an optional date/time to check (default: today/now)
  111. 					/Verbose   tells the program to display the result on screen
  112.  
  113. 			Notes:  An "errorlevel" 0 is returned if the date is in DST, 2 if the date
  114. 					is not in DST, or 1 in case of (command line) errors.
  115. 					This program uses local date/time formats and timezone settings.
  116. 			        /Verbose switch may be abbreviated to /V.
  117.  
  118. 			Written by Rob van der Woude
  119. 			https://www.robvanderwoude.com
  120. 			*/
  121.  
  122. 			#endregion Help Text
  123.  
  124.  
  125. 			#region Error Message
  126.  
  127. 			if ( errmsg.Length > 0 )
  128. 			{
  129. 				List<string> errargs = new List<string>( errmsg );
  130. 				errargs.RemoveAt( 0 );
  131. 				Console.Error.WriteLine( );
  132. 				Console.ForegroundColor = ConsoleColor.Red;
  133. 				Console.Error.Write( "ERROR:\t" );
  134. 				Console.ForegroundColor = ConsoleColor.White;
  135. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  136. 				Console.ResetColor( );
  137. 			}
  138.  
  139. 			#endregion Error Message
  140.  
  141.  
  142. 			#region Display Help Text
  143.  
  144. 			Console.Error.WriteLine( );
  145.  
  146. 			Console.Error.WriteLine( "isDST,  Version {0}", progver );
  147.  
  148. 			Console.Error.WriteLine( "Check if a date is in Daylight Saving Time" );
  149.  
  150. 			Console.Error.WriteLine( );
  151.  
  152. 			Console.Error.Write( "Usage:  " );
  153. 			Console.ForegroundColor = ConsoleColor.White;
  154. 			Console.Error.WriteLine( "ISDST  [ date ]  [ /Verbose ]" );
  155. 			Console.ResetColor( );
  156.  
  157. 			Console.Error.WriteLine( );
  158.  
  159. 			Console.Error.Write( "Where:  " );
  160. 			Console.ForegroundColor = ConsoleColor.White;
  161. 			Console.Error.Write( "date" );
  162. 			Console.ResetColor( );
  163. 			Console.Error.WriteLine( "       is an optional date/time to check (default: today/now)" );
  164.  
  165. 			Console.ForegroundColor = ConsoleColor.White;
  166. 			Console.Error.Write( "        /V" );
  167. 			Console.ResetColor( );
  168. 			Console.Error.WriteLine( "erbose   tells the program to display the result on screen" );
  169.  
  170. 			Console.Error.WriteLine( );
  171.  
  172. 			Console.Error.WriteLine( "Notes:  An \"errorlevel\" 0 is returned if the date is in DST, 2 if the date" );
  173.  
  174. 			Console.Error.WriteLine( "        is not in DST, or 1 in case of (command line) errors." );
  175.  
  176. 			Console.Error.WriteLine( "        This program uses local date/time formats and timezone settings." );
  177.  
  178. 			Console.ForegroundColor = ConsoleColor.White;
  179. 			Console.Error.Write( "        /Verbose" );
  180. 			Console.ResetColor( );
  181. 			Console.Error.Write( " switch may be abbreviated to " );
  182. 			Console.ForegroundColor = ConsoleColor.White;
  183. 			Console.Error.Write( "/V" );
  184. 			Console.ResetColor( );
  185. 			Console.Error.WriteLine( "." );
  186.  
  187. 			Console.Error.WriteLine( );
  188.  
  189. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  190.  
  191. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  192.  
  193. 			#endregion Display Help Text
  194.  
  195.  
  196. 			return 1;
  197. 		}
  198.  
  199. 	}
  200. }

page last modified: 2024-04-16; loaded in 0.0174 seconds