2010-07-22 8 views

Antwort

6

Wenn Sie interessiert hier bei der Umsetzung ist in etwa wie Windows-Zeitstempel (auch bekannt als ticks) berechnet werden können:

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); 
} 
+0

ich dies in PHP zu schreiben, bin zu wollen. – David

+1

@David: PHP 'Zeit' und' gmmktime' Funktionen gibt Unix-Zeitstempel zurück. Wenn Sie möchten, können Sie es in Ticks konvertieren als '621355968000000000 + unixTimestamp * 10000000' – Regent

+0

Was ist 0xd6bf94d5e5L, 0xc92a69c000L und 922337203685L? –

1

Normalerweise ist es die verstrichene Zeit seit einem bestimmten Datum. Im Fall der Unix-Zeit ist dies die Zeit seit dem 1. Januar 1970 in Sekunden.

6

Hier ist ein Beispiel dafür, wie Unix-Zeitstempel aus dem wikipedia article berechnet:

Die Zahl Unix Zeit ist Null bei der Unix Epoche, und erhöht sich um genau 86 400 pro Tag seit Beginn der Epoche. So 2004-09-16T00: 00: 00Z, 12 677 Tage nach der Epoche, wird durch die Unix-Zeit-Nummer 12 677 × 86 400 = 1 095 292 800 dargestellt. Dies kann aus der Epoche auch rückwärts verlängert werden unter Verwendung von negativen Zahlen; so 1957-10-04T00: 00: 00Z, 4 472 Tage vor Beginn der Epoche wird durch die Unix-Zeit-Zahl dargestellt -4 472 × 86 400 = -386 380 800.

Verwandte Themen