VxWorks 系统时间时区设置

设置VxWorks 系统时间的时区

VxWorks 系统中,设置TIMEZONE环境变量的值可以改变系统时间的时区

代码:putenv("TIMEZONE=CTT::-480:040102:100102");(将时区设置为北京时间,从412点到1012点为夏令时)

等于右边的字符串的意义:name_of_zone:(unused):time_in_minutes_from_UTC:daylight_start:daylight_end

(时区名:(未使用):标准时间与本地时间相差的分钟数:夏令时开始时间:夏令时的结束时间)


time.h中的常用函数

localtime

: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为tm类型的本地时间,

说明:此函数获得的tm结构体的时间是日历时间。

: struct tm*localtime(const time_t *clock);

返回值:返回指向tm 结构体指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.

 

gmtime

把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为tm类型的标准时间,与localtime的区别是,该函数转换后的时间没有经过时区变换,是UTC时间

time

得到从标准计时点(一般是197011日零点零分)到当前时间的秒数。

用法:time_t time(time_t* timer)

 

测试代码

#include 
#include 
#include 

int main(void)
{
    time_t td;
    struct timespec stime;
    struct tm * ptime;
    struct tm * ptime2;
    char *ptr;
    
    /*将时区设置为0时区*/
    putenv("TIMEZONE=UTC::0::");
    time(&td);
    printf("initial:UTC == LCOAL:\n");
    printf("    UTC time = %s", asctime(gmtime(&td)));
    printf("  Local time = %s", asctime(localtime(&td)));

    /*通过clock_gettime函数获取1970年1月1日零点零分到当前时间的秒数(还有纳米)*/
    if (clock_gettime(CLOCK_REALTIME, &stime) == -1 ) 
    {
       perror("getclock");
       return 0;
    }
    
    
    /*通过clock_settime函数将系统的时间增加一个小时*/
    printf("add one hour use clock_settime:\n");
    stime.tv_sec += 3600; 
    if (clock_settime(CLOCK_REALTIME, &stime) == -1) 
    {
       perror("setclock");
       return 0;
    }

    time(&td);
    printf("   time(&td) = %d\n", td);
    printf("    UTC time = %d\n", mktime(gmtime(&td)));
    printf("  Local time = %d\n", mktime(localtime(&td)));
    printf("    UTC time = %s", asctime(gmtime(&td)));
    printf("  Local time = %s", asctime(localtime(&td)));
    printf("\n\n");
    
    
    /*将时区设置为东8时区,及北京时间,无夏令时*/
    printf("set the time zone to beijin:\n");
    putenv("TIMEZONE=CTT::-480::");
    time(&td);
    if (clock_gettime(CLOCK_REALTIME, &stime) == -1 ) 
    {
       perror("getclock");
       return 0;
    }
    printf("     UTC time = %d\n", mktime(gmtime(&td)));
    printf("clock_gettime = %d\n", stime.tv_sec);
    printf("    time(&td) = %d\n", td);
    printf("   Local time = %d\n", mktime(localtime(&td)));
    ptime = localtime(&td);
    printf("  beijin sec time = %d", mktime(ptime));
    printf("  beijin time = %s", asctime(ptime));
    printf("  %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\n", 
            ptime->tm_year, ptime->tm_yday, ptime->tm_mon+1, ptime->tm_mday, ptime->tm_wday,
            ptime->tm_hour, ptime->tm_min, ptime->tm_sec, ptime->tm_isdst);

    
    /*将时区设置为东8时区,及北京时间,从4月1日2点到10月1日2点为夏令时*/
    printf("set Daylight:\n");
    putenv("TIMEZONE=CTT::-480:040102:100102");
    time(&td);
    if (clock_gettime(CLOCK_REALTIME, &stime) == -1 ) 
    {
       perror("getclock");
       return 0;
    }
    printf("     UTC time = %d\n", mktime(gmtime(&td)));
    printf("clock_gettime = %d\n", stime.tv_sec);
    printf("    time(&td) = %d\n", time(&td));
    printf("   Local time = %d\n", mktime(localtime(&td)));
    ptime2 = localtime(&td);
    if(1 == ptime2->tm_isdst)
    {
        printf("  beijin sec time = %d", mktime(ptime2));
        printf("  beijin time = %s", asctime(ptime2));
        printf("  %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\n", 
                ptime2->tm_year, ptime2->tm_yday, ptime2->tm_mon+1, ptime2->tm_mday, ptime2->tm_wday,
                ptime2->tm_hour, ptime2->tm_min, ptime2->tm_sec, ptime2->tm_isdst);
        
        /*检验tm_isdst的值是否影响时间的显示*/
        ptime2->tm_isdst = 0;
        printf("set tm_isdst to 0:\n");
        printf("  beijin sec time = %d", mktime(ptime2));
        printf("  beijin time = %s", asctime(ptime2));
        ptime2->tm_isdst = -1;
        printf("set tm_isdst to -1:\n");
        printf("  beijin sec time = %d", mktime(ptime2));
        printf("  beijin time = %s", asctime(ptime2));
    }
    printf("\n\n");
    
    
    /*将时区设置为东8时区,及北京时间,无夏令时*/
    printf("set no Daylight:\n");
    putenv("TIMEZONE=CTT::-480::");
    time(&td);
    ptime = localtime(&td);
    printf("  beijin sec time = %d", mktime(ptime));
    printf("  beijin time = %s", asctime(ptime));
    printf("  %d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d,\t%d\n", 
            ptime->tm_year, ptime->tm_yday, ptime->tm_mon+1, ptime->tm_mday, ptime->tm_wday,
            ptime->tm_hour, ptime->tm_min, ptime->tm_sec, ptime->tm_isdst);
    printf("\n\n");
    
    return 0;
}

2014年7月30日下午的运行结果

VxWorks 系统时间时区设置_第1张图片

你可能感兴趣的:(VxWorks 系统时间时区设置)