C和指針 第十六章 標準函數庫

字符串轉換:windows

long int strtol(char const *string, char **unused, int base);

將字符串轉換爲數值形式,遇到非法字符中止,若是stop不是NULL,則將轉換中止位置儲存在stop中。數組

#include <stdlib.h>
#include <stdio.h>

int main()
{
    //stop儲存轉換中止的位置的指針
    char *stop;
    printf("%ld\n", strtol("123abc", &stop, 10));
    printf("%s\n", stop);

    //若是字符串以0x開頭,base爲0或者16都會以16進制轉換
    printf("%ld\n", strtol("0x123", &stop, 16));
    printf("%s\n", stop);
    printf("%ld\n", strtol("0x123", &stop, 0));
    printf("%s\n", stop);

    //當base不是0和16遇到0x,爲非法字符
    printf("%ld\n", strtol("0x123", &stop, 10));
    printf("%s\n", stop);

    return 0;
}

運行:函數

日期和時間:指針

clock_t clock(void);

clock返回從秩序開始執行處理器所消耗的時間,一般是處理器時鐘的滴答次數,若是須要轉換成秒,須要除以常量 CLOCKS_PER_SECorm

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>


int main()
{
    clock_t start;
    clock_t end;
    //開始時間
    start = clock();
    //睡眠3000毫秒
    Sleep(3000);
    //結束時間
    end = clock();

    printf("%lu", (end - start) / CLOCKS_PER_SEC);
    return 0;
}

運行:blog

time_t time(time_t *returned_value);

不一樣編譯器有不一樣返回值,因此不能夠經過兩次相減得到時間差,通常默認返回從1970後到如今的秒數,若是須要時間差能夠經過difftime函數字符串

int main()
{
    time_t now;
    now = time(NULL);

    printf("%lu", now);
    return 0;
}

運行:編譯器

char *ctime(time_t const *time_value)

ctime函數的參數是一個指向time_t的指針,並返回一個指向字符串的指針string

struct tm *gmtime(time_t const *time_value)
struct tm *localtime(time_t const *time_value)

gmtime函數把時間值轉換爲世界協調時間,localtime轉換爲本地時間,struct tm結構包含時間的各個字段信息。io

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>


int main()
{
    time_t now;
    time_t end;
    char *time_s;
    //now爲1970年至今時間戳
    now = time(NULL);
    //time_s爲日期字符串格式
    time_s = ctime(&now);
    printf("%s %lu\n", time_s, now);

    //休眠一秒
    Sleep(1000);
    end = time(NULL);
    //difftime返回秒數時間差
    printf("between %g\n", difftime(end, now));

    //localtime轉換time_t值爲tm結構,tm結構包含日期和時間的各個組成部分
    struct tm *tm_time = localtime( &end);
    //tm結構體中月份以0爲開始, tm_year以1900爲起始年
    printf("%d %d %d\n", tm_time -> tm_mon + 1, tm_time -> tm_mday, tm_time -> tm_year + 1900);

    //gmtime轉換爲UTC時間
    tm_time = gmtime( &end);
    //tm結構體中月份以0爲開始, tm_year以1900爲起始年
    printf("%d %d %d\n", tm_time -> tm_mon + 1, tm_time -> tm_mday, tm_time -> tm_year + 1900);

    //asctime函數把tm參數所表示的時間轉換爲一個指定格式的字符串
    char *astime;
    astime = asctime(tm_time);
    printf("%s\n", astime);
    return 0;
}

運行:

函數strftime把一個tm結構轉換成一個根據某個格式字符串而指定的字符串。

size_t strftime(char *string, size_t maxsize, char const *format, struct tm const *tm_ptr);

若是轉換結果字符串長度小於maxsize參數,那麼該字符串就被複制到第一個參數指定的數組中。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
    char format_time[100];
    size_t maxsize = 100;
    char format[100] = "%Y-%m-%d %H:%M:%S";
    time_t now;
    now = time(NULL);
    struct tm *time_tm = localtime(&now);
    strftime(format_time, maxsize, format, time_tm);
    printf("%s", format_time);
    return 0;
}

運行:

localtime和gmtime能夠把time_t轉換成tm結構體個,而mktime能夠把tm結構體轉換成time_t值

time_t mktime(struct tm *tm_ptr)
相關文章
相關標籤/搜索