创建一个程序,根据EST时区打印出当前星期几
Creating a program that prints out the current day of the week based on EST time zone
到目前为止我创建的代码是:
#include <stdio.h>
#include <time.h>
int main()
{
int days, rmd;
time_t seconds;
seconds = time(NULL);
days = (seconds/(60*60*24));
rmd=days%7;
if(rmd==4){
printf("Monday n");
}
if(rmd==5){
printf("Tuesday n");
}
if(rmd==6){
printf("Wednesday n");
}
if(rmd==0){
printf("Thursday n");
}
if(rmd==1){
printf("Friday n");
}
if(rmd==2){
printf("Saturday n");
}
if(rmd==3){
printf("Sunday n");
}
return 0;
}
我理解time(NULL)返回自1970年1月1日的epoch以来所经过的秒数。我也知道1月1日是星期四。我也知道UTC时区比EST早5个小时,但我不确定究竟如何考虑所有这些因素。
如果可能(例如,您不受家庭作业的限制而不能使用它),请考虑使用stftime:
http://www.cplusplus.com/reference/ctime/strftime/%A会告诉你星期几
您可以这样使用localtime()
和strftime()
:
#include <stdio.h>
#include <time.h>
int main()
{
time_t rawtime;
struct tm *info;
char buffer[100];
time(&rawtime);
info = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%A", info);
printf("Current weekday: %sn", buffer);
return 0;
}
如果需要自定义星期名称,也可以手动使用"info->tm_wday
",表示星期几,取值范围为0 ~ 6。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- Log4cpp:以UTC/GMT时区打印日期
- Poco 日期时间格式化程序 - 使用当前时区打印时间戳
- 创建一个程序,根据EST时区打印出当前星期几