File Last Modified Time with Milliseconds Precisio

2019-02-25 19:25发布

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
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-25 20:11

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.

Write the code...

#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;
}

Compile, link....

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

And test...

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.

查看更多
登录 后发表回答