字符串转换:

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_SEC

#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;
}

运行:

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的指针,并返回一个指向字符串的指针

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

gmtime函数把时间值转换为世界协调时间,localtime转换为本地时间,struct tm结构包含时间的各个字段信息。

#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)

C和指针 第十六章 标准函数库的更多相关文章

  1. C和指针 第十六章 标准函数库 信号

    信号名<signal.h> 程序中大多数错误都是程序本身导致的,但是,有些程序遇到的事件却不是程序本身所引发的.比如用户终止程序,程序无法预知此类事件发生的情况,信号就是为了对此类事件做出 ...

  2. C和指针 第十六章 标准函数库 本地跳转setjmp.h

    setjmp和longjmp提供一种类似goto语句的机制,但它的作用域不局限于同一个函数的作用域之内.这些函数可以用于深层次的嵌套函数调用链. int setjmp(jmp_buf state); ...

  3. C和指针 第十六章 习题

    16.8 计算平均年龄 #include <stdlib.h> #include <stdio.h> #define MAX_LEN 512 int main() { int ...

  4. UNP学习笔记(第二十六章 线程)

    线程有时称为轻权进程(lightweight process) 同一进程内的所有线程共享相同的全局内存.这使得线程之间易于共享信息,然后这样也会带来同步的问题 同一进程内的所有线程处理共享全局变量外还 ...

  5. 【C++】《C++ Primer 》第十六章

    第十六章 模板与泛型编程 面向对象编程和泛型编程都能处理在编写程序时不知道类型的情况. OOP能处理类型在程序允许之前都未知的情况. 泛型编程在编译时就可以获知类型. 一.定义模板 模板:模板是泛型编 ...

  6. 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记

    第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...

  7. Gradle 1.12 翻译——第十六章. 使用文件

    有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...

  8. 第十六章——处理锁、阻塞和死锁(3)——使用SQLServer Profiler侦测死锁

    原文:第十六章--处理锁.阻塞和死锁(3)--使用SQLServer Profiler侦测死锁 前言: 作为DBA,可能经常会遇到有同事或者客户反映经常发生死锁,影响了系统的使用.此时,你需要尽快侦测 ...

  9. CSS3秘笈复习:十三章&十四章&十五章&十六章&十七章

    第十三章 1.在使用浮动时,源代码的顺序非常重要.浮动元素的HTML必须处在要包围它的元素的HTML之前. 2.清楚浮动: (1).在外围div的底部添加一个清除元素:clear属性可以防止元素包围浮 ...

随机推荐

  1. ArrayList<E>源码分析

    ArrayList是按照线性表结构实现的 ArrayList的主要继承结构 public class ArrayList<E> extends AbstractList<E> ...

  2. sql语句返回值的问题

    由于执行sql语句的时候执行成功或者失败会返回执行的影响函数,用list是因为查询的结果可能为null也可能set后放到集合里去: 所以返回值类型用int

  3. mybatis generator 自动生成dao层映射代码

    资源: doc url :http://www.mybatis.org/generator/ download:https://github.com/mybatis/generator/release ...

  4. github如何删除一个(repository)仓库

    GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本库格式进行托管,故名 GitHub.作为开源代码库以及版本控制系统,Github拥有140多万开发者用户.随着越 ...

  5. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. C++知识回顾(一)

    感觉世界都是约定好的,每门语言的第一个程序总是Hello World!但是也有一些书似乎是在追求个性,会用一些其他的,但是是Not Hello World!本人需要再学习一下C++,所以从最基础的开始 ...

  7. 最全的 JavaScript 知识总结

    来源于:http://gcdn.gcpowertools.com.cn/showtopic-28404-1-3.html?utm_source=gold.xitu.io&utm_medium= ...

  8. Web Deploy自动配置

    自动发布配置,需要在发布的配置文件里面添加以下一句,避免在发布时,无权限! <Project ToolsVersion="4.0" xmlns="http://sc ...

  9. replace和translate的用法

    select replace ('111222333444','222','888') from dual;with tmp as(select 'aabb/123\:cde工人' s from du ...

  10. Uncaught SyntaxError: Invalid or unexpected token

    出现错误的地方:在Jquery中,调用有参数的方法,动态传递参数时报错 出现错误的原因: 动态传递参数的时候,参数中有换行符 错误的解决:参数传递之前,将换行符替换 var  temp = model ...