This question already has answers here:
How to get the current time in milliseconds from C in Linux?
                                
                                    (5个答案)
                                
                        
                        
                            Time in milliseconds in C
                                
                                    (7个答案)
                                
                        
                        
                            how to add seconds to current time in c?
                                
                                    (3个答案)
                                
                        
                                7年前关闭。
            
                    
我正在用C编写代码,我需要在当前时间加上毫秒
到目前为止,我有:

current_time = time(NULL);
loc_time =本地时间(&current_time);

但这仅在我需要HH:MM:SS:MM时给出HH:MM:SS

假设当地时间是20:00:00:10。我想添加10毫秒并显示它,以便显示20:00:00:20。我对c还是很陌生,因此非常感谢您的帮助。我很困惑,因为时间是int格式的类型,并且要加上毫秒,我必须在当前秒数(非int)中加上.001秒。

最佳答案

问题在于“ localtime()” API仅具有秒(而不是毫秒)的粒度。

尝试gettimeofday()

也可以看看:


Getting the current time in milliseconds


如果您使用的是Windows,还可以尝试QueryPerformanceCounter():


How can I get the Windows system time with millisecond resolution?

关于c - 在c中添加毫秒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14823046/

10-13 21:48