/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc.
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
namespace UVtools.Core.Extensions;
public static class DateTimeExtensions
{
///
/// Gets the Unix timestamp since Jan 1, 1970 UTC
///
public static TimeSpan Timestamp => DateTime.UtcNow.Subtract(DateTime.UnixEpoch);
///
/// Gets the Unix timestamp in seconds since Jan 1, 1970 UTC
///
public static double TimestampSeconds => Timestamp.TotalSeconds;
///
/// Gets the Unix minutes in seconds since Jan 1, 1970 UTC
///
public static double TimestampMinutes => Timestamp.TotalMinutes;
///
/// Gets the from a unix timestamp in seconds
///
///
///
public static DateTime GetDateTimeFromTimestampSeconds(double seconds)
{
return DateTime.UnixEpoch.AddSeconds(seconds);
}
///
/// Gets the from a unix timestamp in minutes
///
///
///
public static DateTime GetDateTimeFromTimestampMinutes(double minutes)
{
return DateTime.UnixEpoch.AddMinutes(minutes);
}
///
/// Calculates the age in years of the current System.DateTime object today.
///
/// The date of birth
/// Age in years today. 0 is returned for a future date of birth.
public static int Age(this DateTime birthDate)
{
return Age(birthDate, DateTime.Today);
}
///
/// Calculates the age in years of the current System.DateTime object on a later date.
///
/// The date of birth
/// The date on which to calculate the age.
/// Age in years on a later day. 0 is returned as minimum.
public static int Age(this DateTime birthDate, DateTime laterDate)
{
var age = laterDate.Year - birthDate.Year;
if (age > 0)
{
age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age));
}
else
{
age = 0;
}
return age;
}
}