Help us understand the problem. What is going on with this article?

Seagate DateCode の変換方法

More than 1 year has passed since last update.

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で再実装した機会にまとめることに
特定ロット不良といった不具合期間は避けたいですね :-(

参考文献

www.lerti.fr/web/public/NoteTechnique03-v1.01.pdf

directk
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした