首页 > 其他分享 >时区与时间

时区与时间

时间:2022-12-16 09:34:23浏览次数:54  
标签:struct int tv 时间 时区 now settimeofday 函数

一. 网址参考

  1. GMT、UTC、DST、CST时区代表的意义

  2. How to Set the Time Zone in Linux

  3. 关于GMT UTC CST和Linux时区设置 

  4. setenv环境变量函数

  5. settimeofday()--Set System Clock

 

二. 实践

1. setenv函数:

  通过此函数并不能添加或修改 shell 进程的环境变量,或者说通过setenv函数设置的环境变量只在本进程,而且是本次执行中有效。如果在某一次运行程序时执行了setenv函数,进程终止后再次运行该程序,上次的设置是无效的,上次设置的环境变量是不能读到的。

2. settimeofday函数:通过秒值,修改系统的UTC时间; 

函数原型:
复制代码
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 */
};
 int settimeofday (struct timeval *tp,
                  struct timezone *tzp);
复制代码

  示例:

复制代码
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct timeval now;
    int rc;

    now.tv_sec=866208142;
    now.tv_usec=290944;

    rc=settimeofday(&now, NULL);
    if(rc==0) {
        printf("settimeofday() successful.\n");
    }
    else {
        printf("settimeofday() failed, "
        "errno = %d\n",errno);
        return -1;
    }

    return 0;
}
Example Output:
settimeofday() successful.
复制代码

标签:struct,int,tv,时间,时区,now,settimeofday,函数
From: https://www.cnblogs.com/shanyu20/p/16986520.html

相关文章