【问题标题】:Batch change accessed and modified date, with date from another file's content?批量更改访问和修改日期,日期来自另一个文件的内容?
【发布时间】:2020-08-30 08:11:01
【问题描述】:

我正在将旧笔记从基于 SQL 数据库的笔记应用程序迁移到单独的文本文件中。 我已经成功地将笔记和日期代码导出为单独的文本文件。

文件的顺序如下:

$ ls -1
Note0001.txt
Note0001-date.txt
Note0002.txt
Note0002-date.txt
Note0003.txt
Note0003-date.txt

日期文件的内容如下所示:

$ cat Note0001-date.txt 
388766121.742373
$ cat Note0002-date.txt 
274605766.273638
$ cat Note0003-date.txt 
384996285.436197

日期是自纪元 2001-01-01 以来的秒数。请参阅有关格式的其他问题:What type of date format is this? And how to convert it?

如何将笔记文件NoteNNNN.txt的访问和修改日期批量修改为相应日期文件NoteNNNN-date.txt内容中的日期?

如何将日期转换为 UTC+1?最好考虑 DST(夏令时)。 我正在尝试使用描述此问题的方法转换日期: https://unix.stackexchange.com/questions/2987/ 但它在 bash 3.2.57 (macOS) 中输出错误消息:

$ date -d '2001-01-01 UTC+1 + 388766121 seconds'
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
        [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

我不熟悉在终端中处理日期和时间戳。

【问题讨论】:

  • 你想要to touch文件。
  • What date format is that ?你刚刚指定了The dates are seconds since the epoch 2001-01-01,所以它们是自纪元以来的秒数。它们是什么格式的? how to convert it? 那句话中的“它”是什么并转换成什么?转换“日期格式”?
  • 是的,对不起,我编辑了。
  • 你的盒子时区是UTC+1吗? date +%Z =?
  • 是的,我的时区是 UTC+1(瑞典),夏季夏令时。 date +%Z 给出 CEST。

标签: bash macos date


【解决方案1】:

遍历每个文件对,访问时间戳,移动时间戳以便 unix 工具可以理解,然后触摸文件。 IE。大问题是小问题的总和。

# find all files named .txt but not -date.txt
find . -name '*.txt' '!' -name '*-date.txt' |
# remove the .txt suffix
sed 's/\.txt$//' |
{ 
   # the reference point of files content
   start=$(date -d "2001-01-01" +%s) # will not work with BSD date
   # I guess just precompute the value:
   start=978303600
   # for each file
   while IFS= read -r f; do
       # get the timestamp
       diff=$(<"$f"-date.txt)
       # increment the timestamp to seconds since epoch
       ref=$(<<<"scale=6; $start + $diff" bc)
       # TODO: use a tool convert the timestamp sinece epoch to BSD touch 
       # compatible format, ie. to ccyy-mm-ddTHH:MM:SS[.frac][Z]
       ref=(TODO "$ref")
       # change access and modification times of .txt file
       touch -d "@$ref" "$f".txt
   done
}

【讨论】:

  • 谢谢卡米尔库克。该脚本是否也尊重 UTC+1 和 DST?
  • 我测试了脚本,它显示一个错误:触摸:非法选项--d
  • Will that script also respect UTC+1 and DST? 不知道,请参阅man date 处理LC_*TZ 环境变量。 touch: illegal option -- d 好吧,你在 macos 上。所以你有 openbsd touchdate,我写这些时考虑到了 GNU 工具。您可以安装 GNU 替代品或将脚本重构为 BSD touch 和 date 可以使用的东西。
  • 如果您在 macOS 上使用 homebrew,则可以运行 brew install coreutils,这将为您提供 GNU datetouch 作为 @ 987654333@ 和gtouch。 HTH。
  • 太棒了!谢谢马克。我在 KamilCuk 脚本的第一个版本中更改为 gdate 和 gtouch,它在 macOS 上运行。但我仍然不知道如何处理 UTC+1 和 DST。这里有一些信息:stackoverflow.com/questions/19901906,但可能有更好的方法。现在真的很近了。
【解决方案2】:

假设您的操作系统本地时区是您想要的输出,并且您有支持 GNU awk time functionsawk 版本,您可以使用 以下脚本。另外:

如果 DST 夏令时标志为正,则假定时间为 是夏令时;如果为零,则假定时间为标准时间 时间;如果是否定的(默认值),mktime() 会尝试确定 夏令时是否在指定时间内有效。

文件tst.awk:

BEGIN {
    epoch = mktime("2001 01 01 00 00 00") 
}

FNR==1 {
    close(out)
    out = substr(FILENAME, 1, length(FILENAME)-9) ".txt"
}

{ 
    print strftime("%F %T %Z", epoch+$0) > out
}

用法:

awk -f tst.awk *-date.txt

示例

这是一个脚本示例,没有 I/O 部分,只是转换日期时间。

测试文件:

> cat file
388766121.742373
274605766.273638
384996285.436197

脚本tst.awk:

BEGIN { epoch = mktime("2001 01 01 00 00 00") }
{ print strftime("%F %T %Z", epoch+$0) }

输出:

> awk -f tst.awk file
2013-04-27 15:35:21 EEST
2009-09-14 08:22:46 EEST
2013-03-14 23:24:45 EET

默认情况下使用我的盒子的时区(EET)。如果我们想打印到不同的时区,我们应该定义它并设置TZ。此外,默认情况下使用 DST,请注意有些日子会打印为 EEST(夏令时)。

【讨论】:

  • 感谢您的回复。我测试了它并得到了这个:$ awk -f epoch-2001-convert.awk *-date.txt awk: can't open file epoch-2001-convert.awk source line number 1 source file epoch-2001-convert.awk context is &gt;&gt;&gt; &lt;&lt;&lt;
  • 如何打印夏令时标志?
  • @Kasam 我已经更新了一个 example 来证明我所说的并且默认情况下 DST 是打开的。如果您遇到任何类似awk: can't open file... 的错误,您需要自行解决,错误信息非常明确。
  • 问题出在 macOS 上的 awk 上,安装 brew gawk 后脚本工作正常。在名为 MemoLite0142.txt 的文件上运行第一个脚本时,它创建了一个名为 MemoLit.txt 的文件,并带有时间戳。时间戳的日期错误,但正确 [!] 为 DST 调整的时间。修改后的脚本打印到屏幕上。结果与上述相同,日期错误,但时间正确。如何使用此方法更改文件时间戳[具有正确日期]?
  • @Kasam 对于所有转换,结果和预期日期之间是否存在标准偏移差异?几天了?
猜你喜欢
  • 1970-01-01
  • 2013-07-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多