GetTimeStamp()

From Hack Wars Wiki

Jump to: navigation, search

This function returns a completely customizable timestamp. You can configure your time zone, 24/12-hour-system and the template string, into which the function parses the date and time data.

The template key words and what they stand for (I think it's self-explanatory, but just to be on the safe side...):

-
    -
  • DAY: day of the month, with leading zero
  • -
  • MONTH: abbreviation of the month's name, like in the standard date function
  • -
  • MONTHNR: the month's number in the year, with leading zero
  • -
  • YEAR: only the last two places of the year, like 09 of 2009
  • -
  • FULLYEAR: all four places of the year
  • -
  • TIME: time, hh:mm:ss (AM/PM)
  • -

-

Code

string getTimeStamp() {
//	your time difference towards UTC (Greenwitch Mean Time), 
//	should be between -11 and 12
//	e.g. 1 for central europe, -5 for eastern USA etc.
 
	int UTCoffset = 1; 
 
//	true means 24-hour-system
//	false means standard 12-hour-system
 
	boolean twentyFourHours = true; 
 
//	structure the key words DAY, MONTH or MONTHNR, YEAR or FULLYEAR and TIME
//	using brackets, dots, commas, hyphens, spaces etc. 
//	also think of the end of the timestamp. 
//	should it always be followed by a space, or a hyphen, or nothing?
//	e.g. American date format: "[MONTHNR/DAY/YEAR - TIME] "
//	or my German format: "[DAY. MONTH FULLYEAR - TIME Uhr] "
 
	string format = "[DAY. MONTH FULLYEAR - TIME] "; 
 
//#############################################################################################
 
//	just in case the server's UTC offset ever changes... currently its -5, Eastern Standard Time
	int serverUTCoffset = -5; 
 
	string monthNames[]; 
	monthNames[1] = "Jan"; 
	monthNames[2] = "Feb"; 
	monthNames[3] = "Mar"; 
	monthNames[4] = "Apr"; 
	monthNames[5] = "May"; 
	monthNames[6] = "Jun"; 
	monthNames[7] = "Jul"; 
	monthNames[8] = "Aug"; 
	monthNames[9] = "Sep"; 
	monthNames[10] = "Oct"; 
	monthNames[11] = "Nov"; 
	monthNames[12] = "Dec"; 
 
	string date[] = split(getDate(), "-"); 
	int day = parseInt(date[0]); 
	int month = 0; 
	for (int i=1; i<=12; i++)
		if (equal(date[1], monthNames[i]))
			month = i; 
	int year = parseInt(date[2]); 
	string getTime = getTime(); 
	string halfOfTheDay = substr(getTime, strlen(getTime)-2, strlen(getTime)); // AM/PM
	string time[] = split(substr(getTime, 0, strlen(getTime)-3), ":"); 
	int hour = parseInt(time[0])%12; 
	if (equal(halfOfTheDay, "PM"))
		hour += 12; 
	halfOfTheDay = ""; 
 
	hour = hour-serverUTCoffset+UTCoffset; 
	//now check, if this has changed the day (month, year)
	if (hour < 0) {
		hour += 24; 
		day--; 
		if (day <= 0) {
			month--; 
			if (month <= 0) {
				year--; 
				month =12; 
			}
			if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
				day = 31; 
			if (month == 4 || month == 6 || month == 9 || month == 11)
				day = 30; 
			if (month == 2 && year%4 == 0)
				day = 29; 
			if (month == 2 && year%4 != 0)
				day = 28; 
		}
	}
	if (hour > 23) {
		hour -= 24; 
		day++; 
		if (day > 31 || ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) || (month == 2 && year%4 == 0 && day > 29) || (month == 2 && year%4 != 0 && day > 28)) {
			day = 1; 
			month++; 
		}
		if (month > 12) {
			month = 1; 
			year++; 
		}
	}
	if (! twentyFourHours) {
		if (hour >= 12)
			halfOfTheDay = " PM"; 
		else
			halfOfTheDay = " AM"; 
		hour = hour%12; 
		if (hour == 0)
			hour = 12; 
	}
	time[0] = (string) hour; 
	if (strlen(time[0]) == 1)
		time[0] = "0" + time[0]; 
 
	format = replaceAll(format, "TIME", join(time[], ":") + halfOfTheDay); 
 
	date[0] = (string) day; 
	if (strlen(date[0]) == 1)
		date[0] = "0" + date[0]; 
	format = replaceAll(format, "DAY", date[0]); 
 
	date[1] = (string) month; 
	if (strlen(date[1]) == 1)
		date[1] = "0" + date[1]; 
	format = replaceAll(format, "MONTHNR", date[1]); 
	format = replaceAll(format, "MONTH", monthNames[month]); 
 
	date[2] = (string) year; 
	format = replaceAll(format, "FULLYEAR", date[2]); 
	format = replaceAll(format, "YEAR", substr(date[2], 2, 4)); 
 
	return format; 
}


Alternative Code

string getTimeStamp(int UTCoffset, boolean twentyFourHours, string format) {
 
//copy anything from below the #-line in the original and paste it here...
 
}


--Merlin 15:55, 30 July 2009 (UTC)

Personal tools