Skrev en datumklass i C# inspirerad av php motsvarande funktion, du kan ju kolla om du hittar något av värde...
/*
* Date class based on unix Epoch time stamp, second from 00:00:00 1/1 1970
*
* beta 1
*
** setDate(long second, long minute, long hour, long day, long month, long year)
* Sets Date variables can be overloaded, e.g. you can set 25 hours or -23 minutes //month don't work as negativ yet
* year: both full (4 digits) and short (2 digits) supported, years > 100 counts as full, years < 70 counts as years + 2000, years >= 70 and <= 100 counts as years + 1900
*
** addDate(long second, long minute, long hour, long day, long month, long year)
* Add Date value, same as above...
*
** setUDate(long unixStamp)
* Sets the unix time stamp directly
*
** addUDate(long unixStamp)
* Adds seconds to time stamp value
*
** setTimeZone(float timeZoneHour) ***Används ej i version
* Sets time zone as float of hours from +0000
*
** setTimeZoneSec(long timeZone) ***Används ej i version
* Sets time zone as int of seconds from +0
*
** string getDate(string referens)
* return string of wanted type, free formating, unknown chars passes through to string (very similar to the date() function in php)
*
* d : Day of the month, 2 digits with leading zeros, 01 to 31
* j : Day of the month, without leading zeros, 1 to 31
* S : English ordinal suffix for the day of the month, 2 characters, st for 1, nd for 2 and so on (works well with j)
* D : Day of the week, 3 letters, Mon to Sun
* l : Day of the week, full text, Monday to Sunday
* w : Day of the week, number, 0 (monday) to 6 (sunday)
* z : Day of the year, number, 0 (1 jan) to 364 (365 if leap)
* W : Week of the year, number, starting on 1
* F : Month, full text, January to December
* M : Month, 3 letters, Jan to Dec
* m : Month, 2 digits with leading zeros, 01 to 12
* n : Month, without leading zeros, 1 to 12
* t : Days in current month, 28 to 31
* L : returns 1 if leap year, else 0
* Y : Year, full 4 digits, e.g. 1999 / 2005
* y : Year, short 2 digits, e.g. 99 / 05
* o : Year as "Y", but day which week is in past year count as that year e.g. the date 1 jan 2006 will return 2005 with "o"
* a : Lowercase am or pm
* A : Uppercase AM or PM
* g : 12 hour clock, without leading zeros, midnight 0, noon 12
* h : 12 hour clock, with leading zeros, midnight 00, noon 12
* G : 24 hour clock, without leading zeros, 0 to 23
* H : 24 hour clock, with leading zeros, 00 to 23
* i : Minutes with leading zeros, 00 to 59
* s : Seconds, with leading zeros, 00 to 59
* e : //none
* I : //none
* O : Difference to Greenwich time (GMT) in hours, e.g. +0200 or -0300
* Z : Timezone offset in seconds. e.g. -21600 or 10800
* U : Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
* { : Char after "{" passes direct to out string, e.g. {YY returns Y2006 (if it's 2006)
*
*/
class Date{
long unixTime;
long timeZoneOffset = 0;
int[] daysInMonth = new int[12] {31,28,31,30,31,30,31,31,30,31,30,31};
long getYear()
{
return this.unixTime / 31557600;
}
long getDaysFromEpoch()
{
return this.unixTime / 86400;
}
long getHour()
{
return (this.unixTime / 3600) % 24;
}
long getMinute()
{
return (this.unixTime / 60) % 60;
}
long getSecond()
{
return this.unixTime % 60;
}
long getWeekDay()
{
return (this.getDaysFromEpoch() + 3) % 7;
}
long getDayInYear()
{
return (this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400)) / 86400;
}
long getWeek()
{
if (this.getDayInYear() - this.getWeekDay() < 0){ //if week from last year comes in to this year.
return ((((this.unixTime - 604800) - (((this.unixTime - 604800) / 31557600) * 31536000 + ((((this.unixTime - 604800) / 31557600) + 1) / 4) * 86400)) / 86400) - ((((this.unixTime - 604800) / 86400) + 3) % 7)) / 7 + 2;
}else{
return (this.getDayInYear() - this.getWeekDay()) / 7 + 1;
}
}
long getMonth()
{
long tempUnixSecond = this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400);
int month;
if((this.getYear() + 2) % 4 == 0){
this.daysInMonth[1] = 29;
}
for (month = 0; tempUnixSecond >= this.daysInMonth[month] * 86400; month++)
{
tempUnixSecond -= this.daysInMonth[month] * 86400;
}
this.daysInMonth[1] = 28;
return month + 1;
}
long getDay()
{
long tempUnixSecond = this.unixTime - (this.getYear() * 31536000 + ((this.getYear() + 1) / 4) * 86400);
if (this.getMonth() >= 2 && (this.getYear() + 2) % 4 == 0)
{
tempUnixSecond -= 86400;
}
for(long n = this.getMonth()-2; n >= 0; n--){
tempUnixSecond -= this.daysInMonth[n] * 86400;
}
return tempUnixSecond / 86400 + 1;
}
public string getDate(string referens){
string returnString = "";
for (int n = 0;n < referens.Length; n++){
switch (referens[n])
{
case 'j':
returnString += this.getDay();
break;
case 'd':
long tempDay = this.getDay();
if (tempDay < 10){
returnString += "0";
}
returnString += tempDay;
break;
case 'S':
switch (this.getDay())
{
case 1:
returnString += "st";
break;
case 2:
returnString += "nd";
break;
case 3:
returnString += "rd";
break;
default:
returnString += "th";
break;
}
break;
case 'D':
switch (this.getWeekDay()){
case 0:
returnString += "Mon";
break;
case 1:
returnString += "Tue";
break;
case 2:
returnString += "Wed";
break;
case 3:
returnString += "Thu";
break;
case 4:
returnString += "Fri";
break;
case 5:
returnString += "Sat";
break;
case 6:
returnString += "Sun";
break;
}
break;
case 'l':
switch (this.getWeekDay()){
case 0:
returnString += "Monday";
break;
case 1:
returnString += "Tuesday";
break;
case 2:
returnString += "Wednesday";
break;
case 3:
returnString += "Thursday";
break;
case 4:
returnString += "Friday";
break;
case 5:
returnString += "Saturday";
break;
case 6:
returnString += "Sunday";
break;
}
break;
case 'w':
returnString += this.getWeekDay();
break;
case 'z':
returnString += this.getDayInYear();
break;
case 'W':
returnString += this.getWeek();
break;
case 'F':
switch(this.getMonth()){
case 1:
returnString += "January";
break;
case 2:
returnString += "February";
break;
case 3:
returnString += "March";
break;
case 4:
returnString += "April";
break;
case 5:
returnString += "May";
break;
case 6:
returnString += "June";
break;
case 7:
returnString += "July";
break;
case 8:
returnString += "August";
break;
case 9:
returnString += "September";
break;
case 10:
returnString += "October";
break;
case 11:
returnString += "November";
break;
case 12:
returnString += "December";
break;
}
break;
case 'M':
switch (this.getMonth())
{
case 1:
returnString += "Jan";
break;
case 2:
returnString += "Feb";
break;
case 3:
returnString += "Mar";
break;
case 4:
returnString += "Apr";
break;
case 5:
returnString += "May";
break;
case 6:
returnString += "Jun";
break;
case 7:
returnString += "Jul";
break;
case 8:
returnString += "Aug";
break;
case 9:
returnString += "Sep";
break;
case 10:
returnString += "Oct";
break;
case 11:
returnString += "Nov";
break;
case 12:
returnString += "Dec";
break;
}
break;
case 'm':
long tempMonth = this.getMonth();
if (tempMonth < 10){
returnString += "0";
}
returnString += tempMonth;
break;
case 'n':
returnString += this.getMonth();
break;
case 't':
if (this.getMonth() == 1 && (this.getYear() + 2) % 4 == 0)
{
returnString += "29";
}else{
returnString += this.daysInMonth[this.getMonth()-1];
}
break;
case 'L':
if ((this.getYear() + 2) % 4 == 0)
{
returnString += "1";
}else{
returnString += "0";
}
break;
case 'Y':
returnString += (this.getYear() + 1970);
break;
case 'y':
returnString += (this.getYear() + 70);
break;
case 'o':
if (this.getDayInYear() - this.getWeekDay() < 0)
{
returnString += (this.getYear() + 1969);
}
else
{
returnString += (this.getYear() + 1970);
}
break;
case 'a':
if(this.getHour() < 12){
returnString += "am";
}else{
returnString += "pm";
}
break;
case 'A':
if (this.getHour() < 12){
returnString += "AM";
}else{
returnString += "PM";
}
break;
case 'g':
long tempHour = this.getHour();
if (tempHour >= 13)
{
tempHour -= 12;
}
returnString += tempHour;
break;
case 'h':
long tempHour2 = this.getHour();
if (tempHour2 >= 13)
{
tempHour2 -= 12;
}
if (tempHour2 < 10)
{
returnString += "0";
}
returnString += tempHour2;
break;
case 'G':
returnString += this.getHour();
break;
case 'H':
long tempHour3 = this.getHour();
if (tempHour3 < 10)
{
returnString += "0";
}
returnString += tempHour3;
break;
case 'i':
long tempMin = this.getMinute();
if (tempMin < 10)
{
returnString += "0";
}
returnString += tempMin;
break;
case 's':
long tempSec = this.getSecond();
if (tempSec < 10)
{
returnString += "0";
}
returnString += tempSec;
break;
case 'e':
//none
break;
case 'I':
//none
break;
case 'O':
if (timeZoneOffset < 0)
{
returnString += "-";
}else{
returnString += "+";
}
long tempTimeZoneHour = timeZoneOffset / 3600;
long tempTimeZoneMin = (timeZoneOffset - tempTimeZoneHour*3600)/60;
if (tempTimeZoneHour < 10)
{
returnString += "0";
}
returnString += tempTimeZoneHour;
if (tempTimeZoneMin < 10)
{
returnString += "0";
}
returnString += tempTimeZoneMin;
break;
case 'Z':
returnString += timeZoneOffset;
break;
case 'U':
returnString += this.unixTime;
break;
case '{':
if (n + 1 < referens.Length)
{
returnString += referens[n+1];
n++;
}
break;
default:
returnString += referens[n];
break;
}
}
return returnString;
}
public void setDate(long second, long minute, long hour, long day, long month, long year){
//changes year to years after 1970.
if (year >= 100){ //full year, e.g. 1984
year -= 1970;
}else if (year < 70){ // short year >= 2000 e.g. 06
year += 30;
}else{ //short year >= 1970, < 2000 e.g. 98
year -= 70;
}
month--;
day--;
year += month / 12;
month %= 12;
day += year * 365;
day += (year * 12 + month + 22) / 48; //leap year
for (int n = 0; n < month; n++){
day += this.daysInMonth[n];
}
this.unixTime = second + minute * 60 + hour * 3600 + day * 86400;
}
public void addDate(long second, long minute, long hour, long day, long month, long year){
year += this.getYear()+1970;
month += this.getMonth();
day += this.getDay();
hour += this.getHour();
minute += this.getMinute();
second += this.getSecond();
this.setDate(second, minute, hour, day, month, year);
}
public void setUDate(long unixStamp){
unixTime = unixStamp;
}
public void addUDate(long unixStamp){
unixTime += unixStamp;
}
public void setTimeZone(float timeZoneHour){
timeZoneOffset = (long)(timeZoneHour * 3600);
}
public void setTimeZoneSec(long timeZone){
timeZoneOffset = timeZone;
}
}
Edit: blev väldigt långt, men men...