linux c 获取系统时间

//============================================================================
// Name        : gcc_001.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

//获取当前目录
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>  //包含了Linux C 中的函数getcwd()
#define FILEPATH_MAX (80)

#include <iostream>
using namespace std;

void getTime(char* datetime, int type);

int main() {

	const char date[512] = { 0 };
	char hostname[512];
	char *login;
	char *usershell;
	int pagesize;
	long int hotsid = gethostid();
	printf("%d,%d ok\n", getpid(), getuid());

	cout << get_current_dir_name() << endl;
	getdate(date);
	cout << date << endl;
	cout << getegid() << "," << geteuid() << endl;

	printf("%d\n", hotsid);

	gethostname(hostname, sizeof(hostname));

	printf("%s\n", hostname);

	login = getlogin();
	pagesize = getpagesize();
	usershell = getusershell();
	printf("%d\n", pagesize);
	printf("%s\n", usershell);
	//printf("%s\n",login);

	cout << "!!Hello World!!!" << endl; // prints !!!Hello World!!!
	int result = access("test.txt", 0);
	cout << result << endl;
	char *file_path_getcwd;
	file_path_getcwd = (char *) malloc(FILEPATH_MAX);
	getcwd(file_path_getcwd, FILEPATH_MAX);
	printf("%s\n", file_path_getcwd);

	char datetime[64];
	getTime(datetime, 0);
	printf("%s\n", datetime);
	getTime(datetime, 1);
	printf("%s\n", datetime);
	getTime(datetime, 2);
	printf("%s\n", datetime);
	getTime(datetime, 3);
	printf("%s\n", datetime);
	getTime(datetime, 4);
	printf("%s\n", datetime);

	return 0;
}
void getTime(char* datetime, int type) {

	time_t now; //实例化time_t结构
	struct tm *timenow; //实例化tm结构指针
	time(&now);
	//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
	timenow = localtime(&now);
	//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
	//printf("%s", asctime(timenow));
	switch (type) {
	//1990-06-09 12:15:12
	case 0:
		sprintf(datetime, "%d-%d-%d %d:%d:%d", timenow->tm_year + 1900,
				timenow->tm_mon + 1, timenow->tm_mday, timenow->tm_hour,
				timenow->tm_min, timenow->tm_sec);
		break;
	case 1:
		sprintf(datetime, "%d-%d-%d", timenow->tm_year + 1900,
				timenow->tm_mon + 1, timenow->tm_mday);
		break;
	case 2:
		sprintf(datetime, "%d:%d:%d", timenow->tm_hour, timenow->tm_min,
				timenow->tm_sec);
		break;
	case 3:
		sprintf(datetime, "%d/%d/%d %d:%d:%d", timenow->tm_year + 1900,
				timenow->tm_mon + 1, timenow->tm_mday, timenow->tm_hour,
				timenow->tm_min, timenow->tm_sec);
		break;
	case 4:
		sprintf(datetime, "%d/%d/%d", timenow->tm_year + 1900,
				timenow->tm_mon + 1, timenow->tm_mday);
		break;
	default:
		break;

	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值