时间操作c语言,时间操作c语言编程

发布时间:2023-01-07

本文目录一览:

  1. C语言如何获取本地时间,然后取时、分、秒的值?
  2. c语言中时间处理
  3. C语言中的时间

C语言如何获取本地时间,然后取时、分、秒的值?

C语言有2个获取时间的函数,分别是time()localtime()time()函数返回unix时间戳-即从1970年1月1日0:00开始所经过的秒数,而localtime()函数则是将这个秒数转化为当地的具体时间(年月日时分秒) 这里时间转化要用到一个“struct tm*”的结构体,结构如下:

struct tm {
    int tm_sec;    /* 秒 – 取值区间为[0,59] */
    int tm_min;    /* 分 - 取值区间为[0,59] */
    int tm_hour;   /* 时 - 取值区间为[0,23] */
    int tm_mday;   /* 一个月中的日期 - 取值区间为[1,31] */
    int tm_mon;    /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
    int tm_year;   /* 年份,其值等于实际年份减去1900 */
    int tm_wday;   /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一 */
    int tm_yday;   /* 从每年1月1日开始的天数– 取值区间[0,365],其中0代表1月1日 */
    int tm_isdst;  /* 夏令时标识符,夏令时tm_isdst为正;不实行夏令时tm_isdst为0 */
};

示例代码:

#include <stdio.h>
#include <time.h>
int getTime()
{
    time_t t;           // 保存unix时间戳的变量 ,长整型
    struct tm* lt;      // 保存当地具体时间的变量
    int p;
    time(&t);           // 等价于 t = time(NULL); 获取时间戳
    lt = localtime(&t); // 转化为当地时间
    p = lt->tm_sec;     // 将秒数赋值给p
    return p;
}

应该就是这样啦~

c语言中时间处理

1. ctime函数

函数: ctime
功能: 把日期和时间转换为字符串
用法: char *ctime(const time_t *time);
程序例:

#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t t;
    t = time(&t);
    printf("Today's date and time: %s\n", ctime(&t));
    return 0;
}

注:若在Linux下使用本函数,需要包含time.h头文件。

2. CTime类

CTime类的对象表示的时间是基于格林威治标准时间(GMT)的。CTimeSpan类的对象表示的是时间间隔。

  • CTime类一般不会被继承使用。其对象的大小是8个字节。
  • CTime表示的日期上限是2038年1月18日,下限是1970年1月1日 12:00:00 AM GMT。

CTime类的构造函数

CTime(); // 构造一个未经初始化的CTime对象
CTime(__time64_t time); // 以__time64_t类型构造CTime对象
CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1);
CTime(const SYSTEMTIME st, int nDST = -1);
static CTime WINAPI GetCurrentTime();

CTime类成员函数

函数名 说明
GetYear() 获取年份
GetMonth() 获取月份
GetDay() 获取日期
GetHour() 获取小时
GetMinute() 获取分钟
GetSecond() 获取秒
GetDayOfWeek() 获取星期几(1表示周日)
Format(LPCTSTR pszFormat) 格式化为字符串
格式化字符串说明
格式码 含义
-------- ------
%a 周的英文缩写
%A 周的英文全名
%b 月的英文缩写
%B 月的英文全名
%c 完整的日期和时间
%d 十进制日期(01-31)
%H 24小时制小时(00-23)
%I 12小时制小时(00-11)
%j 一年中的第几天(001-366)
%m 月份(01-12)
%M 十进制分钟(00-59)
%p 上下午标识(AM/PM)
%S 十进制秒(00-59)
%U 一年中的第几个星期(00-51),周日为第一天
%W 一年中的第几个星期(00-51),周一为第一天
%w 十进制星期几(0-6)
%Y 十进制年份

CTime运算符重载

  • CTime operator + (CTimeSpan timeSpan) const;
  • CTime operator - (CTimeSpan timeSpan) const;
  • CTimeSpan operator - (CTime time) const;
  • CTime operator += (CTimeSpan span);
  • CTime operator -= (CTimeSpan span);
  • CTime operator = (__time64_t time);

比较运算符

  • operator ==
  • operator !=
  • operator >
  • operator <
  • operator >=
  • operator <=

示例:CTime 与 CString 转换

CTime m_StartTime1 = CTime::GetCurrentTime();
CString csStartTime = m_StartTime1.Format("%Y%m%d%H%M%S");
将CString转为CTime的几种方法
CString timestr = "2000年04月05日";
int a, b, c;
sscanf(timestr.GetBuffer(timestr.GetLength()), "%d年%d月%d日", &a, &b, &c);
CTime time(a, b, c, 0, 0, 0);
CString s("2001-8-29 19:06:23");
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
将CTime转换为CString的方法
CTime tmScan = CTime::GetCurrentTime();
CString szTime = tmScan.Format("'%Y-%m-%d %H:%M:%S'");
// 取得CTime中的日期
CString cstrDate = tmScan.Format("%Y-%m-%d");
// 取得CTime中的时间
CString cstrTime = tmScan.Format("%H:%M:%S");

3. strftime函数

time_t t = time(0);
char s[32];
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", localtime(&t));

C语言中的时间

1. time()函数

原型time_t time(time_t *timer);
用途:获取当前日历时间(从1970年1月1日0时0分0秒开始经过的秒数) 示例

#include <stdio.h>
#include <time.h>
int main()
{
    time_t lt;
    lt = time(NULL);
    printf("1970年1月1日0时0分0秒到现在经历了%ld秒\n", lt);
    return 0;
}

2. clock()函数

原型clock_t clock(void);
用途:获取程序运行的CPU时钟计时单元数
常量CLOCKS_PER_SEC 表示每秒的计时单元数 示例

#include <stdio.h>
#include <time.h>
int main()
{
    clock_t lt;
    for (int i = 0; i < 1000000000; i++);
    lt = clock();
    printf("循环执行1000000000个空操作需要%f秒\n", (double)lt / CLOCKS_PER_SEC);
    return 0;
}

3. tm结构体

struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};

4. gmtime()localtime()函数

原型

struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);

区别

  • gmtime() 返回世界标准时间(GMT)
  • localtime() 返回本地时间(在中国为GMT+8) 示例
#include <stdio.h>
#include <time.h>
int main()
{
    struct tm *local;
    time_t t;
    t = time(NULL);
    local = localtime(&t);
    printf("现在北京时间是%d点\n", local->tm_hour);
    local = gmtime(&t);
    printf("世界标准时间是%d点\n", local->tm_hour);
    return 0;
}

5. asctime()ctime()函数

原型

char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);

用途:将时间结构体转换为固定格式的字符串 示例

#include <stdio.h>
#include <time.h>
int main()
{
    struct tm *local;
    time_t t;
    t = time(NULL);
    local = localtime(&t);
    printf("%s", asctime(local));
    local = gmtime(&t);
    printf("%s", asctime(local));
    printf("%s", ctime(&t));
    return 0;
}

6. difftime()函数

原型double difftime(time_t time1, time_t time0);
用途:计算两个时间之间的秒数差 示例

#include <stdio.h>
#include <time.h>
int main()
{
    time_t start, end;
    start = time(NULL);
    for (int i = 0; i < 1000000000; i++);
    end = time(NULL);
    printf("这个循环用了%f秒\n", difftime(end, start));
    return 0;
}

7. mktime()函数

原型time_t mktime(struct tm *timer);
用途:将tm结构体转换为日历时间 示例:计算2012年1月20日是星期几

#include <stdio.h>
#include <time.h>
int main()
{
    struct tm t;
    time_t t_of_day;
    t.tm_year = 2012 - 1900;
    t.tm_mon = 0;
    t.tm_mday = 20;
    t.tm_hour = 0;
    t.tm_min = 12;
    t.tm_sec = 1;
    t.tm_isdst = 1;
    t_of_day = mktime(&t);
    printf("%s", ctime(&t_of_day));
    return 0;
}