TimeHelper
Provides various helper methods and properties for working with dates, times, and time offsets.
Propriedades
public static DateTime Now { get; }
value = TimeHelper.Now
Gets the current time including the configured offset.
public static TimeSpan NowOffset { get; set; }
value = TimeHelper.NowOffset
TimeHelper.NowOffset = value
Gets or sets the offset applied to the current time when retrieving Now.
public static DateTimeOffset NowWithOffset { get; }
value = TimeHelper.NowWithOffset
Gets the current time as a DateTimeOffset including the local offset.
public static TimeSpan TimeZoneOffset { get; set; }
value = TimeHelper.TimeZoneOffset
TimeHelper.TimeZoneOffset = value
Gets or sets the time zone offset used for calculations.
public static double UnixNowMls { get; }
value = TimeHelper.UnixNowMls
Gets the current Unix time in milliseconds.
public static double UnixNowS { get; }
value = TimeHelper.UnixNowS
Gets the current Unix time in seconds.
Métodos
public static TimeSpan AddMicroseconds(TimeSpan t, long microseconds)
result = TimeHelper.AddMicroseconds(t, microseconds)
Adds the specified number of microseconds to a TimeSpan.
public static DateTime AddMicroseconds(DateTime dt, long microseconds)
result = TimeHelper.AddMicroseconds(dt, microseconds)
Adds the specified number of microseconds to a DateTime.
public static DateTimeOffset AddMicroseconds(DateTimeOffset dto, long microseconds)
result = TimeHelper.AddMicroseconds(dto, microseconds)
Adds the specified number of microseconds to a DateTimeOffset.
public static DateTime AddNanoseconds(DateTime dt, long nanoseconds)
result = TimeHelper.AddNanoseconds(dt, nanoseconds)
Adds the specified number of nanoseconds to a DateTime.
public static DateTimeOffset AddNanoseconds(DateTimeOffset dto, long nanoseconds)
result = TimeHelper.AddNanoseconds(dto, nanoseconds)
Adds the specified number of nanoseconds to a DateTimeOffset.
public static TimeSpan AddNanoseconds(TimeSpan t, long nanoseconds)
result = TimeHelper.AddNanoseconds(t, nanoseconds)
Adds the specified number of nanoseconds to a TimeSpan.
public static DateTimeOffset ApplyChina(DateTime dt)
result = TimeHelper.ApplyChina(dt)
Creates a DateTimeOffset from a DateTime in China Standard Time.
public static DateTimeOffset ApplyEst(DateTime dt)
result = TimeHelper.ApplyEst(dt)
Creates a DateTimeOffset from a DateTime in Eastern Standard Time.
public static DateTimeOffset ApplyLocal(DateTime dt)
result = TimeHelper.ApplyLocal(dt)
Creates a DateTimeOffset from a DateTime using the local time zone.
public static DateTimeOffset ApplyMoscow(DateTime dt)
result = TimeHelper.ApplyMoscow(dt)
Creates a DateTimeOffset from a DateTime in Russian Standard Time (Moscow).
public static DateTimeOffset ApplyTimeZone(DateTime dt, TimeZoneInfo zone)
result = TimeHelper.ApplyTimeZone(dt, zone)
Creates a DateTimeOffset from a DateTime in the specified time zone.
public static DateTimeOffset ApplyTimeZone(DateTime dt, TimeSpan offset)
result = TimeHelper.ApplyTimeZone(dt, offset)
Creates a DateTimeOffset from a DateTime with the specified offset.
public static DateTimeOffset ApplyUtc(DateTime dt)
result = TimeHelper.ApplyUtc(dt)
Creates a DateTimeOffset from a DateTime using UTC.
public static DateTime ChangeKind(DateTime date, DateTimeKind kind)
result = TimeHelper.ChangeKind(date, kind)
Changes the DateTimeKind of a DateTime.
public static DateTimeOffset Convert(DateTimeOffset dto, TimeZoneInfo zone)
result = TimeHelper.Convert(dto, zone)
Converts the specified DateTimeOffset to the provided time zone.
- dto
- The DateTimeOffset to convert.
- zone
- The target TimeZoneInfo.
Retorna: A DateTimeOffset in the specified time zone.
public static DateTimeOffset ConvertToChina(DateTimeOffset dto)
result = TimeHelper.ConvertToChina(dto)
Converts a DateTimeOffset to China Standard Time zone.
public static DateTimeOffset ConvertToEst(DateTimeOffset dto)
result = TimeHelper.ConvertToEst(dto)
Converts the specified DateTimeOffset to Eastern Standard Time.
- dto
- The DateTimeOffset to convert.
Retorna: A new DateTimeOffset in Eastern Standard Time.
public static DateTimeOffset ConvertToMoscow(DateTimeOffset dto)
result = TimeHelper.ConvertToMoscow(dto)
Converts the specified DateTimeOffset to Moscow Time.
- dto
- The DateTimeOffset to convert.
Retorna: A new DateTimeOffset in Moscow Time.
public static DateTimeOffset ConvertToUtc(DateTimeOffset dto)
result = TimeHelper.ConvertToUtc(dto)
Converts the specified DateTimeOffset to UTC.
- dto
- The DateTimeOffset to convert.
Retorna: A new DateTimeOffset in UTC.
public static int DaysInMonth(DateTime date)
result = TimeHelper.DaysInMonth(date)
Gets the number of days in the month of the given DateTime.
public static DateTime EndOfDay(DateTime dt)
result = TimeHelper.EndOfDay(dt)
Gets the end of the day for the specified DateTime, just before midnight.
public static DateTimeOffset EndOfDay(DateTimeOffset dto)
result = TimeHelper.EndOfDay(dto)
Gets the end of the day for the specified DateTimeOffset, just before midnight.
public static string FromDateTime(DateTime dt, string format, CultureInfo ci)
result = TimeHelper.FromDateTime(dt, format, ci)
Formats a DateTime to a string using the provided format.
public static string FromDateTimeOffset(DateTimeOffset dto, string format, CultureInfo ci)
result = TimeHelper.FromDateTimeOffset(dto, format, ci)
Formats a DateTimeOffset to a string using the provided format.
public static DateTime FromIso8601(string str, IFormatProvider provider)
result = TimeHelper.FromIso8601(str, provider)
Parses the specified string as an ISO8601 date/time.
- str
- The string to parse.
- provider
- An optional IFormatProvider.
Retorna: A DateTime parsed from the string, in UTC.
public static string FromTimeSpan(TimeSpan ts, string format, CultureInfo ci)
result = TimeHelper.FromTimeSpan(ts, format, ci)
Formats a TimeSpan to a string using the provided format.
public static DateTime FromUnix(double time, bool isSeconds)
result = TimeHelper.FromUnix(time, isSeconds)
Creates a DateTime from a Unix time in seconds or milliseconds.
- time
- The Unix time to convert.
- isSeconds
- If set to true, interprets time as seconds; otherwise milliseconds.
Retorna: A DateTime in UTC.
public static DateTime FromUnix(long time, bool isSeconds)
result = TimeHelper.FromUnix(time, isSeconds)
Creates a DateTime from a Unix time in seconds or milliseconds.
- time
- The Unix time to convert.
- isSeconds
- If set to true, interprets time as seconds; otherwise milliseconds.
Retorna: A DateTime in UTC.
public static DateTime FromUnixAuto(long ts)
result = TimeHelper.FromUnixAuto(ts)
Creates a DateTime from a Unix timestamp with auto-detection of unit (seconds, milliseconds, 1/10000 seconds, or microseconds).
- ts
- The Unix timestamp to convert.
Retorna: A DateTime in UTC.
public static DateTime FromUnixMcs(long mcs)
result = TimeHelper.FromUnixMcs(mcs)
Creates a DateTime from Microseconds since GregorianStart.
- mcs
- Microseconds to convert.
Retorna: A DateTime in UTC.
public static DateTime FromUnixMcs(double mcs)
result = TimeHelper.FromUnixMcs(mcs)
Creates a DateTime from Microseconds since GregorianStart.
- mcs
- Microseconds to convert.
Retorna: A DateTime in UTC.
public static int GetIso8601WeekOfYear(DateTime time, CultureInfo ci)
result = TimeHelper.GetIso8601WeekOfYear(time, ci)
Calculates the ISO8601 week number of the specified date.
- time
- The DateTime to evaluate.
- ci
- An optional CultureInfo.
Retorna: The ISO8601 week of the year.
public static LunarPhases GetLunarPhase(DateTime date)
result = TimeHelper.GetLunarPhase(date)
Determines the lunar phase for the specified date.
- date
- The DateTime for which to determine the phase.
Retorna: A LunarPhases value representing the phase of the moon.
public static int GetMicroseconds(TimeSpan ts)
result = TimeHelper.GetMicroseconds(ts)
Gets the microseconds component from a TimeSpan.
public static int GetMicroseconds(DateTime dt)
result = TimeHelper.GetMicroseconds(dt)
Gets the microseconds component from a DateTime.
public static int GetNanoseconds(long ticks)
result = TimeHelper.GetNanoseconds(ticks)
Gets the nanoseconds component from the specified number of ticks.
public static int GetNanoseconds(DateTime dt)
result = TimeHelper.GetNanoseconds(dt)
Gets the nanoseconds component from a DateTime.
public static int GetNanoseconds(TimeSpan ts)
result = TimeHelper.GetNanoseconds(ts)
Gets the nanoseconds component from a TimeSpan.
public static TimeSpan GetUnixDiff(DateTime time)
result = TimeHelper.GetUnixDiff(time)
Gets the difference between the specified DateTime and the GregorianStart.
- time
- The DateTime to evaluate.
Retorna: A TimeSpan representing the difference.
public static bool IsDateOrTime(Type type)
result = TimeHelper.IsDateOrTime(type)
Checks if the given Type is a date or time type.
- type
- The Type to check.
Retorna: True if the type is a date/time or TimeSpan; otherwise false.
public static bool IsDateTime(Type type)
result = TimeHelper.IsDateTime(type)
Checks if the given Type is a date/time type.
- type
- The Type to check.
Retorna: True if the type is a DateTime or DateTimeOffset; otherwise false.
public static bool IsWeekday(DayOfWeek dow)
result = TimeHelper.IsWeekday(dow)
Checks if the given DayOfWeek represents a weekday (Monday through Friday).
- dow
- The DayOfWeek.
Retorna: True if it is a weekday; otherwise false.
public static bool IsWeekday(DateTimeOffset date)
result = TimeHelper.IsWeekday(date)
Checks if the given DateTimeOffset falls on a weekday (Monday through Friday).
- date
- The DateTimeOffset to check.
Retorna: True if it is a weekday; otherwise false.
public static bool IsWeekday(DateTime date)
result = TimeHelper.IsWeekday(date)
Checks if the given DateTime falls on a weekday (Monday through Friday).
- date
- The DateTime to check.
Retorna: True if it is a weekday; otherwise false.
public static bool IsWeekend(DayOfWeek dow)
result = TimeHelper.IsWeekend(dow)
Checks if the given DayOfWeek represents a weekend (Saturday or Sunday).
- dow
- The DayOfWeek.
Retorna: True if it is a weekend; otherwise false.
public static bool IsWeekend(DateTimeOffset date)
result = TimeHelper.IsWeekend(date)
Checks if the given DateTimeOffset falls on a weekend (Saturday or Sunday).
- date
- The DateTimeOffset to check.
Retorna: True if it is a weekend; otherwise false.
public static bool IsWeekend(DateTime date)
result = TimeHelper.IsWeekend(date)
Checks if the given DateTime falls on a weekend (Saturday or Sunday).
- date
- The DateTime to check.
Retorna: True if it is a weekend; otherwise false.
public static long MicrosecondsToTicks(long mcs)
result = TimeHelper.MicrosecondsToTicks(mcs)
Converts microseconds to ticks.
public static long NanosecondsToTicks(long nanoseconds)
result = TimeHelper.NanosecondsToTicks(nanoseconds)
Converts nanoseconds to ticks.
public static IEnumerable<DateTime> Range(DateTime from, DateTime to, TimeSpan interval)
result = TimeHelper.Range(from, to, interval)
Generates a sequence of DateTime values from a start to an end with a given interval.
- from
- Start date.
- to
- End date.
- interval
- The interval between generated dates.
Retorna: An enumerable of dates.
public static DateTime StartOfWeek(DateTime date, DayOfWeek startOfWeek)
result = TimeHelper.StartOfWeek(date, startOfWeek)
Gets the start of the week for the specified DateTime, based on a chosen DayOfWeek.
public static void SyncMarketTime(int timeout)
TimeHelper.SyncMarketTime(timeout)
Synchronizes the current offset by comparing local time with an NTP server.
- timeout
- The synchronization timeout in milliseconds.
public static long TicksToMicroseconds(long ticks)
result = TimeHelper.TicksToMicroseconds(ticks)
Converts ticks to microseconds.
public static long TicksToNanoseconds(long ticks)
result = TimeHelper.TicksToNanoseconds(ticks)
Converts ticks to nanoseconds.
public static DateTime To(DateTime time, TimeZoneInfo source, TimeZoneInfo destination)
result = TimeHelper.To(time, source, destination)
Converts a DateTime between time zones.
public static DateTime ToDateTime(string value, string format, CultureInfo ci)
result = TimeHelper.ToDateTime(value, format, ci)
Converts a string to a DateTime using the provided format.
public static DateTimeOffset ToDateTimeOffset(string value, string format, CultureInfo ci)
result = TimeHelper.ToDateTimeOffset(value, format, ci)
Converts a string to a DateTimeOffset using the provided format.
public static DateTimeOffset ToDateTimeOffset(DateTime date, TimeZoneInfo zone)
result = TimeHelper.ToDateTimeOffset(date, zone)
Converts a DateTime and a time zone to a DateTimeOffset.
public static DateTimeOffset ToDateTimeOffset(DateTime date, TimeSpan offset)
result = TimeHelper.ToDateTimeOffset(date, offset)
Converts a DateTime and a specific offset to a DateTimeOffset.
public static string ToIso8601(DateTime dt, IFormatProvider provider)
result = TimeHelper.ToIso8601(dt, provider)
Formats the specified DateTime as an ISO8601 string.
- dt
- The DateTime to format.
- provider
- An optional IFormatProvider.
Retorna: An ISO8601-formatted string.
public static double ToJulianDate(DateTime date)
result = TimeHelper.ToJulianDate(date)
Converts the specified DateTime to a Julian date.
- date
- The DateTime to convert.
Retorna: A double representing the Julian date.
public static DateTime ToLocalTime(DateTimeOffset dto, TimeZoneInfo zone)
result = TimeHelper.ToLocalTime(dto, zone)
Converts a DateTimeOffset to a DateTime in the specified time zone.
public static long ToNanoseconds(TimeSpan ts)
result = TimeHelper.ToNanoseconds(ts)
Converts a TimeSpan to the total number of nanoseconds.
public static double TotalCenturies(TimeSpan value)
result = TimeHelper.TotalCenturies(value)
Returns total centuries in the specified TimeSpan.
public static double TotalMilleniums(TimeSpan value)
result = TimeHelper.TotalMilleniums(value)
Returns total millenniums in the specified TimeSpan.
public static double TotalMonths(TimeSpan value)
result = TimeHelper.TotalMonths(value)
Returns total months in the specified TimeSpan.
public static double TotalWeeks(TimeSpan value)
result = TimeHelper.TotalWeeks(value)
Returns total weeks in the specified TimeSpan.
public static double TotalYears(TimeSpan value)
result = TimeHelper.TotalYears(value)
Returns total years in the specified TimeSpan.
public static TimeSpan ToTimeSpan(string value, string format, CultureInfo ci)
result = TimeHelper.ToTimeSpan(value, format, ci)
Converts a string to a TimeSpan using the provided format.
public static double ToUnix(DateTimeOffset time, bool isSeconds)
result = TimeHelper.ToUnix(time, isSeconds)
Converts the specified DateTimeOffset to Unix time in seconds or milliseconds.
- time
- The DateTimeOffset to convert.
- isSeconds
- If set to true, returns seconds; otherwise milliseconds.
Retorna: A double representing the Unix time.
public static double ToUnix(DateTime time, bool isSeconds)
result = TimeHelper.ToUnix(time, isSeconds)
Converts the specified DateTime to Unix time in seconds or milliseconds.
- time
- The DateTime to convert.
- isSeconds
- If set to true, returns seconds; otherwise milliseconds.
Retorna: A double representing the Unix time.
public static long ToUnixMcs(DateTime time)
result = TimeHelper.ToUnixMcs(time)
Converts the specified DateTime to Unix Microseconds since GregorianStart.
- time
- The DateTime to convert.
Retorna: A long representing the Unix time in microseconds.
public static DateTimeOffset Truncate(DateTimeOffset time, TimeSpan timeSpan)
result = TimeHelper.Truncate(time, timeSpan)
Truncates the specified DateTimeOffset to the given TimeSpan.
- time
- The DateTimeOffset to truncate.
- timeSpan
- The TimeSpan precision.
Retorna: The truncated DateTimeOffset.
public static DateTime Truncate(DateTime dateTime, TimeSpan timeSpan)
result = TimeHelper.Truncate(dateTime, timeSpan)
Truncates a DateTime to the specified time span precision.
public static DateTimeOffset Truncate(DateTimeOffset time, long precision)
result = TimeHelper.Truncate(time, precision)
Truncates the specified DateTimeOffset to the given precision in ticks.
- time
- The DateTimeOffset to truncate.
- precision
- The precision in ticks.
Retorna: The truncated DateTimeOffset.
public static DateTime Truncate(DateTime time, long precision)
result = TimeHelper.Truncate(time, precision)
Truncates a DateTime to the specified ticks precision.
public static TimeSpan Truncate(TimeSpan dateTime, TimeSpan timeSpan)
result = TimeHelper.Truncate(dateTime, timeSpan)
Truncates a TimeSpan to the specified time span precision.
public static TimeSpan Truncate(TimeSpan time, long precision)
result = TimeHelper.Truncate(time, precision)
Truncates a TimeSpan to the specified ticks precision.
public static DateTime? TryFromUnix(double time, bool isSeconds)
result = TimeHelper.TryFromUnix(time, isSeconds)
Tries to create a DateTime from a Unix time in seconds or milliseconds. Returns null if time is near zero.
- time
- The Unix time to convert.
- isSeconds
- If set to true, interprets time as seconds; otherwise milliseconds.
Retorna: A DateTime in UTC, or null if near zero.
public static DateTime? TryFromUnix(long time, bool isSeconds)
result = TimeHelper.TryFromUnix(time, isSeconds)
Tries to create a DateTime from a Unix time in seconds or milliseconds. Returns null if time is 0.
- time
- The Unix time to convert.
- isSeconds
- If set to true, interprets time as seconds; otherwise milliseconds.
Retorna: A DateTime in UTC, or null if 0.
public static DateTime? TryToDateTime(string value, string format, CultureInfo ci)
result = TimeHelper.TryToDateTime(value, format, ci)
Attempts to convert a string to a DateTime using the provided format.
public static DateTimeOffset? TryToDateTimeOffset(string value, string format, CultureInfo ci)
result = TimeHelper.TryToDateTimeOffset(value, format, ci)
Attempts to convert a string to a DateTimeOffset using the provided format.
public static TimeSpan? TryToTimeSpan(string value, string format, CultureInfo ci)
result = TimeHelper.TryToTimeSpan(value, format, ci)
Attempts to convert a string to a TimeSpan using the provided format.
public static DateTime UtcKind(DateTime date)
result = TimeHelper.UtcKind(date)
Converts a DateTime to UTC kind.
Campos
public static readonly TimeZoneInfo China
value = TimeHelper.China
Represents the China Standard Time zone.
public static readonly TimeZoneInfo Cst
value = TimeHelper.Cst
Represents the Central Standard Time zone.
public static readonly TimeZoneInfo Est
value = TimeHelper.Est
Represents the Eastern Standard Time zone.
public static readonly TimeZoneInfo Fle
value = TimeHelper.Fle
Represents the FLE (Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius) Standard Time zone.
public static readonly TimeZoneInfo Gmt
value = TimeHelper.Gmt
Represents the GMT Standard Time zone.
public static readonly DateTime GregorianStart
value = TimeHelper.GregorianStart
Represents a reference start date (1/1/1970, UTC).
public static readonly TimeZoneInfo Korea
value = TimeHelper.Korea
Represents the Korea Standard Time zone.
public static readonly TimeSpan LessOneDay
value = TimeHelper.LessOneDay
A TimeSpan that is one tick less than a day.
public static readonly TimeSpan Minute1
value = TimeHelper.Minute1
A TimeSpan of one minute.
public static readonly TimeSpan Minute10
value = TimeHelper.Minute10
A TimeSpan of ten minutes.
public static readonly TimeSpan Minute15
value = TimeHelper.Minute15
A TimeSpan of fifteen minutes.
public static readonly TimeSpan Minute5
value = TimeHelper.Minute5
A TimeSpan of five minutes.
public static readonly TimeSpan Month
value = TimeHelper.Month
A TimeSpan of one month (30 days).
public static readonly TimeZoneInfo Moscow
value = TimeHelper.Moscow
Represents the Russian Standard Time zone (Moscow).
public const long NanosecondsPerTick
value = TimeHelper.NanosecondsPerTick
Represents the number of nanoseconds in 1 tick.
public const long TicksPerCentury
value = TimeHelper.TicksPerCentury
Represents the number of ticks in 1 century.
public const long TicksPerMicrosecond
value = TimeHelper.TicksPerMicrosecond
Represents the number of ticks in 1 microsecond.
public const long TicksPerMillenium
value = TimeHelper.TicksPerMillenium
Represents the number of ticks in 1 millenium.
public const long TicksPerMonth
value = TimeHelper.TicksPerMonth
Represents the number of ticks in 1 month.
public const double TicksPerNanosecond
value = TimeHelper.TicksPerNanosecond
Represents the number of ticks in 1 nanosecond.
public const long TicksPerWeek
value = TimeHelper.TicksPerWeek
Represents the number of ticks in 1 week.
public const long TicksPerYear
value = TimeHelper.TicksPerYear
Represents the number of ticks in 1 year.
public static readonly TimeZoneInfo Tokyo
value = TimeHelper.Tokyo
Represents the Tokyo Standard Time zone.
public static readonly TimeZoneInfo Tunisia
value = TimeHelper.Tunisia
Represents the West Central Africa Standard Time zone (Tunisia).
public static readonly TimeSpan Year
value = TimeHelper.Year
A TimeSpan of one year (365 days).