2017-03-14 3 views
0

私はdkfileutilsからPath.touchためのテストを書いている:time.time()とfile.getmtime()の関係は?

class Path(str): 
    def touch(self, mode=0o666, exist_ok=True): 
     """Create this file with the given access mode, if it doesn't exist. 
      (based on https://github.com/python/cpython/blob/master/Lib/pathlib.py) 
     """ 
     if exist_ok: 
      # First try to bump modification time 
      # Implementation note: GNU touch uses the UTIME_NOW option of 
      # the utimensat()/futimens() functions. 
      try: 
       os.utime(self, None) 
      except OSError: 
       # Avoid exception chaining 
       pass 
      else: 
       return 
     flags = os.O_CREAT | os.O_WRONLY 
     if not exist_ok: 
      flags |= os.O_EXCL 
     fd = os.open(self, flags, mode) 
     os.close(fd) 

私は..

def test_touch(): 
    before = time.time() 
    with open('a', 'w') as fp: 
     fp.write('a') 
    after = time.time() 

    assert before <= after 

    a = Path('a') 
    a_before_touch = a.getmtime() 
    assert before <= a_before_touch <= after 

    a.touch() 
    after_touch = time.time() 
    a_after_touch = a.getmtime() 

    assert a_before_touch <= a_after_touch 
    assert a_after_touch >= after 
    assert a_after_touch <= after_touch 

を以下のバリエーションを試してみましたが、それは(https://travis-ci.org/datakortet/dkfileutils/builds)失敗し続け、68を実行します。

>   assert before <= a_before_touch <= after 
E   assert 1489532961.064958 <= 1489532961.064464 

ラン67

>   assert a_after_touch > after 
E   assert 1489532462.412086 > 1489532462.412933 

ラン66

>   assert a_after_touch > after 
E   assert 1489532286.6042855 > 1489532286.606766 

アムそのtime.time()とfile.getmtimeを想定しで私は愚かな()に関連していますか?

答えて

2

すべてのファイルシステムは、ファイルタイムスタンプを格納するために限られたビット数しか割り当てません。いくつかの古いファイルシステムでは、解像度は1秒以下でした。一般的に、時計を直接読むのと同じ解像度を決して与えるつもりはありません。