以数学方式从unix-timestamp计算日数? [英] Calculate day number from an unix-timestamp in a math way?

查看:113
本文介绍了以数学方式从unix-timestamp计算日数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1313905026 - > 8如何从unix-timestamp中计算日期数字,以数学方式计算,而不使用任何函数和简单的数学公式。 (今天08/21/2011)

解决方案

没有简单的公式来做到这一点。你需要减去从时代以来的年数(占闰年),这可能需要一个循环或某种离散的计算。然后使用某种类型的循环来减去当前年份每个月的秒数。剩下的是目前进入该月的秒数。



我会做这样的事情。

  x = ... //秒数
年= 1970

while(x> / *一年* /){一月份的
x = x - / *秒,二月的十二月* /
如果(年%4 == 0){
x - = / * leapeay秒在二月* /
} else {
x - = / *二月的常规秒* /
}
}

//这样的东西:

if(x> / * seconds in january * /){
x - = / * seconds in january * /
}
if(x> / * seconds in february * /)一月份的{
x - = / *秒* /
}





//之后,只需从x秒获取天数,然后设置即可。

编辑



我建议使用日期功能简单,但是这是一个可能的非循环的替代答案,以防任何人需要它,或者将会进一步发展。



让t是从时代以来的当前时间(以秒为单位)。



让F是四年的秒数。那是三年和一个闰年。这应该是:126230400。



现在如果你把所有的时间都带走了,你会得到一个余数:y。



所以y = n%F。



现在有几种情况:
1. y少于一年
2. y不到两年
3. y不到三年,少于两个月
4. y不到三年,大于两个月
5. y不到四年



请注意,1972年是一个闰年,所以如果你从1970年算起了四岁,那你离开的地方将是两年的闰年



让jan,feb,febLY,mar,may,...,dec是每个月的秒数(你需要计算出来)



d表示当前月份的日数,D表示一天中的秒数(86400)。
y表示常规年份的秒数,yLY表示闰年的秒数。

  y =(t%F)
if(y< Y){
if(y> jan){
y - = jan
}
if(y> feb){
y - = feb
}



d = y%D
}
else if(y< 2 * y){
y = y - Y
if(y> jan){
y - = jan
}
if(y> feb){
y - = feb
}



d = y%D
}
else if(y< 2 * y + yLY){
y = y - 2 * Y
if(y> jan ){
y - = jan
}
if(y> febLY){
y - = febLY
}



d = y%D
}
else {
y = y - 2 * Y - yLY
if(y> jan){
y - = jan
}
if(y> feb){
y - = feb
}



d = y%D
}

未测试。此外,由于地球不能以1轮/ 24小时旋转,所以偶尔会调整时间。你需要做一些研究因素,在。


How can i calculate day number from a unix-timestamp, in a mathematical way and without using any functions and in simple math formula.

1313905026 --> 8 (Today 08/21/2011)

解决方案

There is no simple formula to do this. You would need to subtract the number of years (accounting for leap years) since the epoch, which would probably require a loop or a discrete calculation of some kind. Then use some type of loop to subtract out the number of seconds in each month for the current year. What you are left with is the number of seconds currently into the month.

I would do something like this.

x = ...//the number of seconds
year = 1970

while (x > /*one year*/){
 x = x - /*seconds in january, and march-december*/
 if(year % 4 == 0){
  x -= /*leapeay seconds in february*/
 }else{
  x -= /*regular seconds in february*/
 }
}

//Then something like this:

if(x > /*seconds in january*/){
 x -= /*seconds in january*/
}
if(x > /*seconds in february*/){
 x -= /*seconds in january*/
}

.
.
.

//After that just get the number of days from x seconds and you're set.

Edit

I recommend using date functions for simplicity, but here is a possible non-loopy alternative answer in case anyone needs it, or would care to develop it further.

First let t be the current time in seconds since the epoch.

Let F be the number of seconds in four years. That is three regular years and one leap year. That should be: 126230400.

Now if you take away all of the time contributed by F, you will get a remainder: y.

So y = n % F.

There are several cases now: 1. y is less that one year 2. y is less than two years 3. y is less than three years and less than two months 4. y is less than three years and greater than two months 5. y is less than four years

Note that 1972 was a leap year, so if you count up by four from 1970, wherever you left off will be a leap year in two years.

let jan, feb, febLY, mar, may, ..., dec be the number of seconds in each month (you'd need to calculate it out).

d represents the day number of the current month and D represents the number of seconds in a day (86400). y represents the number of seconds in a regular year, and yLY represents the number of seconds in a leap year.

y = (t % F)
if(y < Y){
 if(y > jan){
  y -= jan
 }
 if(y > feb){
  y -= feb
 }
 .
 .
 .
 d = y % D
}
else if(y < 2 * y){
 y = y - Y
 if(y > jan){
  y -= jan
 }
 if(y > feb){
  y -= feb
 }
 .
 .
 .
 d = y % D
}
else if(y < 2 * y + yLY){
 y = y - 2 * Y
 if(y > jan){
  y -= jan
 }
 if(y > febLY){
  y -= febLY
 }
 .
 .
 .
 d = y % D
}
else{
 y = y - 2 * Y - yLY
 if(y > jan){
  y -= jan
 }
 if(y > feb){
  y -= feb
 }
 .
 .
 .
 d = y % D
}

Not tested. Also, since the Earth doesn't spin at EXACTLY 1 rotation / 24 hours, they've occasionally made adjustments to time. You need to do a bit of research factor that in.

这篇关于以数学方式从unix-timestamp计算日数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆