260 lines
8.5 KiB
C#
260 lines
8.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.ESystem
|
|
{
|
|
public class Time
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
static string copyright = GenerateCopyright();
|
|
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
static string date = value.ToShortDateString();
|
|
static string time = value.ToLongTimeString();
|
|
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
|
|
if (startYear.Contains("-"))
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf("-"));
|
|
}
|
|
else
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
}
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
string start = temp.Remove(temp.IndexOf(startYear) - 1);
|
|
string end = (temp.Remove(0, temp.IndexOf(" by")));
|
|
|
|
return String.Concat(start, " ", startYear, "-", currentYear, end);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the dll file.
|
|
/// </summary>
|
|
public static string DllName { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the dll file.
|
|
/// </summary>
|
|
public static string DllVersion { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice.
|
|
/// </summary>
|
|
public static string Copyright { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the name-, version- and copyright-information of the dll file.
|
|
/// </summary>
|
|
public static string[] DllInfo { private set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
|
|
string[] dllInfo = new string[3];
|
|
dllInfo[0] = dllName;
|
|
dllInfo[1] = dllVersion;
|
|
dllInfo[2] = copyright;
|
|
DllInfo = dllInfo;
|
|
}
|
|
|
|
protected string ProgramName { private set; get; }
|
|
protected string ProgramVersion { private set; get; }
|
|
protected string ProgramCopyright { private set; get; }
|
|
#endregion
|
|
|
|
static Time()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public Time()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write milliseconds or no
|
|
/// </summary>
|
|
public enum WriteMillisecond
|
|
{
|
|
Yes,
|
|
No
|
|
}
|
|
|
|
private static string messageFormat = "{0:T}.{1}"; // Definiert das Mitteilungsformat
|
|
|
|
/// <summary>
|
|
/// Current day
|
|
/// </summary>
|
|
/// <returns>The current day</returns>
|
|
public static string CurrentDay()
|
|
{
|
|
return DateTime.Now.ToString("dddd");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Current date
|
|
/// </summary>
|
|
/// <returns>The current date</returns>
|
|
public static string CurrentDate()
|
|
{
|
|
return DateTime.Now.ToString("dd.MM.yyyy");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Current time
|
|
/// </summary>
|
|
/// <param name="choice">Write milliseconds or not</param>
|
|
/// <returns>The current time</returns>
|
|
public static string CurrentTime(WriteMillisecond choice)
|
|
{
|
|
if (choice == WriteMillisecond.Yes)
|
|
{
|
|
DateTime time = DateTime.Now;
|
|
int ms = time.Millisecond;
|
|
return String.Format(messageFormat, new object[] { time, ms.ToString("000") });
|
|
}
|
|
else
|
|
{
|
|
return DateTime.Now.ToString("hh:mm:ss");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Current date and time
|
|
/// </summary>
|
|
/// <param name="choice">Write milliseconds or not</param>
|
|
/// <returns>The current date and time</returns>
|
|
public static string CurrentDateAndTime(WriteMillisecond choice)
|
|
{
|
|
if (choice == WriteMillisecond.Yes)
|
|
{
|
|
DateTime time = DateTime.Now;
|
|
int ms = time.Millisecond;
|
|
return String.Format(messageFormat, new object[] { time.ToString(), ms.ToString("000") });
|
|
}
|
|
else
|
|
{
|
|
return DateTime.Now.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Current day, date and time
|
|
/// </summary>
|
|
/// <returns>The current day, date and time</returns>
|
|
public static string CurrentDayDateTime()
|
|
{
|
|
return DateTime.Now.ToString("dddd, dd.MM.yyyy hh:mm:ss");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop watch
|
|
/// </summary>
|
|
public class StopWatch
|
|
{
|
|
private DateTime startTime;
|
|
private DateTime endTime;
|
|
|
|
/// <summary>
|
|
/// Starts the stopwatch
|
|
/// </summary>
|
|
/// <returns>Returns the start time</returns>
|
|
public string Start()
|
|
{
|
|
startTime = DateTime.Now;
|
|
return startTime.ToString("yyyy-MM-dd HH:mm:ss.ms");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops the stopwatch
|
|
/// </summary>
|
|
/// <returns>Returns the stop time</returns>
|
|
public string Stop()
|
|
{
|
|
endTime = DateTime.Now;
|
|
return endTime.ToString("yyyy-MM-dd HH:mm:ss.ms");
|
|
}
|
|
|
|
/// <summary>
|
|
/// The time difference
|
|
/// </summary>
|
|
/// <returns>Returns the time difference</returns>
|
|
public string DurationTime()
|
|
{
|
|
if (startTime.ToString()!="01.01.0001 00:00:00" && startTime<=endTime)
|
|
{
|
|
TimeSpan timeSpan = endTime.Subtract(startTime);
|
|
return String.Format("{0:00}D{1:00}:{2:00}:{3:00}.{4:000}", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
|
|
}
|
|
else
|
|
{
|
|
return String.Empty;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|