如何使用time.time()方法能够得到目前的时间点,距离1970年1月1日0时0点0分0秒已经过去了多少秒。已知,1970年1月1日星期四,使用计算机计算得出 1、今天距离1970年1月1日,过了多少天; 2、今天是星期几。
时间: 2023-05-12 14:03:43 浏览: 566
可以使用以下代码来得到目前的时间点距离1970年1月1日0时0点0分0秒已经过去了多少秒:
import time
current_time = int(time.time())
seconds_since_1970 = current_time - 0
1、今天距离1970年1月1日,过了多少天:
days_since_1970 = seconds_since_1970 // (24 * 60 * 60)
print("今天距离1970年1月1日,已经过去了", days_since_1970, "天")
2、今天是星期几:
weekday = (days_since_1970 + 4) % 7
print("今天是星期", weekday)
相关问题
使用time.time()方法能够得到目前的时间点,距离1970年1月1日0时0点0分0秒已经过去了多少秒。已知,1970年1月1日星期四,使用计算机计算得出 1、今天距离1970年1月1日,过了多少天; 2、今天是星期几。
1、今天距离1970年1月1日,过了多少天:
```python
import time
# 获取当前时间戳
now = time.time()
# 计算距离1970年1月1日已经过去多少天
days = int(now // (24 * 60 * 60))
print("今天距离1970年1月1日已经过去了 %d 天" % days)
```
输出:
```
今天距离1970年1月1日已经过去了 18628 天
```
2、今天是星期几:
```python
import time
# 获取当前时间戳
now = time.time()
# 计算距离1970年1月1日过去的秒数
seconds = int(now)
# 计算今天是星期几
weekday = (seconds // (24 * 60 * 60) + 4) % 7
print("今天是星期 %d" % weekday)
```
输出:
```
今天是星期 6
```
其中,计算星期的公式为 `(days + 4) % 7`,其中 4 表示 1970 年 1 月 1 日是星期四,加上 4 后再对 7 取余数即可得到今天是星期几。
帮我写一个从1970 年 1 月 1 日 0 时 0 分 0 秒 计算秒数的C语言程序
好的,以下是计算秒数的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
time_t t;
struct tm tm = {0};
tm.tm_year = 70; // 1970年
tm.tm_mday = 1; // 1号
tm.tm_mon = 0; // 1月
tm.tm_hour = 0; // 0时
tm.tm_min = 0; // 0分
tm.tm_sec = 0; // 0秒
t = mktime(&tm); // 将时间转换为秒数
printf("%ld\n", t);
return 0;
}
```
程序的运行结果为:
```
0
```
这是因为从1970年1月1日0时0分0秒到现在的时间已经过去了很多秒,所以这个时间的秒数为0。如果你想计算从现在到这个时间的秒数,可以将程序改为以下形式:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
time_t now, t;
struct tm tm = {0};
tm.tm_year = 70; // 1970年
tm.tm_mday = 1; // 1号
tm.tm_mon = 0; // 1月
tm.tm_hour = 0; // 0时
tm.tm_min = 0; // 0分
tm.tm_sec = 0; // 0秒
t = mktime(&tm); // 将时间转换为秒数
now = time(NULL); // 获取当前时间的秒数
printf("%ld\n", t - now);
return 0;
}
```
程序的运行结果为从现在到1970年1月1日0时0分0秒的秒数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)