在Linux中,使用time(NULL)函数和localtime获取实时日期的技巧
最编程
2024-08-08 17:35:54
...
time(); 函数
函数原型: time_t time(time_t *timer) 函数用途: 得到机器的日历时间或者设置日历时间 头 文 件: time.h 输入参数: timer=NULL时,得到机器日历时间, =时间数值时 用于设置日历时间; time_t是一个long类型
/* time - 获取计算机系统当前的日历时间(Calender Time)
* 处理日期时间的函数都是以本函数的返回值为基础进行运算
*
* 函数原型:
* #include <time.h>
*
* time_t time(time_t *calptr);
*
* 返回值:
* 成功:秒数,从1970-1-1,00:00:00
*
* 使用:
* time_t now;
*
* time(&now); // == now = time(NULL);
*/
localtime();函数
函数原型: struct tm *localtime(const time_t *timer) 函数用途: 返回一个以tm结构表达的机器时间信息 头 文 件: time.h 输入参数: timer:使用time()函数获得的机器时间;
/*
* localtime - 将时间数值变换成本地时间,考虑到本地时区和夏令时标志
*
* 函数声明:
* #include <time.h>
*
* struct tm * localtime(const time_t *timer);
*
*/
//结构tm的定义为:
struct tm
{
int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */
int tm_min; /* Minutes: 0-59 */
int tm_hour; /* Hours since midnight: 0-23 */
int tm_mday; /* Day of the month: 1-31 */
int tm_mon; /* Months *since* january: 0-11 */
int tm_year; /* Years since 1900 */
int tm_wday; /* Days since Sunday (0-6) */
int tm_yday; /* Days since Jan. 1: 0-365 */
int tm_isdst; /* +1 Daylight Savings Time, 0 No DST,
* -1 don't know */
};
既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。
/*
* time();
* @author 李政 <1244109467@qq.com>
*/
#include <time.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
struct tm *tp;
time_t t = time(NULL);
tp = localtime(&t);
printf("%d/%d/%d\n",tp->tm_mon+1,tp->tm_mday,tp->tm_year+1900);
printf("%d:%d:%d\n",tp->tm_hour,tp->tm_min,tp->tm_sec);
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助。
推荐阅读
-
在Linux中,使用time(NULL)函数和localtime获取实时日期的技巧
-
包婷婷 (201550484)作业一 统计软件简介与数据操作-SPSS(Statistical Product and Service Solutions),"统计产品与服务解决方案"软件。最初软件全称为"(SolutionsStatistical Package for the Social Sciences),但是随着SPSS产品服务领域的扩大和服务深度的增加,SPSS公司已于2000年正式将英文全称更改为"统计产品与服务解决方案",标志着SPSS的战略方向正在做出重大调整。为IBM公司推出的一系列用于统计学分析运算、数据挖掘、预测分析和决策支持任务的软件产品及相关服务的总称SPSS,有Windows和Mac OS X等版本。 1984年SPSS总部首先推出了世界上第一个统计分析软件微机版本SPSS/PC+,开创了SPSS微机系列产品的开发方向,极大地扩充了它的应用范围,并使其能很快地应用于自然科学、技术科学、社会科学的各个领域。世界上许多有影响的报刊杂志纷纷就SPSS的自动统计绘图、数据的深入分析、使用方便、功能齐全等方面给予了高度的评价。 R统计软件介绍 R是一套完整的数据处理、计算和制图软件系统。其功能包括:数据存储和处理系统;数组运算工具(其向量、矩阵运算方面功能尤其强大);完整连贯的统计分析工具;优秀的统计制图功能;简便而强大的编程语言:可操纵数据的输入和输出,可实现分支、循环,用户可自定义功能。 与其说R是一种统计软件,还不如说R是一种数学计算的环境,因为R并不是仅仅提供若干统计程序、使用者只需指定数据库和若干参数便可进行一个统计分析。R的思想是:它可以提供一些集成的统计工具,但更大量的是它提供各种数学计算、统计计算的函数,从而使使用者能灵活机动的进行数据分析,甚至创造出符合需要的新的统计计算方法。 该语言的语法表面上类似 C,但在语义上是函数设计语言(functional programming language)的变种并且和Lisp 以及 APL有很强的兼容性。特别的是,它允许在"语言上计算"(computing on the language)。这使得它可以把表达式作为函数的输入参数,而这种做法对统计模拟和绘图非常有用。 R是一个免费的*软件,它有UNIX、LINUX、MacOS和WINDOWS版本,都是可以免费下载和使用的。在R主页那儿可以下载到R的安装程序、各种外挂程序和文档。在R的安装程序中只包含了8个基础模块,其他外在模块可以通过CRAN获得。 二、R语言 R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个*、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。 R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX,Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R还有以下特点: 1.R是*软件。这意味着它是完全免费,开放源代码的。可以在它的网站及其镜像中下载任何有关的安装程序、源代码、程序包及其源代码、文档资料。标准的安装文件身自身就带有许多模块和内嵌统计函数,安装好后可以直接实现许多常用的统计功能。[2] 2.R是一种可编程的语言。作为一个开放的统计编程环境,语法通俗易懂,很容易学会和掌握语言的语法。而且学会之后,我们可以编制自己的函数来扩展现有的语言。这也就是为什么它的更新速度比一般统计软件,如,SPSS,SAS等快得多。大多数最新的统计方法和技术都可以在R中直接得到。[2] 3. 所有R的函数和数据集是保存在程序包里面的。只有当一个包被载入时,它的内容才可以被访问。一些常用、基本的程序包已经被收入了标准安装文件中,随着新的统计分析方法的出现,标准安装文件中所包含的程序包也随着版本的更新而不断变化。在另外版安装文件中,已经包含的程序包有:base一R的基础模块、mle一极大似然估计模块、ts一时间序列分析模块、mva一多元统计分析模块、survival一生存分析模块等等.[2] 4.R具有很强的互动性。除了图形输出是在另外的窗口处,它的输入输出窗口都是在同一个窗口进行的,输入语法中如果出现错误会马上在窗口口中得到提示,对以前输入过的命令有记忆功能,可以随时再现、编辑修改以满足用户的需要。输出的图形可以直接保存为JPG,BMP,PNG等图片格式,还可以直接保存为PDF文件。另外,和其他编程语言和数据库之间有很好的接口。[2] 5.如果加入R的帮助邮件列表一,每天都可能会收到几十份关于R的邮件资讯。可以和全球一流的统计计算方面的专家讨论各种问题,可以说是全世界最大、最前沿的统计学家思维的聚集地.[2] R是基于S语言的一个GNU项目,所以也可以当作S语言的一种实现,通常用S语言编写的代码都可以不作修改的在R环境下运行。 R的语法是来自Scheme。R的使用与S-PLUS有很多类似之处,这两种语言有一定的兼容性。S-PLUS的使用手册,只要稍加修改就可作为R的使用手册。所以有人说:R,是S-PLUS的一个“克隆”。 但是请不要忘了:R是免费的(R is free)。R语言源代码托管在github,具体地址可以看参考资料。[3] 。 R语言的下载可以通过CRAN的镜像来查找。 R语言有域名为.cn的下载地址,有六个,其中两个由Datagurn,由 中国科学技术大学提供的。R语言Windows版,其中由两个下载地点是Datagurn和 USTC提供的。 三、stata Stata 是一套提供其使用者数据分析、数据管理以及绘制专业图表的完整及整合性统计软件。它提供许许多多功能,包含线性混合模型、均衡重复反复及多项式普罗比模式。用Stata绘制的统计图形相当精美。 新版本的STATA采用最具亲和力的窗口接口,使用者自行建立程序时,软件能提供具有直接命令式的语法。Stata提供完整的使用手册,包含统计样本建立、解释、模型与语法、文献等超过一万余页的出版品。 除此之外,Stata软件可以透过网络实时更新每天的最新功能,更可以得知世界各地的使用者对于STATA公司提出的问题与解决之道。使用者也可以透过Stata. Journal获得许许多多的相关讯息以及书籍介绍等。另外一个获取庞大资源的管道就是Statalist,它是一个独立的listserver,每月交替提供使用者超过1000个讯息以及50个程序。 四、PYTHON
-
= 0; i--) {
printf("%5d", *(p + i));
}
printf("\n");
return 0;
}
```
2. 利用指针计算奇数索引数组元素之和
```c
// 使用指针计算奇数索引数组元素之和
#include
int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(arr[0]); printf("数组元素: "); for (int i = 0; i < n; i++) { printf("%3d", arr[i]); // 宽度为3,右对齐 } printf("\n"); printf("数组下标: "); for (int i = 0; i < n; i++) { printf("%3d", i); // 宽度为3,右对齐 } printf("\n"); int sum = 0; int *p = arr; // 声明指向整数的指针 for (int i = 0; i < n; i++) { if (i % 2 != 0) { sum += *(p + i); // 若索引i为奇数,累加对应元素值 } } printf("奇数索引数组元素之和: %d\n", sum); return 0; } ``` 3. 查看不同类型指针在Linux Ubuntu gcc下的字节大小 ```c // 在Linux Ubuntu gcc环境下查看不同类型指针的字节大小 #include #include int main() { printf("int指针大小: %zu bytes\n", sizeof(int *)); printf("char指针大小: %zu bytes\n", sizeof(char *)); printf("float指针大小: %zu bytes\n", sizeof(float *)); printf("double指针大小: %zu bytes\n", sizeof(double *)); return 0; } ``` 请注意,对于现代64位系统(如Linux),`int`、`char`、`float`和`double`指针通常都是8字节。但在某些特定情况下(如32位系统或特定编译环境),可能有所不同。 4. 使用指针遍历并打印字符数组 ```c // 使用指针遍历并打印字符数组 #include #include // 引入strlen函数 int main() { char str[] = "Hello, World"; // 字符串常量 int len = strlen(str); // 获取字符串长度 char *ptr = str; // 定义指向字符的指针 printf("输出字符串: %s\n", str); // 输出原始字符串 printf("遍历并打印字符数组: "); for (int i = 0; i < len; i++) { printf("%c", *ptr); // 输出当前字符 ptr++; // 移动指针到下一个字符 } printf("\n"); return 0; } ``` 对于用户自定义输入字符串的情况,请参考以下代码: ```c #include #include int main() { char input[100]; // 用于存放用户输入的字符串,假设最大长度为100 printf("请输入字符串: "); fgets(input, sizeof(input), stdin); // 从标准输入读取字符串,fgets会保留换行符 int len = strlen(input) - 1; // 去掉fgets读取的换行符 char *ptr = input; // 定义指向字符的指针 printf("输入的字符串: %s\n", input); printf("遍历并打印字符数组: "); for (int i = 0; i < len; i++) { printf("%c", *ptr); // 输出当前字符 ptr++; // 移动指针到下一个字符 } printf("\n"); return 0; } ```"> 1. 使用指针倒序显示整数数组内容 ```c // 通过指针逆序打印整数数组 #include
#include #include int main() { int arr[10] = {0}; int n = sizeof(arr) / sizeof(arr[0]); srand(time(NULL)); printf("原始数组: "); for (int i = 0; i < n; i++) { arr[i] = rand() % 100; printf("%5d", arr[i]); } printf("\n"); int *p = arr; // 指针指向数组元素 printf("反转打印数组元素: "); for (int i = n - 1; i >= 0; i--) { printf("%5d", *(p + i)); } printf("\n"); return 0; } ``` 2. 利用指针计算奇数索引数组元素之和 ```c // 使用指针计算奇数索引数组元素之和 #include int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = sizeof(arr) / sizeof(arr[0]); printf("数组元素: "); for (int i = 0; i < n; i++) { printf("%3d", arr[i]); // 宽度为3,右对齐 } printf("\n"); printf("数组下标: "); for (int i = 0; i < n; i++) { printf("%3d", i); // 宽度为3,右对齐 } printf("\n"); int sum = 0; int *p = arr; // 声明指向整数的指针 for (int i = 0; i < n; i++) { if (i % 2 != 0) { sum += *(p + i); // 若索引i为奇数,累加对应元素值 } } printf("奇数索引数组元素之和: %d\n", sum); return 0; } ``` 3. 查看不同类型指针在Linux Ubuntu gcc下的字节大小 ```c // 在Linux Ubuntu gcc环境下查看不同类型指针的字节大小 #include #include int main() { printf("int指针大小: %zu bytes\n", sizeof(int *)); printf("char指针大小: %zu bytes\n", sizeof(char *)); printf("float指针大小: %zu bytes\n", sizeof(float *)); printf("double指针大小: %zu bytes\n", sizeof(double *)); return 0; } ``` 请注意,对于现代64位系统(如Linux),`int`、`char`、`float`和`double`指针通常都是8字节。但在某些特定情况下(如32位系统或特定编译环境),可能有所不同。 4. 使用指针遍历并打印字符数组 ```c // 使用指针遍历并打印字符数组 #include #include // 引入strlen函数 int main() { char str[] = "Hello, World"; // 字符串常量 int len = strlen(str); // 获取字符串长度 char *ptr = str; // 定义指向字符的指针 printf("输出字符串: %s\n", str); // 输出原始字符串 printf("遍历并打印字符数组: "); for (int i = 0; i < len; i++) { printf("%c", *ptr); // 输出当前字符 ptr++; // 移动指针到下一个字符 } printf("\n"); return 0; } ``` 对于用户自定义输入字符串的情况,请参考以下代码: ```c #include #include int main() { char input[100]; // 用于存放用户输入的字符串,假设最大长度为100 printf("请输入字符串: "); fgets(input, sizeof(input), stdin); // 从标准输入读取字符串,fgets会保留换行符 int len = strlen(input) - 1; // 去掉fgets读取的换行符 char *ptr = input; // 定义指向字符的指针 printf("输入的字符串: %s\n", input); printf("遍历并打印字符数组: "); for (int i = 0; i < len; i++) { printf("%c", *ptr); // 输出当前字符 ptr++; // 移动指针到下一个字符 } printf("\n"); return 0; } ```