# -*- coding: utf-8 -*-
# Copyright (c) 2017 Richard Hull and contributors
# See LICENSE.rst for details.

import os
import time
import warnings
import ctypes.util


__all__ = ["deprecation", "monotonic"]


try:
    # only available since python 3.3
    monotonic = time.monotonic
except AttributeError:
    try:
        clock_gettime = ctypes.CDLL(ctypes.util.find_library('c'),
                                    use_errno=True).clock_gettime
    except AttributeError:
        clock_gettime = ctypes.CDLL(ctypes.util.find_library('rt'),
                                    use_errno=True).clock_gettime

    class timespec(ctypes.Structure):
        """
        Time specification, as described in clock_gettime(3).
        """
        _fields_ = (('tv_sec', ctypes.c_long),
                    ('tv_nsec', ctypes.c_long))

    def monotonic():
        """
        Monotonic clock that cannot go backward.
        """
        ts = timespec()
        if clock_gettime(1, ctypes.pointer(ts)):
            errno = ctypes.get_errno()
            raise OSError(errno, os.strerror(errno))
        return ts.tv_sec + ts.tv_nsec / 1.0e9


def deprecation(message):
    warnings.warn(message, DeprecationWarning, stacklevel=2)

@@ -110,16 +110,27 @@
 port os%0A
+import sys%0A
 import t
@@ -312,21 +312,834 @@
 or:%0A    
-try:%0A
+if sys.platform == 'darwin':  # OS X, iOS%0A        libc = ctypes.CDLL('/usr/lib/libc.dylib', use_errno=True)%0A%0A        class mach_timebase_info_data_t(ctypes.Structure):%0A            %22%22%22System timebase info. Defined in %3Cmach/mach_time.h%3E.%22%22%22%0A            _fields_ = (('numer', ctypes.c_uint32),%0A                        ('denom', ctypes.c_uint32))%0A%0A        mach_absolute_time = libc.mach_absolute_time%0A        mach_absolute_time.restype = ctypes.c_uint64%0A%0A        timebase = mach_timebase_info_data_t()%0A        libc.mach_timebase_info(ctypes.byref(timebase))%0A        ticks_per_second = timebase.numer / timebase.denom * 1.0e9%0A%0A        def monotonic():%0A            %22%22%22%0A            Monotonic clock that cannot go backward.%0A            %22%22%22%0A            return mach_absolute_time() / ticks_per_second%0A    else:%0A        try:%0A    
         
@@ -1225,32 +1225,36 @@
                 
+    
 use_errno=True).
@@ -1271,16 +1271,20 @@
 ime%0A    
+   
+ 
 except A
@@ -1294,24 +1294,28 @@
 ibuteError:%0A
+    
         cloc
@@ -1398,32 +1398,36 @@
                 
+  
+  
 use_errno=True).
@@ -1441,16 +1441,20 @@
 ettime%0A%0A
+    
     clas
@@ -1483,36 +1483,44 @@
 cture):%0A        
+ 
+   
 %22%22%22%0A
+    
         Time spe
@@ -1565,32 +1565,36 @@
 ime(3).%0A        
+    
 %22%22%22%0A        _fie
@@ -1581,32 +1581,36 @@
     %22%22%22%0A        
+   
+ 
 _fields_ = (('tv
@@ -1652,16 +1652,20 @@
         
+    
 ('tv_nse
@@ -1685,24 +1685,28 @@
 long))%0A%0A    
+  
+  
 def monotoni
@@ -1710,32 +1710,36 @@
 onic():%0A        
+    
 %22%22%22%0A        Mono
@@ -1726,32 +1726,36 @@
     %22%22%22%0A        
+ 
+   
 Monotonic clock 
@@ -1779,36 +1779,44 @@
 ckward.%0A        
-%22%22%22%0A
+    %22%22%22%0A    
         ts = tim
@@ -1831,16 +1831,20 @@
         
+   
+ 
 if clock
@@ -1888,16 +1888,20 @@
         
+    
 errno = 
@@ -1931,16 +1931,20 @@
         
+  
+  
 raise OS
@@ -1976,16 +1976,20 @@
 errno))%0A
+    
         

add monotonic for py27 osx (#60)