2016-12-03 125 views
0

背景UTC轉換爲十進制的時間

我想根據一個古老的法語版本的一些修改,以創建一個新的日期/時間系統。

這涉及變換UTC日期/時間到新的數量:

  • 12個月=>10個月
  • 52周=>36.5周每月=> 36/37
  • 28/31天每月天
  • 24小時=>20小時
  • 60分鐘=>100分鐘
  • 60秒=>100秒

我編寫了一個時鐘在JavaScript作爲概念驗證,但不確定爲我是否已經正確地計算出一切,另外無論是最好的方法:

代碼

1)getDecimalDate ()計算一年中的某一天,然後計算出它在每月36或37天的新日曆中存在的月份。然後計算月份的新日期。

function getDecimalDate(date) { 
    var oldDay = 1000 * 60 * 60 * 24, 
     startYear = new Date(Date.UTC(date.getUTCFullYear(), 0, 0)), 
     day = Math.floor((date - startYear)/oldDay), 
     num = 0, 
     month = 1; 
    if (day > 36) { num += 36; month = 2; } 
    if (day > 73) { num += 37; month = 3; } 
    if (day > 109) { num += 36; month = 4; } 
    if (day > 146) { num += 37; month = 5; } 
    if (day > 182) { num += 36; month = 6; } 
    if (day > 219) { num += 37; month = 7; } 
    if (day > 255) { num += 36; month = 8; } 
    if (day > 292) { num += 37; month = 9; } 
    if (day > 328) { num += 36; month = 10; } 
    return { day: day - num, month: month, year: date.getUTCFullYear(), num: num }; 
} 

2)getDecimalTime()計算自午夜起的毫秒數,然後每天從舊毫秒到新的總計更改它,然後計算小時,分鐘等

function getDecimalTime(date) { 
    var oldDay = 1000 * 60 * 60 * 24, 
     newDay = 1000 * 100 * 100 * 20, 
     startDay = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())), 
     delta = ((date - startDay)/oldDay) * newDay; 

    var hours = Math.floor(delta/10000000) % 20; 
    delta -= hours * 10000000; 
    var minutes = Math.floor(delta/100000) % 100; 
    delta -= minutes * 100000; 
    var seconds = Math.floor(delta/1000) % 100; 
    delta -= seconds * 1000; 
    var milliseconds = Math.floor(delta) % 1000; 
    return { milliseconds: milliseconds, seconds: seconds, minutes: minutes, hours: hours }; 
} 

你可以看到一個工作在此版本: https://jsfiddle.net/kmturley/7mrwc3x3/9/

結果

請記住我已經決定了日/月使用拉丁文學名(月= 9,模具=日,DEC = 10,mense =月)

  • 字符串 - 週六12月3日=> Novdie Decmense 10
  • 日期 - 2016年3月12日=> 2016年10月10日
  • 時間 - 22點47分52秒=> 18:98:43

問題

  1. 是數學正確嗎?
  2. 時區有問題嗎?我 試圖將所有日期對象轉換爲UTC,但JavaScript可以是 棘手
  3. 我可以改進代碼嗎?該月的選擇似乎可以改善,但我無法找出一個更好的方法來計算36 和37天的月份。如果(num%36.5 === 1)不起作用?

謝謝!

更新 - 2016年12月7日 - 新版本的基礎上的解決方案: https://jsfiddle.net/kmturley/7mrwc3x3/10/

https://github.com/kmturley/decimal-time

+0

'是否有任何與時區的問題是什麼?我試過將所有的Date對象轉換爲UTC,但JavaScript可能會很棘手......好吧,時區並不比偏移複雜,所以我個人會保留這些函數的UTC位。 – Keith

+0

所有日期對象**都是** UTC。時區偏移量來自主機,非UTC方法使用該偏移量來計算「本地」值。如果您使用所有UTC方法,那麼您將得到UTC值,並且不使用時區偏移。 – RobG

+0

好點,我有一些不必要的代碼轉換UTC到UTC有:) –

回答

1

是數學正確的嗎?

你沒有說哪個月有35天的,並且有36個,所以我們不得不接受的是,如果陳述是正確的。您不會顯示如何創建日期,因此它可能會或可能不會。而且你不會說閏年會發生什麼,這個系統似乎每年只有365天。

以下:

  • 24小時=>20小時
  • 60分鐘=>100分鐘
  • 60秒=>100秒

不看起來正確。實際上,你的意思是:

  • 1天= 20十進制小時
  • 1位小數小時= 100十進制分鐘
  • 1位小數分鐘= 100十進制秒
  • 1位小數秒= 1000十進制毫秒

你以毫秒爲單位獲得時間和縮小到小數的策略看起來不錯,我只會提出以下意見。

getDecimalTime它更簡單通過第一複印日期計算朝九特派然後其UTC小時設置爲零:

startDay = new Date(+date); 
startDate.setUTCHours(0,0,0,0); 

然後規模:

var diffMilliseconds = date - startDate; 
var decimalMilliseconds = diffMilliseconds/8.64e7 * 2.0e8; 

所以1標準毫秒= 2.314814814814815十進制毫秒

在日期函數,則表達式:

new Date(date.getUTCFullYear(), 0, 0) 

將創建爲上年12月31日的日期(即日期爲0),如果你是在1月1日之後,那麼它應該是:

new Date(date.getUTCFullYear(), 0, 1); 

很可能你有一天沒有。否則,代碼似乎是正確的。對我來說,得到小數時間函數是作爲簡單:

function getDecimalTime(date) { 
 
    // Pad numbers < 10 
 
    function z(n){return (n<10?'0':'')+n;} 
 
    // Copy date so don't modify original 
 
    var dayStart = new Date(+date); 
 
    var diffMs = date - dayStart.setUTCHours(0,0,0,0); 
 
    
 
    // Scale to decimal milliseconds 
 
    var decMs = Math.round(diffMs/8.64e7 * 2.0e8); 
 
    
 
    // Get decimal hours, etc. 
 
    var decHr = decMs/1.0e7 | 0; 
 
    var decMin = decMs % 1.0e7/1.0e5 | 0; 
 
    var decSec = decMs % 1.0e5/1.0e3 | 0; 
 
    decMs  = decMs % 1.0e3; 
 
    return z(decHr) + ':' + z(decMin) + ':' + z(decSec) + '.' + ('0' + z(decMs)).slice(-3); 
 
} 
 

 
// Helper to format the time part of date 
 
// as UTC hh:mm:ss.sss 
 
function formatUTCTime(date) { 
 
    function z(n){return (n<10?'0':'')+n;} 
 
    return z(date.getUTCHours()) + ':' + 
 
     z(date.getUTCMinutes()) + ':' + 
 
     z(date.getUTCSeconds()) + '.' + 
 
     ('00' + date.getUTCMilliseconds()).slice(-3); 
 
} 
 

 
// Test 00:00:00.003 => 00:00:00.007 
 
// i.e. 3ms * 2.31decms => 6.93decms 
 
var d = new Date(Date.UTC(2016,0,1,0,0,0,3)); 
 
console.log(getDecimalTime(d)); 
 

 
// Test 12:00:00.000 => 10:00:00.000 
 
// i.e. noon to decimal noon 
 
var d = new Date(Date.UTC(2016,0,1,12,0,0,0)); 
 
console.log(getDecimalTime(d)); 
 

 
// Test current time 
 
d = new Date(); 
 
console.log(formatUTCTime(d)); 
 
console.log(getDecimalTime(d));

+0

哇驚人的,這正是我所需要的。現在測試並開放源代碼:https://github.com/kmturley/decimal-time謝謝! –

+0

更新小提琴解決方案:https://jsfiddle.net/kmturley/7mrwc3x3/10/也基於Date.prototype,這使得它更容易! –