Seagate Date Code

Seagate hard drives use a code to represent their manufacturing date. It is encoded as a number representing a year, week number and day of the week.

The date code is a four or five-digit number that can be interpreted as YYWD or YYWWD where

  • YY is the year, 00 is year 1999,
  • W or WW is the week number beginning 1, and
  • D is day of week beginning 1.
  • Week 1 begins on the first saturday of July in the stated year

Some examples:

  • 06212 means Sunday 20 November 2005
  • 0051 means Saturday 31 July 1999

The following bash script takes a code and displays the decoded date. It can optionally take a second argument, which is a format specifciation as per the date command. If a format isn't given then it defaults to

%A %B %-d %Y

Usage:

$ seadate code [format]

For example

$ seadate 14431
Saturday April 26 2014

or

$ seadate 14431 '%B %-d, %Y'
April 26, 2014

Here's the script:

#!/bin/bash
#
# Seagate hard drive date decoder
#
# Specification: http://www.lerti.fr/web/public/NoteTechnique03-v1.01.pdf
# Compare with:  http://www.westernnetworks.com/tools/seagatedatecode2.php
#
# Format YYWWD or YYWD, where:
#                        YY is the year, 00 is year 1999
#                        W or WW is the week number beginning 1
#                        D is day of week beginning 1
#
# Week 1 begins on the first saturday in July of the given year.
#
# Usage: $1 is the code to convert
#        $2 is an optional format (see `man date`), defaulted (see code)
#
########################################################## JL 20140923 ###


date_format=${2:-%A %B %-d %Y}  
regex='^(..)(..?)(.)$'  
code=$1

[[ $code =~ $regex ]] || { echo "bad code"; exit 1; }
year=$(( ${BASH_REMATCH[1]} + 1999 ))  
week=$(( ${BASH_REMATCH[2]} - 1))  
day=$(( ${BASH_REMATCH[3]} - 1))

offset=$(( 6 - $(date -d "$year-07-01" +%u) ))  
july_first_saturday=$(date -d "$year-7-01 $offset days" +%d)  
date -d  "${year}-07-${july_first_saturday} ${week} weeks ${day} days" "+${date_format}"  

The original script

This script bases its calculation on the last Saturday in June because

  • it's easer to find the last Saturday than the first, and
  • week number can be added to it, making week 1 the first Saturday in July.

However, I preferred the method used in a Stack Overflow answer; hence the updated version. Here is the original for posterity:

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

date_format=${2:-%A %B %-d %Y}

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 '{ $6 && X=$6 } END { print X }')

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

References: