[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

time/day and C only


I want to make a greeting, in Linux (C program only)like

if hour is > 18PM and < 22PM
print "good evening, today is Wednesday. in year 2001 has 200 days remaining, time now is %s", time$);

if hour is > 4AM and <8AM
print "good morning, today is Friday. in year 2001 has 172 days remaining, time now is %s", time$);

0
16bits
Asked:
16bits
1 Solution
 
mnashadkaCommented:
Try something like:
time_t now;
struct tm *time_struct;
char day_str[20];
char time_str[20];

now = time(NULL);
time_struct = mktime(&now);

strftime(day_str, 20, "%a", time_struct);
strftime(time_str, 20, "%H:%M:%S", time_struct);

if(time_struct->
tm_hour > 18 && time_struct->tm_hour < 22)
{
  printf("Good evening, today is %s in year %d, it is the %d day of the year, time now is %s", day_str, time_struct->tm_year, time_struct->yday, time_str);
}
else if(time_struct->
tm_hour > 4 && time_struct->
tm_hour < 8)
{
  printf("Good morning, today is %s in year %d, it is the %d day of the year, time now is %s", day_str, time_struct->tm_year, time_struct->yday, time_str);
}

Note, this doesn't give you the number of days remaing.  To do this, you need to figure out the number of days in the year and subtract time_struct->yday from that.  Hope this helps.    
0
 
16bitsAuthor Commented:
it actually helps a lot, thank you!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now