calendar
This lesson provides you with a little more practice with the text function.
Example 1
Create a month calendar, selected by a user, in HTML format.We're betting that this example will make a lot more sense to you. So, how would we do this? Let's use the main function from the previous example and modify it; that is, a user enters the year required for creating a calendar.
congetstr( "Enter a year: ", year )
out @ calendar( uint( year ))
out.write( "calendar.htm" )
shell( "calendar.htm" )
Now, we describe a variable of datetime type in the calendar text function and set January 1 of the specified year into this variable. Then we output the title of the HTML file and start creating the calendar.
text calendar( uint year )
\{ datetime stime
stime.setdate( 1, 1, year )
}<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Calendar for year \(stime.year)</TITLE>
<STYLE TYPE="text/css">
<!--
BODY {background: #FFF; font-family: Verdana;}
H1 {text-align: center; margin: 5px;}
TABLE {border: 0; border-spacing: 7px;}
TD {padding: 3px; border: 1px solid; text-align: center;}
#copy {font-size: smaller; text-align: center;}
-->
</STYLE>
</HEAD>
<BODY><H1>\(stime.year)</H1>
<TABLE ALIGN=CENTER>
Notice that this calendar contains three columns and four rows. The first day of the week is stored with the help of the firstday variable for the customer. dayofweek returns the day number for the current date value. The nameofmonth function returns the name of the month in a user language.
firstday = firstdayofweek()
dayofweek = stime.dayofweek
fornum i = 0, 4
{
@"\l<TR>"
fornum j = 1, 4
{
month = i * 3 + j
@"\l<TD>\(nameofmonth( stemp, month ))
<PRE>"
...
}
}
Now that we have defined it, we can use abbrnameofday function to obtain the abbreviated day name. It is essential to add missing space characters, because the calendar includes characters which have the identical width. So, let's use each day name with four characters.
fornum k = firstday, firstday + 7
{
@" \( abbrnameofday( stemp, k ).setlen( 2 ))"
}
@" \l"
@" ".repeat( ( 7 + dayofweek - firstday ) % 7 )
If dayofweek function has the value 0, any Sunday is highlighted using red color. Our attention is turned to line feeds added after the last day of the week. The number of strings output is stored in the lines variable in the following way:
uint day = 1
uint lines
while day <= daysinmonth( year, month )
{
if !dayofweek : @"<FONT COLOR=red>"
@str( day++ ).fillspacel( 4 )
if !dayofweek : @"</FONT>"
dayofweek = ( dayofweek + 1 ) % 7
if dayofweek == firstday
{
@" \l"
lines++
}
}
Finally, the space characters are inserted into the last string and the missing rows are output in order to create the months with the identical height.
@" ".repeat( ( 7 + firstday - dayofweek ) % 7 )
while lines++ < 7 : @" \l"
@"</PRE>"
Frankly speaking, this task is difficult to comprehend because it provides a wealth of HTML texts and extra formatting. On the other hand, we finally succeed in writing this program.