You are not logged in.
Pages: 1
Hi all guys, I post a simple script to get unix time and convert it to human time.
Automatically calculate the time zone.
/// @func get_unixtime()
/// @desc Returns Unix timestamp.
function get_unixtime(){
var timezone = date_get_timezone();
date_set_timezone(timezone_utc);
var epoch = floor(date_create_datetime(1970, 1, 1, 0, 0, 0));
date_set_timezone(timezone);
var datetime = date_current_datetime();
var timestamp = floor(date_second_span(epoch, datetime));
return timestamp;
}
/// @func convert_unixtime(unix_timestamp)
/// @desc Returns GameMaker datetime from Unix timestamp.
function convert_unixtime(timestamp){
var timezone = date_get_timezone();
date_set_timezone(timezone_utc);
var epoch = date_create_datetime(1970, 1, 1, 0, 0, timestamp);
var epH = date_get_hour(epoch);
date_set_timezone(timezone);
var cH = date_get_hour(date_current_datetime());
var calcFuse = (cH - epH) * 3600;
var datetime = date_create_datetime(1970, 1, 1, 0, 0, timestamp + calcFuse);
return datetime;
}
Example:
var timestamp = get_unixtime();
draw_text(x,y,"Unix time: "+string(timestamp));
var datetime = convert_unixtime(timestamp);
draw_text(x,y+32,"Human time: "+string(date_datetime_string(datetime)));
Offline
Pages: 1