wangzhengquan
2020-06-12 21afe7a2024b0158904ebe08a9d3f5f914792484
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
#define _POSIX_C_SOURCE 200112L // 为 GCC 的 setenv
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <libgen.h>
 
int main(void)
{    
    char *strFileName = "/home/basic/wk/basic/test/test_time.c";
    char *dir = dirname(strdup(strFileName) );
    printf("dir = %s\n", dir);
 
    char *dirc, *basec, *bname, *dname;
    char *path = "/home/basic/wk/basic/test/test_time.c";
 
    dirc = strdup(path);
    basec = strdup(path);
    dname = dirname(dirc);
    bname = basename(basec);
    printf("dirname=%s, basename=%s\n", dname, bname);
    
    // setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX 限定
 
    // struct tm tm = *localtime(&(time_t){time(NULL)});
    // printf("%d\n", tm.tm_year);
    // printf("Today is           %s", asctime(&tm));
    // printf("(DST is %s)\n", tm.tm_isdst ? "in effect" : "not in effect");
    // tm.tm_mon -= 100;  // tm_mon 落在正常范围外
    // mktime(&tm);       // tm_dst 不被设为 -1 ;使用今日的 DST 状态
    // printf("100 months ago was %s", asctime(&tm));
    // printf("(DST was %s)\n", tm.tm_isdst ? "in effect" : "not in effect");
}