2016-12-03 121 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,这使得它更容易! –