前言:
这篇文章介绍 标准时间与秒单位,秒单位与标准时间的转换方式,这份代码一般用在嵌入式单片机里比较多,比如:设置RTC时钟的时间,从RTC里读取秒单位时间后,需要转换成标准时间显示。
下面列出的两个例子里,注释和思路都写了,思路和实现办法在代码注释里,
1. 时间转换-秒与标准时间的转换
1.时间转换(秒与标准时间的转换)
(1)函数1: 将秒单位时间转为标准时间。 --RTC实时时钟—>秒为单位—>每秒钟cnt++;
237562867493 -----xxxx年xx月xx日xx时xx分xx秒 星期x。
示例代码: (模拟电子钟)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#include <stdio.h> #include <string.h> //字符串处理 #include <Windows.h> //时间单位 int year,mon,mdeay,hour,min,t_sec; //闰年的月份 int mon_r[12]={31,29,31,30,31,30,31,31,30,31,30,31}; //平年的月份 int mon_p[12]={31,28,31,30,31,30,31,31,30,31,30,31}; unsigned int TimeToSec( int year, int mon, int mdeay, int hour, int min, int sec); void SecToTime(unsigned int sec); int main( void ) { //将标准时间转为秒单位时间 (设置时间) unsigned int time =TimeToSec(2018,6,6,16,40,20); while (1) { time ++; Sleep(1000); //睡眠1秒时间。 单位是ms SecToTime( time ); printf ( "%d-%d-%d %d:%d:%d\n" ,year,mon,mdeay,hour,min,t_sec); } return 0; } /* 函数功能: 判断平年和闰年 函数返回值: 1表示闰年 0表示平年 */ int GetYearStat( int year) { if ((year%4==0&&year%100!=0)||year%400==0) return 1; //闰年 return 0; //平年 } /* 将秒单位时间转为标准时间 时间基准点: 1970年1月1日0时0分0秒 思想: 减法 */ void SecToTime(unsigned int sec) { int i; year=1970; //基准年份 /*1. 计算过了多少年*/ while (sec>=365*24*60*60) //秒还够一年 { if (GetYearStat(year)) //闰年 { if (sec>366*24*60*60) { sec-=366*24*60*60; //减去一年 year++; //年份累加 } else { break ; } } else { sec-=365*24*60*60; //减去一年 year++; //年份累加 } } /*2. 计算过了多少月*/ mon=1; if (GetYearStat(year)) //闰年 { for (i=0; i<12; i++) { if (sec>=mon_r[i]*24*60*60) //够一个月 { sec-=mon_r[i]*24*60*60; //减去一个月 mon++; //增加一个月 } else break ; } } else { for (i=0; i<12; i++) { if (sec>=mon_p[i]*24*60*60) //够一个月 { sec-=mon_p[i]*24*60*60; //减去一个月 mon++; //增加一个月 } else break ; } } /*3. 计算过了多少天*/ mdeay=1; while (sec>=24*60*60) //判断是否够一天 { sec-=24*60*60; mdeay++; } /*4. 过了多少小时*/ hour=0; while (sec>=60*60) { sec-=60*60; hour++; } /*5. 过了多少分钟*/ min=0; while (sec>=60) { sec-=60; min++; } /*6. 过了多少秒*/ t_sec=sec; } /* 将标准时间转为秒单位时间 思路: 全程加法 时间基准点: 1970年1月1日0时0分0秒 返回值: 得到的秒单位时间 */ unsigned int TimeToSec( int year, int mon, int mdeay, int hour, int min, int sec) { int i; int sec_cnt=0; //记录秒单位的时间 /*1. 转换年*/ for (i=1970; i<year; i++) { if (GetYearStat(i)) //闰年 { sec_cnt+=366*24*60*60; } else { sec_cnt+=365*24*60*60; } } /*2. 转换月*/ for (i=0; i<mon-1; i++) { if (GetYearStat(year)) //闰年 { sec_cnt+=mon_r[i]*24*60*60; } else { sec_cnt+=mon_p[i]*24*60*60; } } /*3. 转换天数*/ sec_cnt+=(mdeay-1)*24*60*60; /*4. 转换小时*/ sec_cnt+=hour*60*60; /*5. 转换分钟*/ sec_cnt+=min*60; /*6. 转换秒*/ sec_cnt+=sec; return sec_cnt; //返回秒单位时间 } |
完整的代码(添加星期):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#include <stdio.h> #include <string.h> //字符串处理 #include <Windows.h> //时间单位 int year,mon,mdeay,hour,min,t_sec,week; //闰年的月份 int mon_r[12]={31,29,31,30,31,30,31,31,30,31,30,31}; //平年的月份 int mon_p[12]={31,28,31,30,31,30,31,31,30,31,30,31}; unsigned int TimeToSec( int year, int mon, int mdeay, int hour, int min, int sec); void SecToTime(unsigned int sec); int GetWeek(unsigned int sec); int main( void ) { //将标准时间转为秒单位时间 (设置时间) unsigned int time =TimeToSec(2018,9,1,16,40,20); while (1) { time ++; Sleep(1000); //睡眠1秒时间。 单位是ms SecToTime( time ); week=GetWeek( time ); //获取星期 printf ( "%d-%d-%d %d:%d:%d 星期%d\n" ,year,mon,mdeay,hour,min,t_sec,week); } return 0; } /* 函数功能: 判断平年和闰年 函数返回值: 1表示闰年 0表示平年 */ int GetYearStat( int year) { if ((year%4==0&&year%100!=0)||year%400==0) return 1; //闰年 return 0; //平年 } /* 将秒单位时间转为标准时间 时间基准点: 1970年1月1日0时0分0秒 思想: 减法 */ void SecToTime(unsigned int sec) { int i; year=1970; //基准年份 /*1. 计算过了多少年*/ while (sec>=365*24*60*60) //秒还够一年 { if (GetYearStat(year)) //闰年 { if (sec>366*24*60*60) { sec-=366*24*60*60; //减去一年 year++; //年份累加 } else { break ; } } else { sec-=365*24*60*60; //减去一年 year++; //年份累加 } } /*2. 计算过了多少月*/ mon=1; if (GetYearStat(year)) //闰年 { for (i=0; i<12; i++) { if (sec>=mon_r[i]*24*60*60) //够一个月 { sec-=mon_r[i]*24*60*60; //减去一个月 mon++; //增加一个月 } else break ; } } else { for (i=0; i<12; i++) { if (sec>=mon_p[i]*24*60*60) //够一个月 { sec-=mon_p[i]*24*60*60; //减去一个月 mon++; //增加一个月 } else break ; } } /*3. 计算过了多少天*/ mdeay=1; while (sec>=24*60*60) //判断是否够一天 { sec-=24*60*60; mdeay++; } /*4. 过了多少小时*/ hour=0; while (sec>=60*60) { sec-=60*60; hour++; } /*5. 过了多少分钟*/ min=0; while (sec>=60) { sec-=60; min++; } /*6. 过了多少秒*/ t_sec=sec; } |
2. 时间转换-标准时间转秒
(2)函数2: 将标准时间转为秒单位的时间。
2018年6月1日19点41分23秒----------xxxxxxx秒
闰年366,平年365。 区分: 每年二月份相差一天.
标准时间基准点: 1970年1月1日0时0分0秒。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/* 将标准时间转为秒单位时间 思路: 全程加法 时间基准点: 1970年1月1日0时0分0秒 返回值: 得到的秒单位时间 */ unsigned int TimeToSec( int year, int mon, int mdeay, int hour, int min, int sec) { int i; int sec_cnt=0; //记录秒单位的时间 /*1. 转换年*/ for (i=1970; i<year; i++) { if (GetYearStat(i)) //闰年 { sec_cnt+=366*24*60*60; } else { sec_cnt+=365*24*60*60; } } /*2. 转换月*/ for (i=0; i<mon-1; i++) { if (GetYearStat(year)) //闰年 { sec_cnt+=mon_r[i]*24*60*60; } else { sec_cnt+=mon_p[i]*24*60*60; } } /*3. 转换天数*/ sec_cnt+=(mdeay-1)*24*60*60; /*4. 转换小时*/ sec_cnt+=hour*60*60; /*5. 转换分钟*/ sec_cnt+=min*60; /*6. 转换秒*/ sec_cnt+=sec; return sec_cnt; } /* 函数功能: 根据秒单位时间获取星期 函数形参: 秒单位时间 返回值 :星期(1~7) */ int GetWeek(unsigned int sec) { int mdeay=sec/60/60/24; //将秒单位时间转为天数 switch (mdeay%7) { case 0: //星期4 return 4; break ; case 1: //星期5 return 5; break ; case 2: //星期6 return 6; break ; case 3: //星期天 return 7; break ; case 4: //星期1 return 1; break ; case 5: //星期2 return 2; break ; case 6: //星期3 return 3; break ; default : break ; } } |
到此这篇关于C语言标准时间与秒单位相互转换的文章就介绍到这了,更多相关C语言时间转换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.51cto.com/u_11822586/5288630