系统时间和日期:它可以用于比较吗? [英] system time and date: can it be used for comparison?

查看:163
本文介绍了系统时间和日期:它可以用于比较吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i只是想知道我是否可以使用系统时间和日期进行比较和数学运算。



就像我输入日期一样,假设1996年1月3日我将它与系统时间比较

然后年龄应为19.



有人可以告诉我使用什么以及如何使用它。有点难以理解如何使用time.h或ctime来获取价值



谢谢。 :)

解决方案

时间计算是一个比你想象的更广泛的领域。重要提示:您有全局时间(UTC)和本地时区(偏移到UTC)。看看地球仪。通常是从1970年1月1日0:00开始的秒数。我称之为unix开始时间。



最佳起点 C ++日期和时间



MFC(NSTime)或Apple(NSDate)等大多数框架都有很多功能,但你必须尊重时区。编写一些示例和自己的类来学习它。


对于您提供的简单案例,时间函数相当容易使用。对于除此之外的任何事情,请准备好流下眼泪..



这里有一些代码可以执行你提到的计算。



注意:

1.时间返回的值每秒递增1。

2.值0对应1970年1月1日(Unix纪元)

3.我本可以使用difftime而不是手动执行减法。



  #include   <   cstdio  >  
#include < ctime >

int main( int argc, char ** argv)
{
time_t now =时间(NULL);
tm birthDateTm = { 0 };

birthDateTm.tm_year = 1996 - 1900 ;
birthDateTm.tm_mon = 0 ;
birthDateTm.tm_mday = 3 ;

time_t birthTime = mktime(& birthDateTm);
printf( BirthTime:%ld \ n,birthTime);

unsigned long dif = now - birthTime; // difftime(now,birthTime);


< span class =code-keyword> unsigned long years = dif /( 24 * 3600 * 7 * 52 );
printf( %s和%s之间的累计时间为:%ld \ n,ctime(& birthTime),ctime(& now),years);
}







输出:

出生时间: 820591200  
Wed Jan之间的经过时间 03 01 00 00 < span class =code-digit> 1996
和Wed Jan 03 01 00 00 1996
19


hi everyone,

i was just wondering if I could use the system time and date for comparison and mathematical operations.

like if I input a date lets say January 3, 1996 and I compare it with the system Time
then the Age should be 19.

can someone tell me what to use and how to do it. kinda having a hard time understang how to get the values using time.h or ctime

thank you. :)

解决方案

Time calculation is a wider area than you may imagine. Important: You have a global time ("UTC") and local time zones (offset to UTC). Look at a globe. Often times are seconds since 01.01.1970 0:00 o clock. I call it "unix start time".

Best start here C++ Date and Time.

Most frameworks like MFC (NSTime) or Apple (NSDate) have a lot of functions, but you must respect time zones. Write some examples and own classes to learn it.


For the trivial case that you present, the time functions are fairly easy to make use of. For anything much beyond this, be prepared to shed some tears..

Here's some code that will perform the calculation you mention.

Note that:
1. The value returned by time increments by 1 each second.
2. A value of 0 corresponds to Jan 1 1970 (the Unix Epoch)
3. I could have used difftime instead of performing the subtraction manually.

#include <cstdio>
#include <ctime>

int main(int argc, char **argv)
{
    time_t now = time(NULL);
    tm birthDateTm = {0};

    birthDateTm.tm_year = 1996-1900;
    birthDateTm.tm_mon = 0;
    birthDateTm.tm_mday = 3;

    time_t birthTime = mktime(&birthDateTm);
    printf("BirthTime: %ld\n", birthTime);

    unsigned long dif = now - birthTime; //difftime(now, birthTime);


    unsigned long years = dif / (24 * 3600 * 7 * 52);
    printf("Elapsed time between %s and %s in years is: %ld\n", ctime(&birthTime), ctime(&now), years);
}




Output:

BirthTime: 820591200
Elapsed time between Wed Jan 03 01:00:00 1996
 and Wed Jan 03 01:00:00 1996
 in years is: 19


这篇关于系统时间和日期:它可以用于比较吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆