linux 时间戳转换/dmesg 时间转换

linux时间戳转换

1. 将日期转换成时间戳
$date +%s -d "04/24/2014 15:30:00"
1398324600
2. 将时间戳转换成日期
$date -d @1398324600
Thu Apr 24 15:30:00 CST 2014
3. 将当前日期转换成时间戳
$date +%s
1398765730

dmesg 时间转换

dmesg 输出的格式不易查看,可以通过命令进行转换。

记录如下:

时间查看:

date -d "1970-01-01 UTC `echo "$(date +%s)-$(cat /proc/uptime|cut -f 1 -d' ')+12288812.926194"|bc ` seconds"

/proc/uptime详解

在Linux中,我们常常会使用到uptime命令去看看系统的运行时间,它与一个文件有关,就是/proc/uptime,下面对其进行详细介绍。

View Code BASH
1
2
3
4
master@jay-intel:~$ cat /proc/uptime
6447032.12 48185264.69
master@jay-intel:~$ cat /proc/cpuinfo  | grep processor | wc -l
8

第一列输出的是,系统启动到现在的时间(以秒为单位),这里简记为num1;
第二列输出的是,系统空闲的时间(以秒为单位),这里简记为num2。

注意,很多很多人都知道第二个是系统空闲的时间,但是可能你不知道是,在SMP系统里,系统空闲的时间有时会是系统运行时间的几倍,这是怎么回事呢?
因为系统空闲时间的计算,是把SMP算进去的,就是所你有几个逻辑的CPU(包括超线程)。

系统的空闲率(%) = num2/(num1*N) 其中N是SMP系统中的CPU个数。

从上面我的一台机器上的数据可知,
本机启动到现在的时间长度为:6447032.12 seconds = 74.6 days
空闲率为:48185264.69/(6447032.12*8)=93.4%

系统空闲率越大,说明系统比较闲,可以加重一些负载;而系统空闲率很小,则可能考虑升级本机器硬件或者迁移部分负载到其他机器上。

Some docs from Redhat:
The first number is the total number of seconds the system has been up. The second number is how much of that time the machine has spent idle, in seconds. (Jay’s comments: Please pay attention to SMP system.)


在Java中,可以使用`java.time`包中的类来将Linux时间戳转换为可读的时间格式。Linux时间戳通常指的是从1970年1月1日(Unix纪元)开始的秒数(或毫秒数)。下面是一个简单的转换例子: ```java import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class LinuxTimestampConverter { public static void main(String[] args) { // 示例:Linux时间戳,单位是秒 long linuxTimestampInSeconds = 1619651600L; // 将秒转换为毫秒 long linuxTimestampInMilliseconds = linuxTimestampInSeconds * 1000L; // 将时间戳转换为Instant对象 Instant instant = Instant.ofEpochMilli(linuxTimestampInMilliseconds); // 获取系统默认的时区 ZoneId zoneId = ZoneId.systemDefault(); // 将Instant对象转换为ZonedDateTime对象 ZonedDateTime zonedDateTime = instant.atZone(zoneId); // 格式化时间输出 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = zonedDateTime.format(formatter); System.out.println("转换后的时间为: " + formattedDateTime); } } ``` 上述代码中,我们首先将Linux时间戳从秒转换为毫秒,因为`Instant.ofEpochMilli`方法接受的是毫秒级别的时间戳。然后,我们创建了一个`Instant`对象,它代表了这一时刻的全球标准时间(UTC)。接着,我们使用默认时区创建了一个`ZonedDateTime`对象,这样可以得到具体时区的时间。最后,我们通过`DateTimeFormatter`对时间进行了格式化,以便输出易读的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值