以毫秒为精度的文件上次修改时间 [英] File Last Modified Time with Milliseconds Precision

查看:42
本文介绍了以毫秒为精度的文件上次修改时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,其中包含一些通过脚本修改的文件,因此它们的 mtime 仅相差几毫秒.file mtime filename 命令以秒为单位给出了上次修改时间,这对我没有帮助.如何以毫秒精度获得他们的最后修改时间?

I have a directory with some files which are modified through a script so their mtimes differ by only a few milliseconds. The file mtime filename command gives me last modified time in seconds, which doesn't really help me. How can I get their last modified time with millisecond precision?

推荐答案

看起来最简单的方法是编写 Tcl 扩展在 C 中以您需要的精度获得文件修改时间.这其实很简单.

Looks like the easiest way to do this is write a Tcl extension in C to get the file modification time with the precision you need. This is actually quite straightforward.

编写代码...

#define USE_TCL_STUBS

#include <tcl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int precisionmtimeObjCmd (
  ClientData cd,
  Tcl_Interp* interp,
  int objc,
  Tcl_Obj * const objv[]
  )
{
  char        *fn;
  int         len1;
  struct stat statinfo;
  long        value;

  /* for UTF-8 locales */
  if (objc != 2) {
    Tcl_WrongNumArgs(interp, 1, objv, "precisionmtime");
    return TCL_ERROR;
  }
  fn = Tcl_GetStringFromObj(objv[1], &len1);
  stat (fn, &statinfo);
  value = statinfo.st_mtime;
  value *= 1000;
  value += statinfo.st_mtim.tv_nsec / 1000000;
  Tcl_SetObjResult (interp, Tcl_NewLongObj(value));
  return TCL_OK;
}

int Precisionmtime_Init (Tcl_Interp *interp)
{
  Tcl_Encoding utf;
  if (!Tcl_InitStubs (interp,"8.3",0)) {
    return TCL_ERROR;
  }

  Tcl_CreateObjCommand (interp,"precisionmtime", precisionmtimeObjCmd, NULL, NULL);
  Tcl_PkgProvide (interp,"precisionmtime","0.1");

  return TCL_OK;
}

编译、链接....

bll-tecra:bll$ cc -I/home/bll/local/include -c pmtime.c -o pmtime.o -fPIC
bll-tecra:bll$ cc -L/home/bll/local/lib -o precisionmtime.so -shared pmtime.o -ltclstub8.6

并测试...

bll-tecra:bll$ rlwrap tclsh
% load [pwd]/precisionmtime.so
% info commands precision*
precisionmtime
% precisionmtime /home/bll/local/include/tcl.h
1524458623343
% file mtime /home/bll/local/include/tcl.h
1524458623
% 

好吧,为你做了所有的工作.但是很有趣.

Well, did all the work for you. But it was fun.

这篇关于以毫秒为精度的文件上次修改时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆