using System; using System.Collections.Generic; namespace RobvanderWoude { class isDST { static string progver = "1.03"; static int Main( string[] args ) { // Defaults bool verbose = false; int rc = 1; DateTime checkDate = DateTime.Now; #region Parse Command Line if ( args.Length > 2 ) { return ShowHelp( "Invalid command line argument(s)." ); } foreach ( string arg in args ) { switch ( arg.ToLower( ) ) { case "/?": case "/h": case "-h": case "--h": case "/help": case "-help": case "--help": return ShowHelp( ); case "/v": case "-v": case "--v": case "/verbose": case "-verbose": case "--verbose": verbose = true; break; default: // Check if the argument is a valid date/time try { checkDate = Convert.ToDateTime( arg ); } catch ( FormatException ) { return ShowHelp( "Invalid date \"{0}\"", arg ); } break; } } // Two command line arguments are allowed, but only if one of these is the /Verbose switch if ( args.Length == 2 && !verbose ) { return ShowHelp( "Invalid command line argument(s)." ); } #endregion Parse Command Line // Check if date is in DST bool result = TimeZone.CurrentTimeZone.IsDaylightSavingTime( checkDate ); // Display result on screen if ( verbose ) { Console.Write( checkDate.ToLongDateString( ).ToString( ) ); if ( result ) { rc = 0; Console.ForegroundColor = ConsoleColor.Red; Console.Write( " IS" ); Console.ResetColor( ); } else { rc = 2; Console.Write( " is" ); Console.ForegroundColor = ConsoleColor.Green; Console.Write( " NOT" ); Console.ResetColor( ); } Console.WriteLine( " in Daylight Saving Time" ); } // Return result as 'errorlevel' return rc; } public static int ShowHelp( params string[] errmsg ) { #region Help Text /* isDST, Version 1.03 Check if a date is in Daylight Saving Time Usage: ISDST [ date ] [ /Verbose ] Where: date is an optional date/time to check (default: today/now) /Verbose tells the program to display the result on screen Notes: An "errorlevel" 0 is returned if the date is in DST, 2 if the date is not in DST, or 1 in case of (command line) errors. This program uses local date/time formats and timezone settings. /Verbose switch may be abbreviated to /V. Written by Rob van der Woude https://www.robvanderwoude.com */ #endregion Help Text #region Error Message if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } #endregion Error Message #region Display Help Text Console.Error.WriteLine( ); Console.Error.WriteLine( "isDST, Version {0}", progver ); Console.Error.WriteLine( "Check if a date is in Daylight Saving Time" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "ISDST [ date ] [ /Verbose ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "date" ); Console.ResetColor( ); Console.Error.WriteLine( " is an optional date/time to check (default: today/now)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /V" ); Console.ResetColor( ); Console.Error.WriteLine( "erbose tells the program to display the result on screen" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Notes: An \"errorlevel\" 0 is returned if the date is in DST, 2 if the date" ); Console.Error.WriteLine( " is not in DST, or 1 in case of (command line) errors." ); Console.Error.WriteLine( " This program uses local date/time formats and timezone settings." ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /Verbose" ); Console.ResetColor( ); Console.Error.Write( " switch may be abbreviated to " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/V" ); Console.ResetColor( ); Console.Error.WriteLine( "." ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "https://www.robvanderwoude.com" ); #endregion Display Help Text return 1; } } }