如何在 Bash 脚本中解码希捷的硬盘驱动器日期代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25992187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:23:39  来源:igfitidea点击:

How to decode Seagate's hard drive date code in a Bash script

bash

提问by starfry

Seagatehard drives display a code instead of the manufacturing date. The code is described hereand an online decoder is available here.

希捷硬盘显示代码而不是制造日期。该代码是描述在这里和在线译码器可用在这里

In short, it's a 4 or 5 digit number of the form YYWWDor YYWD, where:

简而言之,它是形式YYWWDor的 4 位或 5 位数字YYWD,其中:

  • YYis the year, 00 is year 1999
  • Wor WWis the week number beginning 1
  • Dis day of week beginning 1
  • Week 1 begins on the first saturday of July in the stated year
  • YY是年份,00 是 1999 年
  • W或者WW是从 1 开始的周数
  • D是星期几从 1 开始
  • 第 1 周从指定年份的 7 月的第一个星期六开始

Examples

例子

  • 06212 means Sunday 20 November 2005
  • 0051 means Saturday 31 July 1999
  • 06212 表示 2005 年 11 月 20 日星期日
  • 0051 表示 1999 年 7 月 31 日星期六

How can this be decoded in a bashscript ?

这如何在bash脚本中解码?

采纳答案by thriqon

This is what I did, it should work:

这就是我所做的,它应该可以工作:

#!/bin/bash

DATE=
REGEX="^(..)(..?)(.)$"

[[ $DATE =~ $REGEX ]]
YEAR=$(( ${BASH_REMATCH[1]} + 1999 ))
WEEK=$(( ${BASH_REMATCH[2]} - 1))
DAYOFWEEK=$(( ${BASH_REMATCH[3]} - 1))


OFFSET=$(( 6 - $(date -d "$YEAR-07-01" +%u) ))
DATEOFFIRSTSATURDAY=$(date -d "$YEAR-7-01 $OFFSET days" +%d)
FINALDATE=`date -d  "$YEAR-07-$DATEOFFIRSTSATURDAY $WEEK weeks $DAYOFWEEK days"`

echo $FINALDATE

It worked for the two dates given above... If you want to customize the date output, add a format string at the end of the FINALDATe assignment.

它适用于上面给出的两个日期...如果您想自定义日期输出,请在 FINALDATe 分配的末尾添加一个格式字符串。

回答by starfry

Here is a short script, it takes two arguments: $1is the code to convert and $2is an optional format (see man date), otherwise defaulted (see code).

这是一个简短的脚本,它有两个参数:$1是要转换的代码,$2是可选格式(请参阅man date),否则为默认格式(请参阅代码)。

It uses the last Saturday in June instead of the first one in July because I found it easer to locate and it allowed me to just add the relevant number of weeks and days to it.

它使用 6 月的最后一个星期六而不是 7 月的第一个星期六,因为我发现它更容易定位,并且它允许我只添加相关的周数和天数。

#!/bin/bash
date_format=${2:-%A %B %-d %Y}

code=
[[ ${#code} =~ ^[4-5]$ ]] || { echo "bad code"; exit 1; }

let year=1999+${code:0:2}
[[ ${#code} == 4 ]] && week=${code:2:1} || week=${code:2:2}
day=${code: -1}

june_last_saturday=$(cal 06 ${year} | awk '{  && X= } END { print X }')

date -d "${year}-06-${june_last_saturday} + ${week} weeks + $((${day}-1)) days" "+${date_format}"

Examples:

例子:

$ seadate 06212
Sunday November 20 2005
$ seadate 0051
Saturday July 31 1999

回答by Jared Palmer

I created a Seagate Date Code Calculator that actually works with pretty good accuracy. I've posted it here on this forum for anyone to use: https://www.data-medics.com/forum/seagate-date-code-conversion-translation-tool-t1035.html#p3261

我创建了一个 Seagate 日期代码计算器,它实际上可以非常准确地工作。我已将其发布在此论坛上供任何人使用:https: //www.data-medics.com/forum/seagate-date-code-conversion-translation-tool-t1035.html#p3261

It's far more accurate than the other ones online which often point to the entirely wrong year. I know it's not a bash script, but will still get the job done for anyone else who's searching how to do this.

它比网上的其他那些经常指向完全错误的年份要准确得多。我知道它不是 bash 脚本,但仍然可以为正在搜索如何执行此操作的其他任何人完成工作。

Enjoy!

享受!