我得到一个字节流,在每个2000字节之后,我想创建一个新文件并存储它。由于它是一个连续的字节流,所以我不能使用计数器等。所以,我想使用系统时间戳来唯一地识别类似于filename的文件。
我找到了一些线程来获得系统时间戳,但我在那里看到了几秒钟。还有其他方法可以在C,linux (timestmap like: 2011-11-08 18:02:08.954092000)中获得完整的时间戳吗?在所有线程中,我看到的只有几秒钟,就像2011-11-08 18:02:08一样
发布于 2015-07-29 11:23:18
创建带有毫秒时间戳的文件名字符串的示例代码:
get_time_in_ms.c
#include<stdio.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
void get_time_in_ms()
{
long ms;
time_t time;
struct timespec spec;
char filename[14];
clock_gettime(CLOCK_REALTIME, &spec);
time = spec.tv_sec;
ms = round(spec.tv_nsec / 1000000 ); // Convert nanoseconds to milliseconds
printf("Current time: %lu.%03ld seconds since the Epoch\n", time, ms);
sprintf(filename,"%lu%03ld",time, ms);
printf("File name : %s\n", filename);
}
void main() {
get_time_in_ms();
}输出:
$ gcc -Wl,--no-as-needed -lrt -lm get_time_in_ms.c -o get_time_in_ms
$ date && get_time_in_ms
Wed Jul 29 16:51:20 IST 2015
Current time: 1438168880.503 seconds since the Epoch
File name : 1438168880503发布于 2015-07-29 10:35:24
使用date --rfc-3339='ns'命令以纳秒精度查看o/p。您可以尝试查看"man date"命令以获得更详细的细节。如您所见,下面是我使用这个命令得到的o/p:
[user1@mach]# date --rfc-3339='ns'
2015-07-29 03:34:09.077024060-07:00https://stackoverflow.com/questions/31697393
复制相似问题