Merge pull request #1323 from StephenMcConnel/bug-23591
[mono.git] / mcs / class / Mono.Posix / Mono.Unix.Native / NativeConvert.cs
index 6dc29ae5cc55d7f189d2cc22010ae30b5b78cad1..ffa329f1b5bf3d5fa7cc75a7a6e93355b118d117 100644 (file)
@@ -18,6 +18,24 @@ namespace Mono.Unix.Native {
                // Non-generated exports
                //
 
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromRealTimeSignum")]
+               private static extern int FromRealTimeSignum (Int32 offset, out Int32 rval);
+
+               // convert a realtime signal to os signal
+               public static int FromRealTimeSignum (RealTimeSignum sig)
+               {
+                       int sigNum;
+                       if (FromRealTimeSignum (sig.Offset, out sigNum) == -1)
+                               ThrowArgumentException (sig.Offset);
+                       return sigNum;
+               }
+
+               // convert an offset to an rt signum
+               public static RealTimeSignum ToRealTimeSignum (int offset)
+               {
+                       return new RealTimeSignum (offset);
+               }
+
                // convert from octal representation.
                public static FilePermissions FromOctalPermissionString (string value)
                {
@@ -165,6 +183,8 @@ namespace Mono.Unix.Native {
                        return '-';
                }
 
+               public static readonly DateTime UnixEpoch =
+                       new DateTime (year:1970, month:1, day:1, hour:0, minute:0, second:0, kind:DateTimeKind.Utc);
                public static readonly DateTime LocalUnixEpoch = 
                        new DateTime (1970, 1, 1);
                public static readonly TimeSpan LocalUtcOffset = 
@@ -175,6 +195,11 @@ namespace Mono.Unix.Native {
                        return FromTimeT (time);
                }
 
+               public static DateTime ToDateTime (long time, long nanoTime)
+               {
+                       return FromTimeT (time).AddMilliseconds (nanoTime / 1000);
+               }
+
                public static long FromDateTime (DateTime time)
                {
                        return ToTimeT (time);
@@ -182,15 +207,18 @@ namespace Mono.Unix.Native {
 
                public static DateTime FromTimeT (long time)
                {
-                       DateTime r = LocalUnixEpoch.AddSeconds ((double) time + 
-                                       LocalUtcOffset.TotalSeconds);
-                       return r;
+                       return UnixEpoch.AddSeconds (time).ToLocalTime ();
                }
 
                public static long ToTimeT (DateTime time)
                {
-                       TimeSpan unixTime = time.Subtract (LocalUnixEpoch) - LocalUtcOffset;
-                       return (long) unixTime.TotalSeconds;
+                       if (time.Kind == DateTimeKind.Unspecified)
+                               throw new ArgumentException ("DateTimeKind.Unspecified is not supported. Use Local or Utc times.", "time");
+
+                       if (time.Kind == DateTimeKind.Local)
+                               time = time.ToUniversalTime ();
+
+                       return (long) (time - UnixEpoch).TotalSeconds;
                }
 
                public static OpenFlags ToOpenFlags (FileMode mode, FileAccess access)
@@ -299,6 +327,38 @@ namespace Mono.Unix.Native {
                                throw new ArgumentException (fopen_mode);
                        return fopen_mode;
                }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromStat")]
+               private static extern int FromStat (ref Stat source, IntPtr destination);
+
+               public static bool TryCopy (ref Stat source, IntPtr destination)
+               {
+                       return FromStat (ref source, destination) == 0;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToStat")]
+               private static extern int ToStat (IntPtr source, out Stat destination);
+
+               public static bool TryCopy (IntPtr source, out Stat destination)
+               {
+                       return ToStat (source, out destination) == 0;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_FromStatvfs")]
+               private static extern int FromStatvfs (ref Statvfs source, IntPtr destination);
+
+               public static bool TryCopy (ref Statvfs source, IntPtr destination)
+               {
+                       return FromStatvfs (ref source, destination) == 0;
+               }
+
+               [DllImport (LIB, EntryPoint="Mono_Posix_ToStatvfs")]
+               private static extern int ToStatvfs (IntPtr source, out Statvfs destination);
+
+               public static bool TryCopy (IntPtr source, out Statvfs destination)
+               {
+                       return ToStatvfs (source, out destination) == 0;
+               }
        }
 }