Ryujinx-GtkSharp/glib/time_t_CustomMarshaler.cs
Mike Kestner e83c55a242 2004-03-12 Mike Kestner <mkestner@ximian.com>
* */Makefile.am : automakify the build
	* */Makefile.in : kill
	* *.custom : remove System.Drawing dependencies
	* *.cs : remove System.Drawing dependencies
	* *-api.xml : mv to *-api.raw
	* glue/* : mv to lib specific gluelibs for glib, gdk, gtk, and glade.
	* gtk/gtk-symbols : alias GtkType to GType
	* sources/gtk-sharp-sources.xml : create .raw files. They are now
	transformed to .xml files by the metadata compilation step.

svn path=/trunk/gtk-sharp/; revision=23967
2004-03-12 21:18:11 +00:00

79 lines
2.0 KiB
C#

// time_t_CustomMarshaler.cs - Custom marshaling between time_t and DateTime
//
// Author: Mike Kestner <mkestner@ximian.com>
//
// Copyright (c) 2004 Novell, Inc.
namespace GLib {
using System;
using System.Runtime.InteropServices;
public class time_t_CustomMarshaler : ICustomMarshaler {
static time_t_CustomMarshaler marshaler;
int utc_offset;
DateTime local_epoch;
private time_t_CustomMarshaler ()
{
utc_offset = (int) DateTime.Now.Subtract (DateTime.UtcNow).TotalSeconds;
local_epoch = new DateTime (1970, 1, 1, 0, 0, 0);
}
public static ICustomMarshaler GetInstance (string cookie)
{
if (marshaler == null)
marshaler = new time_t_CustomMarshaler ();
return marshaler;
}
public IntPtr MarshalManagedToNative (object obj)
{
DateTime dt = (DateTime) obj;
int size = Marshal.SizeOf (typeof (int)) + GetNativeDataSize ();
IntPtr ptr = Marshal.AllocCoTaskMem (size);
IntPtr time_t_ptr = new IntPtr (ptr.ToInt32 () + Marshal.SizeOf (typeof(int)));
int secs = ((int)dt.Subtract (local_epoch).TotalSeconds) + utc_offset;
if (GetNativeDataSize () == 4)
Marshal.WriteInt32 (time_t_ptr, secs);
else if (GetNativeDataSize () == 8)
Marshal.WriteInt64 (time_t_ptr, secs);
else
throw new Exception ("Unexpected native size for time_t.");
return time_t_ptr;
}
public void CleanUpNativeData (IntPtr data)
{
IntPtr ptr = new IntPtr (data.ToInt32 () - Marshal.SizeOf (typeof (int)));
Marshal.FreeCoTaskMem (ptr);
}
public object MarshalNativeToManaged (IntPtr data)
{
long secs;
if (GetNativeDataSize () == 4)
secs = (int)data;
else if (GetNativeDataSize () == 8)
secs = (long)data;
else
throw new Exception ("Unexpected native size for time_t.");
TimeSpan span = new TimeSpan ((int)secs - utc_offset);
return local_epoch.Add (span);
}
public void CleanUpManagedData (object obj) {}
[DllImport ("glibsharpglue")]
static extern int gtksharp_time_t_sizeof ();
public int GetNativeDataSize ()
{
return gtksharp_time_t_sizeof ();
}
}
}