c++ 时间与日期

本文深入探讨了C++11标准库中关于随机数生成和时间操作的功能,包括绝对值函数、字符串转数值函数、随机数生成器初始化、随机数产生以及获取当前时间等常用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

随机数及atoi

注意C++11有更强大的方法!

//随机数及atoi 
#include
int abs( int num );//功能: 函数返回参数num.的绝对值。
double atof( const char *str );//功能:将字符串str转换成一个双精度数值并返回结果。
int atoi( const char *str );//功能:将字符串str转换成一个整数并返回结果。
void   srand(unsigned   seed);//   初始化随机数发生器     
int   rand();//   产生一个随机数并返回这个数

time相关

#include <iostream>
using namespace std;
 
time_t getMorningTime() 
{  
    time_t t = time(NULL); 
	cout<<t<<endl;
	cout<<timezone<<endl;
	// GMT+0
    struct tm * tm= localtime(&t); 
	cout<<tm->tm_hour<<endl;
    tm->tm_hour = 0;  
    tm->tm_min = 0;  
    tm->tm_sec = 0;  
	// 转为 GMT+8
    return  mktime(tm);  
}  
 
int main()
{
	//cout<<_timezone ;
	cout << getMorningTime() << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值