Solved

time/day and C only

Posted on 2001-06-30
2
195 Views
Last Modified: 2010-04-21

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
Comment
Question by:16bits
2 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 6241458
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
 

Author Comment

by:16bits
ID: 6241637
it actually helps a lot, thank you!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question