C/C++基础 (time)

如何才能快速入坑 C/C++?曾经我也是抱着《C++ Primer 中文版》《Effective C++ 中文版》。但是对于初学者,它们并不是最好的选择,这里推荐《C/C++学习指南》一本非常适合入门的指导书。分享学习时候写的代码。

C:\Users\LAILAI\Desktop>g++ --version
g++.exe (GCC) 4.7.0 20111220 (experimental)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\Users\LAILAI\Desktop>g++ filename.cpp
// filename.cpp
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

time_t convert(int year, int month, int day, int hour, int minute, int second) {
	tm info = { 0 };
	info.tm_year  = year - 1900;
	info.tm_mon = month  - 1;
	info.tm_mday = day;
	info.tm_hour = hour;
	info.tm_min = minute;
	info.tm_sec = second;
	time_t t = mktime(&info);
	return t;
}

int main() {
	time_t now = time(NULL);
	printf("now: %d \n",(int) now); //1970-01-01 00:00:00  seconds: 1542110087

	tm info = *localtime(&now);
	int year = info.tm_year + 1900;
	int month = info.tm_mon + 1;
	int day = info.tm_mday;
	int hour = info.tm_hour;
	int minute = info.tm_min;
	int second = info.tm_sec;

	time_t start = convert(1994,1,10,0,0,0);
	time_t end = convert(2016,10,13,0,0,0);

	int diff = (int)(end - start);
	int days = diff / (24 * 3600);
	printf("%d\n", days);
	system( "PAUSE ");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值