1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Seagate DateCode の変換方法

Posted at

Seagate製造のドライブに記載されているDateCodeの計算方法

計算

  • 書式は YYwd [w=1-9] 又は YYwwd [ww=10-53]
  • 7月第1土曜をYY-1年の第1週の開始日とする
    • 例:2017/07/01 (Sat) → 18年の第1週は7/1
  • wは週としてdは日数として計算する
    • 例:16112 → 2015/07/04 (Sat) + (11-1)*7 + 2 → 2015/09/14 (Mon)
  • 土曜始まりの会計年度というのがポイント?

実装例

JavaScript

sgDate.js
function sgDate( str ) {
  // Convert Seagate DateCode to YYYY/MM/DD
  var sy = 1999;
  var sw = 1;
  var sd = 0;

  // Format: YYwd YYwwd
  if ( str.length < 4 ){
    return "";
  } else if ( str.length < 5 ){
    sy += parseInt(str.substr(0,2)); 
    sw = parseInt(str.substr(2,1));
    sd = parseInt(str.substr(3,1));
  } else {
    sy += parseInt(str.substr(0,2)); 
    sw = parseInt(str.substr(2,2));
    sd = parseInt(str.substr(4,1));
  }

  try{
    // Begins from first Saturday of July 1999
    var d = new Date(sy,6,1);
    d.setTime( d.getTime() - d.getTimezoneOffset()*60*1000 );
    d.setDate( d.getDate() + 6 - d.getDay() ); 
    // Add weeks and days
    d.setDate( d.getDate() + sw * 7 - 7);
    d.setDate( d.getDate() + sd );
    return( d.toISOString().slice(0,10).replace(/-/g,"/") );
  }catch(e){
    return "Invalid string"; 
  }
}

moment.js

計算部分は moment.js を使うと少し見やすくなる

// require moment.js
  var dm = moment({ Y:sy, M:6, d:1 });
  dm.add( 6 - dm.day(), 'd');    
  dm.add( sw, 'w' );
  dm.add( sd, 'd' );
  return( dm.format("YYYY/MM/DD (ddd)") );

備考

PHP実装をJavaScriptで再実装した機会にまとめることに
特定ロット不良といった不具合期間は避けたいですね :-(

参考文献

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?