总秒数与时间的转换
wangxl@20180719
- 概述
单片机上为节使空间经常使用4个字节来存储时间的总秒数,上位机需要相应的处理解析秒数与时间的转换。
- 参考例程
package com.rcxt.utils;
import java.util.Calendar;
public class Sec2Time {
private final static int SecsPerComYear = 3153600;
private final static int SecsPerLeapYear = 31622400;
private final static int SecsPerFourYear = 126230400;
private final static int SecsPerDay = (3600*24);
private int Year_Secs_Accu[]={0,
31622400,
63158400,
94694400,
126230400};
private int Month_Secs_Accu_C[] = { 0,
2678400,
5097600,
7776000,
10368000,
13046400,
15638400,
18316800,
20995200,
23587200,
26265600,
28857600,
31536000};
private int Month_Secs_Accu_L[] = {0,
2678400,
5184000,
7862400,
10454400,
13132800,
15724800,
18403200,
21081600,
23673600,
26352000,
28944000,
31622400};
private int Month_Days_Accu_C[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
private int Month_Days_Accu_L[] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
//总秒数转成时间
public String sec2time(long TimeVar)
{
int TY = 0, TM = 1, TD = 0;
int Num4Y,NumY, OffSec, Off4Y = 0;
int i;
int NumDay; //OffDay;
int THH = 0, TMM = 0, TSS = 0;
if(TimeVar==0) return "";
Num4Y = (int)(TimeVar/SecsPerFourYear);
OffSec = (int)(TimeVar%SecsPerFourYear);
i=1;
while(OffSec > Year_Secs_Accu[i++])
Off4Y++;
/* Numer of Complete Year */
NumY = Num4Y*4 + Off4Y;
/* 2000,2001,...~2000+NumY-1 complete year before, so this year is 2000+NumY*/
TY = 2000+NumY;
OffSec = OffSec - Year_Secs_Accu[i-2];
/* Month (TBD with OffSec)*/
i=0;
if((TY%4)>0)
{// common year
while(OffSec > Month_Secs_Accu_C[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_C[i-2];
}
else
{// leap year
while(OffSec > Month_Secs_Accu_L[i++]);
TM = i-1;
OffSec = OffSec - Month_Secs_Accu_L[i-2];
}
/* Date (TBD with OffSec) */
NumDay = OffSec/SecsPerDay;
OffSec = OffSec%SecsPerDay;
TD = NumDay+1;
/* Compute hours */
THH = OffSec/3600;
/* Compute minutes */
TMM = (OffSec % 3600)/60;
/* Compute seconds */
TSS = (OffSec % 3600)% 60;
String str;
str = (TY)+"-"+(TM)+"-"+(TD );
str = str+" "+ (THH )+":"+(TMM )+":"+(TSS );
// System.out.println(str);
return str;
}
//当前时间转成总秒数
public long time2secs()
{
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
int LeapY;
int TotDays;
if(year==2000)
LeapY = 0;
else
LeapY = (year - 2000 -1)/4 +1;
int years = (year - 2000)-(LeapY);
if ((year%4)>0)
//common year
TotDays = LeapY*366 + years*365 + Month_Days_Accu_C[month] + (date-1);
else
//leap year
TotDays = LeapY*366 + years*365 + Month_Days_Accu_L[month] + (date-1);
long TotSeconds = TotDays*SecsPerDay + (hour*3600 + minute*60 + second);
return TotSeconds;
}
public static void main(String[] args) throws Exception
{
//当前时间秒数 CC 2C 72 21 2017-10-12 13:38:20
//byte data[] = {(byte)0xCC, (byte)0x2C, (byte)0x72, (byte)0x21};
//int secs = (((byte)data[0]&0xff)<<0)|(((byte)data[1]&0xff)<<8)|(((byte)data[2]&0xff)<<16)|(((byte)data[3]&0xff)<<24);
long secs;
String str;
Sec2Time sec2time=new Sec2Time();
secs = sec2time.time2secs();
str=sec2time.sec2time(secs);
System.out.println("总秒数="+secs);
System.out.println("时间="+str);
}
}
//输出:
//总秒数=585303983
//时间=2018-7-19 8:26:23