Click here to Skip to main content
15,896,334 members
Articles / Security / Encryption

Encrypting Editor Notepad Replacement

Rate me:
Please Sign up or sign in to vote.
4.85/5 (50 votes)
28 Jul 2014CPOL80 min read 179.6K   6.1K   112  
A C# .NET 3.5 (Win7) Windows Forms Application with source code
using System;
using System.Globalization;

namespace ISO8601_Class
{
	/// <summary> A subset of ISO 8601 datetime string functions
	/// Example: 2001-06-01 12:13:13.222
	/// Also supports adjustment of DateTime.Now to a non-local reference time value.
	/// Also supports TimeSpan as a time for today.
	/// Designed for standard Windows 7 .Net 3.5 where timespan.tostring has no format.
	/// </summary> 

	static class ISO8601
	{
		#region Class data
		/// <summary> All the standard formats </summary>
		static string[] formats = {
			"yyyy-MM-dd HH:mm:ss.fff", 
			"yyyy-MM-dd HH:mm:ss", 
			"yyyy-MM-dd HH:mm", 
            "yyyy-MM-dd", 
            "HH:mm:ss.fff",
			"HH:mm:ss",
			"HH:mm"};
		// Difference from a provided reference time value
		static TimeSpan local = new TimeSpan (0);
		#endregion
		/// <summary> Get or set System.DateTime.Now adjusted by a reference time value.
		/// This is used to synchronize between servers that may not be accurate
		/// </summary>
		public static DateTime Now
		{
			// Return adjusted DateTime.Now 
			get
			{
				return System.DateTime.Now + local;
			}
			// save adjustment of DateTime.Now to a reference time value
			set
			{
				local = value - System.DateTime.Now;
			}
		}
		/// <summary> Return ISO format time string for ISO8601.Now 
		/// including hours, minutes, seconds, milliseconds "HH:mm:ss.fff"
		/// </summary> 
		public static string Time ()
		{
			return Time (Now);
		}
		/// <summary> Return ISO format time string for ISO8601.Now 
		/// including hours, minutes, seconds "HH:mm:ss"
		/// </summary> 
		public static string TimeHMS ()
		{
			return TimeHMS (Now);
		}
		/// <summary> Return ISO format time string for ISO8601.Now 
		/// including hours, minutes "HH:mm"
		/// </summary> 
		public static string TimeHM ()
		{
			return TimeHM (Now);
		}
		/// <summary> Return ISO format date string for ISO8601.Now
		/// "yyyy-MM-dd"
		/// </summary> 
		public static string Date ()
		{
			return Date (Now);
		}
		/// <summary> Return ISO format datetime string for ISO8601.Now
		/// including hours, minutes, seconds, milliseconds "yyyy-MM-dd HH:mm:ss.fff"
		/// </summary> 
		public static string DateTime ()
		{
			return DateTime (Now);
		}
		/// <summary> Return ISO format datetime string for ISO8601.Now
		/// including hours, minutes, seconds "yyyy-MM-dd HH:mm:ss"
		/// </summary> 
		public static string DateTimeHMS ()
		{
			return DateTimeHMS (Now);
		}
		/// <summary> Return ISO format datetime string for ISO8601.Now
		/// including hours, minutes "yyyy-MM-dd HH:mm"
		/// </summary> 
		public static string DateTimeHM ()
		{
			return DateTimeHM (Now);
		}
		/// <summary> Return ISO format time string for TimeSpan value
		/// including hours, minutes, seconds, milliseconds "HH:mm:ss.fff"
		/// </summary> 
		/// <param name="timespan">The TimeSpan value to convert to string</param> 
		public static string Time (TimeSpan timespan)
		{
			// Only time today!
			if (TimeSpan.Zero > timespan)
				timespan = TimeSpan.Zero;
			// no TimeSpan format string in standard windows 7 .Net 3.5
			return Time (Now.Date + timespan);
		}
		/// <summary> Return ISO format time string for TimeSpan value
		/// including hours, minutes, seconds "HH:mm:ss"
		/// </summary> 
		/// <param name="timespan">The TimeSpan value to convert to string</param> 
		public static string TimeHMS (TimeSpan timespan)
		{
			// Only time today!
			if (TimeSpan.Zero > timespan)
				timespan = TimeSpan.Zero;
			// no TimeSpan format string in standard windows 7 .Net 3.5
			return TimeHMS (Now.Date + timespan);
		}
		/// <summary> Return ISO format time string for TimeSpan value
		/// including hours, minutes "HH:mm"
		/// </summary> 
		/// <param name="timespan">The TimeSpan value to convert to string</param> 
		public static string TimeHM (TimeSpan timespan)
		{
			// Only time today!
			if (TimeSpan.Zero > timespan)
				timespan = TimeSpan.Zero;
			// no TimeSpan format string in standard windows 7 .Net 3.5
			return TimeHM (Now.Date + timespan);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes, seconds, milliseconds "yyyy-MM-dd HH:mm:ss.fff"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string Time (DateTime datetime)
		{
			return datetime.ToString (formats[4]);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes, seconds "yyyy-MM-dd HH:mm:ss"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string TimeHMS (DateTime datetime)
		{
			return datetime.ToString (formats[5]);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes "yyyy-MM-dd HH:mm"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string TimeHM (DateTime datetime)
		{
			return datetime.ToString (formats[6]);
		}
		/// <summary> Return ISO format date string for DateTime value "yyyy-MM-dd"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string Date (DateTime datetime)
		{
			return datetime.ToString (formats[3]);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes, seconds, milliseconds "yyyy-MM-dd HH:mm:ss.fff"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string DateTime (DateTime datetime)
		{
			return datetime.ToString (formats[0]);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes, seconds "yyyy-MM-dd HH:mm:ss"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string DateTimeHMS (DateTime datetime)
		{
			return datetime.ToString (formats[1]);
		}
		/// <summary> Return ISO format datetime string for DateTime value
		/// including hours, minutes "yyyy-MM-dd HH:mm"
		/// </summary> 
		/// <param name="datetime">The DateTime value to convert to string</param> 
		public static string DateTimeHM (DateTime datetime)
		{
			return datetime.ToString (formats[2]);
		}
		/// <summary> Return DateTime value from ISO format date and/or time string
		/// "[yyyy-MM-dd] [HH:mm[:ss[.fff]]]"
		/// </summary> 
		/// <param name="s">The string to convert to DateTime value</param> 
		public static DateTime TryParseExact (string str)
		{
			DateTime dateValue;

			System.DateTime.TryParseExact (str, formats, CultureInfo.InvariantCulture,
						   DateTimeStyles.None, out dateValue);
			return dateValue;
		}
		/// <summary> Visual validate by round-trip conversion of Now values on Console 
		/// </summary> 
		public static void Validate ()
		{
			DateTime datetime = Now;
			TimeSpan time = datetime.TimeOfDay;
			Console.WriteLine ("\nTryParseExact.ToString() = Function = Result");
			Console.WriteLine ("\nUsing DateTime\n");
			Validate ("DateTime", DateTime (datetime));
			Validate ("Date", Date (datetime));
			Validate ("DateTimeHMS", DateTimeHMS (datetime));
			Validate ("DateTimeHM", DateTimeHM (datetime));
			Validate ("Time", Time (datetime));
			Validate ("TimeHMS", TimeHMS (datetime));
			Validate ("TimeHM", TimeHM (datetime));
			Console.WriteLine ("\nUsing Timespan\n");
			Validate ("Time", Time (time));
			Validate ("TimeHMS", TimeHMS (time));
			Validate ("TimeHM", TimeHM (time));
		}
		/// <summary> Console output format for visual validate
		/// </summary> 
		public static void Validate (string description, string s)
		{
			Console.WriteLine (TryParseExact (s) + " = " +description.PadLeft (15) + " = " + s);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer IAUA End Time Ministry
United States United States
I am a retired Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.

I am currently a writer for my personal ministry: IAUA End Time Ministry

Comments and Discussions