Timestamp 计算时间戳的公式是什么?

Timestamp 计算时间戳的公式是什么?,timestamp,Timestamp,这只是一个随机的问题,但有人知道如何计算时间戳的公式吗?我猜它必须考虑每个月和多少天,闰年等。 谢谢通常是从特定日期起经过的时间。对于unix时间,它是自1970年1月1日以来经过的时间(以秒为单位)。下面是一个unix时间戳如何从以下时间计算的示例: 此时Unix时间编号为零 Unix时代,并精确地增加 从新纪元开始每天86400美元。因此 2004-09-16T00:00:00Z,12677天 在纪元之后,由 Unix时间编号12 677×86 400=1 095 292 800. 这是可以

这只是一个随机的问题,但有人知道如何计算时间戳的公式吗?我猜它必须考虑每个月和多少天,闰年等。
谢谢

通常是从特定日期起经过的时间。对于unix时间,它是自1970年1月1日以来经过的时间(以秒为单位)。

下面是一个unix时间戳如何从以下时间计算的示例:

此时Unix时间编号为零 Unix时代,并精确地增加 从新纪元开始每天86400美元。因此 2004-09-16T00:00:00Z,12677天 在纪元之后,由 Unix时间编号12 677×86 400=1 095 292 800. 这是可以扩展的 从新纪元开始,使用 负数;因此 1957-10-04T00:00:00Z,4472天 在纪元之前,表示为 Unix时间编号−4 472 × 86 400 = -386380800


如果您对实现感兴趣,这里大致介绍如何计算Windows时间戳(也称为):

publicstaticint64 GetTimeStamp(
整年,整月,整日,
整数小时、整数分钟、整数秒、整数毫秒)
{
Int64 timestamp=DateToTicks(年、月、日)
+总时间(小时、分钟、秒);
返回时间戳+毫秒*滴答声毫秒;
}
静态只读int[]DaysToMonth365=
新的int[]{0,31,59,90,120,151,181,212,243,273,304,334,365};
静态只读int[]DaysToMonth366=
新的int[]{0,31,60,91,121,152,182,213,244,274,305,335,366};
常数长滴答秒=滴答毫秒*1000L;
常量长滴答数毫秒=10000L;
公共静态布尔IsLeapYear(国际年)
{
如果((年份<1)| |(年份>9999))
抛出新的ArgumentOutOfRangeException(“年”、“坏年”);
如果((第%4年)!=0)
返回false;
如果((年份%100)==0)
回报率((年份%400)==0);
返回true;
}
私有静态长DateToTicks(整数年、整数月、整数天)
{
如果((年>=1)和&(年=1)和&(月=1)和&(天0xd6bf94d5e5L)| |(总秒数<-922337203685L))
抛出新ArgumentOutOfRangeException();
返回(总秒数*滴答秒数);
}

我想用PHP写这篇文章。@David:PHP的
time
gmmktime
函数返回Unix时间戳。如果需要,可以将其转换为滴答声,如
62135596800000000+unixTimestamp*10000000
0xd6bf94d5e5L、0xc92a69c000L和922337203685L是多少?@FaizanRupani这是您可以使用的最大秒数/天数用64位整数表示。看看原始源代码:0xc92a69c000L是十六进制。24小时*60分钟*60秒是一天中的秒,86400。如果你将十六进制转换为十进制(可能在搜索引擎中找到在线工具),你将得到864000000000,看起来是它的倍数。
public static Int64 GetTimeStamp(
                        int year, int month, int day,
                        int hour, int minute, int second, int milliseconds)
{
    Int64 timestamp = DateToTicks(year, month, day)
        + TimeToTicks(hour, minute, second);

    return timestamp + milliseconds * TicksInMillisecond;
}

static readonly int[] DaysToMonth365 =
    new int[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
static readonly int[] DaysToMonth366 =
    new int[] { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
const long TicksInSecond = TicksInMillisecond * 1000L;
const long TicksInMillisecond = 10000L;

public static bool IsLeapYear(int year)
{
    if ((year < 1) || (year > 9999))
        throw new ArgumentOutOfRangeException("year", "Bad year.");

    if ((year % 4) != 0)
        return false;

    if ((year % 100) == 0)
        return ((year % 400) == 0);

    return true;
}

private static long DateToTicks(int year, int month, int day)
{
    if (((year >= 1) && (year <= 9999)) && ((month >= 1) && (month <= 12)))
    {
        int[] daysToMonth = IsLeapYear(year) ? DaysToMonth366 : DaysToMonth365;
        if ((day >= 1) && (day <= (daysToMonth[month] - daysToMonth[month - 1])))
        {
            int previousYear = year - 1;
            int daysInPreviousYears = ((((previousYear * 365) + (previousYear / 4)) - (previousYear / 100)) + (previousYear / 400));

            int totalDays = ((daysInPreviousYears + daysToMonth[month - 1]) + day) - 1;
            return (totalDays * 0xc92a69c000L);
        }
    }
    throw new ArgumentOutOfRangeException();
}

private static long TimeToTicks(int hour, int minute, int second)
{
    long totalSeconds = ((hour * 3600L) + (minute * 60L)) + second;
    if ((totalSeconds > 0xd6bf94d5e5L) || (totalSeconds < -922337203685L))
        throw new ArgumentOutOfRangeException();

    return (totalSeconds * TicksInSecond);
}