时区设置(北京时间:选择亚洲-上海)
方法一:通过修改文件系统
查看当前开发板“亚洲-上海”时区的路径:
root@imx6qsabresd:/usr/share/zoneinfo/Asia# ls
Dhaka Dubai Hong_Kong Karachi Shanghai Tokyo
系统通过读取/etc/localtime来设置当前时区,由下面可知当前设置的是世界时间,不是北京时间(世界时间偏移+8小时)
root@imx6qsabresd:/etc# ls -al localtime
lrwxrwxrwx 1 root root 29 Jan 1 1970 localtime -> /usr/share/zoneinfo/Universal
通过执行软链接命令(ln -s),使/etc/localtime文件中写入的是想要设置的时区的路径,比如:
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@imx6qsabresd:/etc# ls -al
lrwxrwxrwx 1 root root 33 Jun 3 10:37 localtime -> /usr/share/zoneinfo/Asia/Shanghai
此时,时区已经设置亚洲上海了(北京时间),用这个开发板中的localtime文件替换掉源码中/rootfs/etc/localtime文件即可,因为这个文件中的内容就是目标时区的路径。
方法二:通过shell命令
tzselect 命令也可以修改时区,但是需要系统支持这个命令。
方法三:通过库函数
setenv(“TZ”, “GMT+0”, 1);//将当前时区设置成标准区
setenv(“TZ”, “GMT-8”, 1); 表示的意思就是:将当前时区设置成东八区。需要注意的是,这里要写成GMT-8才是东八区,不是GMT+8,原因GMT表示时间的方式有点不同,它的格式是(GMT[+/-]offset), 如果目标时区是在东区,则是负的,否则是正的。意思就是本地时区加减多少(offset)才能得到标准时间。
(原文:https://blog.youkuaiyun.com/u013457167/article/details/79021913 )
时间相关的库函数和结构体
linux下常用时间类型有四种: time_t (long 类型)、 struct tm、 struct timeval 、 struct timespec
Linux下常用时间函数有:time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( )
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)
gmtime() 和 localtime() 将time_t 时间(时间戳、秒数)类型转换为tm结构体;
mktime() 将tm结构体转换为time_t时间类型;
gettimeofday() 获取系统时间戳-time_t时间类型,不需要输入tm结构体
asctime() 将struct tm转换为字符串形式;
各个函数的详细使用说明可以参考:https://www.cnblogs.com/chris-cp/p/3760000.html
获取时间戳(从1970-01-01 00:00:00到现在经历的秒数)
方法一:shell命令(C代码中不建议使用shell命令获取,而应使用库函数)
root@imx6qsabresd:~# date +%s
1562582564
方法二:库函数
int gettimeofday(struct timeval *tv, struct timezone *tz)
其中,头文件是
#include <sys/time.h>
结构体说明
struct timeval{
long int tv_sec; // 秒数
long int tv_usec; // 微秒数
}
timezone 参数若不使用则传入NULL即可,应用如下
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
int main(){
struct timeval tv;
gettimeofday(&tv,NULL);
printf("second:%ld\n",tv.tv_sec); //秒
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒
printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); //微秒
return 0;
}
方法三:库函数
输入 tm 时间结构体
time_t mktime(struct tm *) //typedef long time_t
struct tm tptr;
struct timeval tv;
tptr.tm_year = year - 1900;
tptr.tm_mon = month - 1;
tptr.tm_mday = day;
tptr.tm_hour = hour;
tptr.tm_min = min;
tptr.tm_sec = sec;
tv.tv_sec = mktime(&tptr); //mktime()返回1970-1-1到当前时间的秒数
tv.tv_usec = 0; //微秒
获取时间-年月日时分秒
unsigned char timebuf[20] = {0};
struct tm *t;
time_t tt;
time(&tt);
t = localtime(&tt);
snprintf(timebuf, 20, "%4d-%02d-%02d %02d:%02d:%02d",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
printf("timebuf=%s\r\n",timebuf);
结构体说明:
struct tm {
int tm_sec;/* 秒 - 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份 (从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 - 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 - 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负. */
}
设置时间
方法一:settimeofday
输入时间戳,通过settimeofday()函数来设置系统时间,这个函数设置的精度可以精确到微秒。
#include <time.h>
int settimeofday(const struct timeval *tv , const struct timezone *tz);
参数说明:
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
tz参数为时区,时区结构中tz_dsttime在linux中不支持,应该置为0,通常将参数tz设置为NULL,表示使用当前系统的时区。该函数是glib中的。
方法二:int stime(time_t *t)
输入时间戳即可设置时间,比如设置现在时间为2019年7月9日14点51分30秒
SetSysTime(2019,7,9,14,51,30);
void SetSysTime(int year, int mon, int days, int hour, int min, int sec){
time_t t; //存放时间戳(秒数)
struct tm timeNow = {0};
timeNow.tm_sec = sec; //0-60
timeNow.tm_min = min; //0-59
timeNow.tm_hour = hour; //0-23
timeNow.tm_mday = days; //1-31
timeNow.tm_mon = mon-1; //0-11
timeNow.tm_year = year-1900;
timeNow.tm_isdst = -1; //夏令时标志符,don't know:-1,off:0,on:1
t = mktime(&timeNow); //返回时间戳
stime(&t); //设置时间
}
本文介绍了如何在Linux系统中修改时区,包括通过修改文件系统、使用tzselect命令以及通过库函数实现。重点讲解了设置亚洲上海时区的两种方法,并列举了时间相关的库函数和结构体,如time_t、struct tm等。此外,还阐述了获取时间戳和设置系统时间的具体步骤。
613

被折叠的 条评论
为什么被折叠?



