Unix Time / Member: systime

Here you can post threads requesting help on the official POL Ultima Online Emulator Core 096.
Note: Core 096 is no longer officially supported.
Post Reply
Beaud
Novice Poster
Posts: 41
Joined: Tue Feb 14, 2006 2:06 pm

Unix Time / Member: systime

Post by Beaud »

Anyone has written a function to convert Unix Time to our sweet world wide used time format? If any one know the formula I might try.. Or is there any other way to get a clear time/date format?..
blah
New User
Posts: 12
Joined: Thu Feb 02, 2006 12:05 pm

Post by blah »

Check yahoo groups for 'datetime' package.
It does what You want, but i'm not sure if i found it there, or somewhere else.
But it was written for 095.
Danielle
Grandmaster Poster
Posts: 104
Joined: Tue Feb 07, 2006 3:32 pm

Post by Danielle »

Sure have...

http://www.winterboard.com/clock.inc

Code: Select all

///////////////////////////////////////////////////////////////////////////////
//
//	Author: Danielle Elora, webmaster@winterboard.com
//	Nightscape Shard, http://www.nightscapeonline.com
//
//	Description: Functions for calculating and returning in-game and real-world time/day/month/year values.
//
//	CHANGE LOG:
//			**Based on Jouni Airaksinen's (Mintus@Codefield.com) work.
//			**Initial release date is unknown**
//			**Previous version is unknown; restarting at v. 1.0.0**
//		Version 1.0.0 -- August 26, 2005 -- Danielle Elora
//			Initial Release.
//
//////////////////////////////////////////////////////////////////////////////

use uo;
use basic;
use cfgfile;

// CHANGE TO THE TIMEZONE YOU WISH REAL-WORLD TIME TO BE RETURNED IN.
// Currently this only effects hour and meridiem functions
const TIMEZONE := -4;

//////////////////////////////////////////////////////////////////////
//    NAMING CONVENTION: TimeContext_ReturnType_Returned
//      TimeContext:
//      - gtime = The functions work of the in-game clock (number of seconds since the server booted up)
//      - time  = The functions work of the real-world clock (number of second since January 1, 1970, 12:00:00am)
//			ReturnType:
//      - n = returned value is an integer
//      - s = returned value is a string
//			Returned:
//			- Brief one, two or three word description of what the function is getting/returning.
//
//		TIP:
//			Want to keep this naming format but do not have time to update the calling scripts? Create
//			some temporary alias functions for each renamed function to use in the meantime.
//			
//			function GetTime()
//				return gtime_n_time();
//			endfunction
//
//			Just be sure this IS temporary, because this is a waste of resources and processing power.
//
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
//                                                                  //
//      THE FOLLOWING FUNCTIONS WORK OFF THE IN GAME TIME/DATE.     //
//                                                                  //
//////////////////////////////////////////////////////////////////////

// Was: GetTime()
function gtime_n_time()
	var length := GetConfigInt(FindConfigElem(ReadConfigFile("::days"), "WholeDay"),  "Length");
	return (ReadGameClock() - ((ReadGameClock() / (60 * length)) * (60 * length)));
endfunction

// Was: GetTransition()
function gtime_n_transition()
	return GetConfigInt(FindConfigElem(ReadConfigFile("::days"), "Transition"), "Length");
endfunction

// Was: GetDayLength()
function gtime_n_daylength()
	return GetConfigInt(FindConfigElem(ReadConfigFile("::days"), "WholeDay"), "Length");
endfunction

// Was: GetTimeofDay()
function gtime_n_timeofday()
	var hour := CInt((24.0 / (((gtime_n_daylength() * 60) * 1.0) / gtime_n_time())));
	if (hour > 12)
		hour := hour - 12;
	elseif (hour < 1)
		hour := 12;
	endif
	return padleft(hour, 2) + ":" + padleft(CInt(((24.0 / (((gtime_n_daylength() * 60) * 1.0) / gtime_n_time())) - CInt((24.0 / (((gtime_n_daylength() * 60) * 1.0) / gtime_n_time())))) * 60), 2);
endfunction

// Was: GetHourOfDay()
function gtime_n_hour()
	var hour := CInt((24.0 / (((gtime_n_daylength() * 60) * 1.0) / gtime_n_time())));
	if (hour > 12)
		hour := hour - 12;
	endif
	if (hour < 1)
		hour := 12;
	endif
	return hour;
endfunction

//////////////////////////////////////////////////////////////////////
//                                                                  //
//    THE FOLLOWING FUNCTIONS WORK OFF THE REAL WORLD TIME/DATE.    //
//                                                                  //
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////
//  OUTLINE OF FUNCTIONS
//
//    FUNCTION                    ACTION
//    ------------------------------------------------------------------------------------
//    time_n_month()              Returns the current month as a number
//                                - i.e. "01" - "12"
//    ------------------------------------------------------------------------------------
//    time_s_month()              Returns the current month as a string
//                                - i.e. "January" - "December"
//    ------------------------------------------------------------------------------------
//    time_n_dayofmonth()         Returns the current day of the month
//                                - i.e. "01" - "31"
//    ------------------------------------------------------------------------------------
//    time_n_year()               Returns the current year
//                                - i.e. "2005"
//                                - Use '(returned_string - "20")' in your script for a
//                                  shorthand year-value.
//    ------------------------------------------------------------------------------------
//    time_n_hour()               Returns the current hour.
//                                - i.e. "1" - "12"
//    ------------------------------------------------------------------------------------
//    time_n_hourpadded()         Returns the current hour.
//                                - i.e. "01" - "12"
//    ------------------------------------------------------------------------------------
//    time_n_24hour()             Returns the current hour in "military" format.
//                                - i.e. "01" - "24"
//    ------------------------------------------------------------------------------------
//    time_n_min()                Returns the current minute in the hour.
//                                - i.e. "1" - "60"
//    ------------------------------------------------------------------------------------
//    time_n_minpadded()          Returns the current minute in the hour.
//                                - i.e. "01" - "60"
//    ------------------------------------------------------------------------------------
//    time_n_sec()                Returns the current second in the minute.
//                                - i.e. "1" - "60"
//    ------------------------------------------------------------------------------------
//    time_n_secpadded()          Returns the current second in the minute.
//                                - i.e. "01" - "60"
//    ------------------------------------------------------------------------------------
//    time_s_meridiem()           Returns current meridiem as a string.
//                                - i.e. "am" - "pm"
//    ------------------------------------------------------------------------------------
//    time_s_formatteddatetime()  Returns a preformated date and time string in EST.
//                                - i.e. "month dayofmonth, year -- hour:minutes meridiem"
//                                - does not take into account "daylight saving" time
//
//		Additionally ALL functions will accept a raw UNIX-styled timestamp value and the returned
//		integer or string will be based off the passed timestamp rather than the current timestamp.
//
//		This allows you to store a "PolCore().systime" value and then use it later to display for 
//		example the day of the week that timestamp was created/stored.
//
//////////////////////////////////////////////////////////////////////////////////////////


///////////////////
//  Returns the current day of the week as a number (ie. Sunday = "1")
///////////////////
function time_n_dayofweek(time := 0)
	if (time != 0)
		return ((CInt(time / 86400) + 719163) % 7) + 1;
	else
		return ((CInt(PolCore().systime / 86400) + 719163) % 7) + 1;
	endif
endfunction

///////////////////
//  Returns the current day of the week as a string (ie. "Sunday" - "Saturday")
///////////////////
function time_s_dayofweek(time := 0)
	var week_names := array{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	if (time != 0)
		return week_names[((CInt(time / 86400) + 719163) % 7) + 1];
	else
		return week_names[((CInt(PolCore().systime / 86400) + 719163) % 7) + 1];
	endif
endfunction

///////////////////
//  Returns the current month as a number (i.e. "01")
///////////////////
function time_n_month(time := 0)
	var month_days := array{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	var days;
	if (time != 0)
		days := CInt(time / 86400);
	else
		days := CInt((PolCore().systime - 1104465600) / 86400);
	endif
	var current_year_counter := 0;
	var current_month_counter := 1;
	var done := 0;
	var days_in_month := 0;
	repeat
		if (current_month_counter == 2)
			if ((current_year_counter % 4) == 0)
				days_in_month := 29;
			else
				days_in_month := 28;
			endif
		else
			days_in_month := month_days[current_month_counter];
		endif
		if (days_in_month <= days)
			days := days - days_in_month;
			current_month_counter := current_month_counter + 1;
			if (current_month_counter > 12)
				current_month_counter := 1;
				current_year_counter := current_year_counter + 1;
			endif
		else
			done := 1;
		endif
	until (done);
	return padleft(current_month_counter, 2);
endfunction

///////////////////
//  Returns the current month as a string (i.e. "January")
///////////////////
function time_s_month(time := 0)
	var month_days := array{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	var month_names := array{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	var days;
	if (time != 0)
		days := CInt(time / 86400);
	else
		days := CInt((PolCore().systime - 1104465600) / 86400);
	endif
	var current_year_counter := 0;
	var current_month_counter := 1;
	var done := 0;
	var days_in_month := 0;
	repeat
		if (current_month_counter == 2)
			if ((current_year_counter % 4) == 0)
				days_in_month := 29;
			else
				days_in_month := 28;
			endif
		else
			days_in_month := month_days[current_month_counter];
		endif
		if (days_in_month <= days)
			days := days - days_in_month;
			current_month_counter := current_month_counter + 1;
			if (current_month_counter > 12)
				current_month_counter := 1;
				current_year_counter := current_year_counter + 1;
			endif
		else
			done := 1;
		endif
	until (done);
	return month_names[current_month_counter];
endfunction

///////////////////
//  Returns the current day of the month (i.e. 01 - 31)
///////////////////
function time_n_dayofmonth(time := 0)
	var days;
	var month_days := array{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	if (time != 0)
		days := CInt((time / 86400) + 1);
	else
		days := CInt(((PolCore().systime - 1104465600) / 86400) + 1);
	endif
	var current_year_counter := 0;
	var current_month_counter := 1;
	var done := 0;
	repeat
		if (current_month_counter == 2)
			if ((current_year_counter % 4) == 0)
				if (days > 29)
					days := days - 29;
				else
					return padleft(days, 2);;
				endif
			else
				if (days > 28)
					days := days - 28;
				else
					return padleft(days, 2);;
				endif
			endif
		else
			if (days > 28)
				days := days - month_days[current_month_counter];
			else
				return padleft(days, 2);;
			endif
		endif
		if (days > 0)
			current_month_counter := current_month_counter + 1;
			if (current_month_counter > 12)
				current_month_counter := 1;
				current_year_counter := current_year_counter + 1;
			endif
		else
			done := 1;
		endif
	until (done);
	return padleft(days, 2);
endfunction

///////////////////
//  Returns the current day of the year including today (i.e. 001 - 366)
///////////////////
function time_n_dayofyear(time := 0)
	if (time != 0)
		return padleft(CInt(time / 86400), 3);
	else
		return padleft(CInt((PolCore().systime - 1104465600) / 86400), 3);
	endif
endfunction

///////////////////
//  Returns the current year (i.e. "2005")
//  Use '(returned_string - "20")' in your script for a shorthand year-value.
///////////////////
function time_n_year(time := 0)
	var systime;
	if (time != 0)
		systime := CInt(time);
	else
		systime := CInt(PolCore().systime - 1104465600);
	endif
	var current_year_counter := 0;
	var done := 0;
	repeat
		if (current_year_counter % 4 == 0)
			systime := systime - 31622400;
		else
			systime := systime - 31536000;
		endif
		if (systime >= 0)
			current_year_counter := current_year_counter + 1;
		else
			done:= 1;
		endif
	until (done);
	if (time != 0)
		return (1970 + current_year_counter);
	else
		return (2005 + current_year_counter);
	endif
endfunction

///////////////////
//  Returns current hour. (i.e. "1" - "12")
///////////////////
function time_n_hour(time := 0)
	var hour;
	if (time != 0)
		hour := CInt((time % 86400) / 3600);
	else
		hour := CInt((PolCore().systime % 86400) / 3600);
	endif
	if (hour > 12)
		hour := hour - 12;
	endif
	if (hour < 1)
		hour := 12;
	endif
	time_i_timezone(hour, 0);
	return hour;
endfunction

///////////////////
//  Returns current hour. (i.e. "01" - "12")
//  (zero padded)
///////////////////
function time_n_hourpadded(time := 0)
	var hour;
	if (time != 0)
		hour := CInt((time % 86400) / 3600);
	else
		hour := CInt((PolCore().systime % 86400) / 3600);
	endif
	if (hour > 12)
		hour := hour - 12;
	endif
	if (hour < 1)
		hour := 12;
	endif
	time_i_timezone(hour, 0);
	return padleft(hour, 2);
endfunction

///////////////////
//  Returns current hour. (i.e. "01" - "24")
//  (using a 24-hour clock)
///////////////////
function time_n_24hour(time := 0)
	var hour;
	if (time != 0)
		hour := CInt((time % 86400) / 3600);
	else
		hour := CInt((PolCore().systime % 86400) / 3600);
	endif
	time_i_timezone(hour, 0);
	return padleft(hour, 2);
endfunction

///////////////////
//  Returns the current minute in the hour. (i.e. "1" - "60")
///////////////////
function time_n_min(time := 0)
	if (time != 0)
		return CInt((time % 3600) / 60);
	else
		return CInt((PolCore().systime % 3600) / 60);
	endif
endfunction

///////////////////
//  Returns the current minute in the hour. (i.e. "01" - "60")
///////////////////
function time_n_minpadded(time := 0)
	if (time != 0)
		return padleft(CInt((time % 3600) / 60), 2);
	else
		return padleft(CInt((PolCore().systime % 3600) / 60), 2);
	endif
endfunction

///////////////////
//  Returns the current second in the minute. (i.e. "1" - "60")
///////////////////
function time_n_sec(time := 0)
	if (time != 0)
		return CInt(((time % 3600) / 60) / 60);
	else
		return CInt(((PolCore().systime % 3600) / 60) / 60);
	endif
endfunction

///////////////////
//  Returns the current second in the minute. (i.e. "01" - "60")
//  (zero padded)
///////////////////
function time_n_secpadded(time := 0)
	if (time != 0)
		return padleft(CInt(((time % 3600) / 60) / 60), 2);
	else
		return padleft(CInt(((PolCore().systime % 3600) / 60) / 60), 2);
	endif
endfunction

///////////////////
//  Returns current meridiem as a string. (i.e. "am" - "pm")
///////////////////
function time_s_meridiem(time := 0)
	var hour;
	if (time != 0)
		hour := CInt((time % 86400) / 3600);
	else
		hour := CInt((PolCore().systime % 86400) / 3600);
	endif
	time_i_timezone(hour, 0);
	var meridiem := "am";
	if (hour > 12)
		meridiem := "pm";
	endif
	return meridiem;
endfunction

///////////////////
//  Returns a preformated date and time string in EST. (i.e. "month dayofmonth, year -- hour:minutes meridiem")
//  (does not take into account "daylight saving" time)
///////////////////
function time_s_formatteddatetime(time := 0)
	var dayofmonth := time_n_dayofmonth(time);
	var hour := time_t_hour(time);
	return time_s_month(time) + " " + dayofmonth + ", " + time_n_year(time) + " -- " + padleft(hour, 2) + ":" + time_t_minpadded(time) + time_s_meridiem(time);
endfunction

// INTERNAL FUNCTIONS

///////////////////////////////////////////////////////////////
//  Returns a string length characters long, consisting of text padded on the left with as many instances of "0" as are needed to make up the full length.
//    text	 : value to pad.
//    length : length of the desired return string including padded zeros
///////////////////////////////////////////////////////////////
function padleft(text, length)
	text := CStr(text);
	while ( (CInt(length) - len(text)) >= len(text) )
		text := "0" + text;
	endwhile
	return text;
endfunction

function time_i_timezone(byref hour, byref day)
	if ((hour + TIMEZONE) < 0)
		hour := ((hour + TIMEZONE) + 12);
		day := (day - 1);
	else
		hour := (hour + TIMEZONE);
	endif
endfunction
Beaud
Novice Poster
Posts: 41
Joined: Tue Feb 14, 2006 2:06 pm

Post by Beaud »

Thank you both for your help :)
Post Reply