2004-01-29 21:20:59 +00:00
|
|
|
// 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 ()
|
|
|
|
{
|
2004-02-06 19:49:02 +00:00
|
|
|
utc_offset = (int) DateTime.Now.Subtract (DateTime.UtcNow).TotalSeconds;
|
2004-01-29 21:20:59 +00:00
|
|
|
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;
|
2004-02-04 19:54:46 +00:00
|
|
|
int size = Marshal.SizeOf (typeof (int)) + GetNativeDataSize ();
|
2004-01-29 21:20:59 +00:00
|
|
|
IntPtr ptr = Marshal.AllocCoTaskMem (size);
|
|
|
|
IntPtr time_t_ptr = new IntPtr (ptr.ToInt32 () + Marshal.SizeOf (typeof(int)));
|
2004-02-06 19:49:02 +00:00
|
|
|
int secs = ((int)dt.Subtract (local_epoch).TotalSeconds) + utc_offset;
|
2004-02-04 19:54:46 +00:00
|
|
|
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.");
|
2004-01-29 21:20:59 +00:00
|
|
|
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)
|
|
|
|
{
|
2004-02-12 12:26:25 +00:00
|
|
|
long secs;
|
2004-02-04 19:54:46 +00:00
|
|
|
if (GetNativeDataSize () == 4)
|
2004-02-12 12:26:25 +00:00
|
|
|
secs = (int)data;
|
2004-02-04 19:54:46 +00:00
|
|
|
else if (GetNativeDataSize () == 8)
|
2004-02-12 12:26:25 +00:00
|
|
|
secs = (long)data;
|
2004-02-04 19:54:46 +00:00
|
|
|
else
|
|
|
|
throw new Exception ("Unexpected native size for time_t.");
|
|
|
|
|
2004-02-12 12:26:25 +00:00
|
|
|
TimeSpan span = new TimeSpan ((int)secs - utc_offset);
|
2004-02-04 19:54:46 +00:00
|
|
|
return local_epoch.Add (span);
|
2004-01-29 21:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CleanUpManagedData (object obj) {}
|
|
|
|
|
2004-02-04 19:54:46 +00:00
|
|
|
[DllImport ("gtksharpglue")]
|
|
|
|
static extern int gtksharp_time_t_sizeof ();
|
|
|
|
|
2004-01-29 21:20:59 +00:00
|
|
|
public int GetNativeDataSize ()
|
|
|
|
{
|
2004-02-04 19:54:46 +00:00
|
|
|
return gtksharp_time_t_sizeof ();
|
2004-01-29 21:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|