使用C ++操纵时间 [英] Maniputing time with C++

查看:84
本文介绍了使用C ++操纵时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hI ..

我有一个非常简单的问题..

i以HH:MM:SS格式获得当前时间

如果我以第二种格式获得新值,我将如何计算新时间。

例如,我现在的时间是16:36:30。我得到80秒,需要将此值添加到我当前的时间。

计算新时间的简单方法是什么

hI..
I got a very simple question..
i got my current time in "HH:MM:SS"format
how i ''m going to calculate the new time if i got a new value in second format.
for example, my current time now is 16:36:30. I got 80 second and need to add this value to my current time.
What is the simply way to calculate the new time

推荐答案

例如,您可以通过以下方式尝试解决它:

For example you can try to settle it, by following way:
#include <time.h>
#include <stdio.h>

int main()
{
    time_t your_time = ....

    struct tm your_time_tm = *localtime( &your_time);


    struct tm then_tm = your_time_tm;
    then_tm.tm_sec += 100;    

    mktime( &then_tm);      // let's normalize it

    printf( "%s\n", asctime( &your_time_tm));
    printf( "%s\n", asctime( &then_tm));

    return 0;
}


1。将字符串转换为小时,分钟和秒钟

2.将这三个数字组合起来计算总秒数

3.添加80

4.将结果值分解为小时,分钟和秒

5.将其转换回字符串。



可能有快捷方式或更快的方法,但所有编程计时器内部转换为单个数字,表示自某个预定起点以来已经过的秒数,毫秒数或甚至更小的单位数。这样做可以让您更轻松地处理这些其他格式。
1. Convert the string to hours, minutes and seconds
2. combine these three numbers to calculate the total number of seconds
3. Add 80
4. Split up the resulting value back to hours, minutes and seconds
5. convert that back to string.

There may be shortcuts or faster ways of doing this, but all programatical timers internally convert to a single number representing the number of seconds, milliseconds or an even smaller unit, that have passed since some predetermined starting point. Doing it this way will make it easier for you to deal with these other formats.


这篇关于使用C ++操纵时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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