Page 1 of 1

Some Time Zone Handling functions

Posted: Thu Mar 31, 2016 2:51 pm
by SparrowhawkMMU
SB does not (currently) offer native functions to handle time zone conversion. I have put in a feature request >> here <<

Here is a small module I created to handle this problem for now:

Code: Select all

DeclareModule DateTime
	Declare.i GetCurrentUTCOffset()
	Declare.i ConvertFromUTCToCurrentTZ(dateTime.i)
	Declare.i ConvertFromCurrentTzToUTC(dateTime.i)
EndDeclareModule

Module DateTime
	
	; Get the current UTC offset in minutes
	Procedure.i GetCurrentUTCOffset()
		
		Protected offset.i = 0		

		! v_offset = new Date().getTimezoneOffset();
		
		ProcedureReturn offset
		
	EndProcedure
	
	
	; Convert a UTC datetime (numeric format) to current timezone (numeric format)
	Procedure.i ConvertFromUTCToCurrentTZ(dateTime.i)
		
		Protected newDate.i
		
		newDate = AddDate(dateTime, #PB_Date_Minute, GetCurrentUTCOffset() * -1)
		
		ProcedureReturn newDate
		
	EndProcedure

	
	; Convert a datetime using the current time zone (numeric format) to UTC (numeric format)
	Procedure.i ConvertFromCurrentTzToUTC(dateTime.i)
		
		Protected newDate.i
		
		newDate = AddDate(dateTime, #PB_Date_Minute, GetCurrentUTCOffset())
		
		ProcedureReturn newDate
		
	EndProcedure
		
EndModule