Home » Archimedes archive » Acorn Computing » 1994 04.adf » 9404 » TechForum/!ConvDate/c/main
TechForum/!ConvDate/c/main
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
| Tape/disk: | Home » Archimedes archive » Acorn Computing » 1994 04.adf » 9404 |
| Filename: | TechForum/!ConvDate/c/main |
| Read OK: | ✔ |
| File size: | 0927 bytes |
| Load address: | 0000 |
| Exec address: | 0000 |
File contents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "wimpt.h"
#include "swis.h"
char month[][10] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
char day[][10] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char affix[][3] = {"th","st","nd","rd","th"};
typedef struct {
int csec, sec, min, hour, mday, month, year;
} ot; /*for Territory_ConvertOrdinalsToTime (NB Time is 5 byte UTC time)*/
typedef struct {
int csec, sec, min, hour, mday, month, year, wday, yday;
} to; /*for Territory_ConvertTimeToOrdinals*/
/*Note, the above structure elements mean:
csec centi-seconds
sec seconds
min minutes
hour hours
mday day of the month (1-31)
month month (1-12)
year year
wday week day (1-7)
yday year day (1-366)
*/
void getdate(int mday, int month, int year, to *t)
{
char utc[5];
ot i;
i.csec = i.sec = i.min = 0;
i.hour = 12;
i.mday = mday;
i.month = month;
i.year = year;
wimpt_complain(os_swi3(Territory_ConvertOrdinalsToTime | XOS_Bit, -1, (int)utc, (int)&i));
wimpt_complain(os_swi3(Territory_ConvertTimeToOrdinals | XOS_Bit, -1, (int)utc, (int)t));
return;
}
BOOL future(ot *t)
{
char utc_test[5];
char utc_now[5];
int i;
wimpt_complain(os_swi3(Territory_ConvertOrdinalsToTime | XOS_Bit, -1, (int)utc_test, (int)t));
utc_now[0] = 3;
wimpt_complain(os_word(14, utc_now));
for (i=4; i>=0; i--) {
if (utc_test[i] > utc_now[i]) return TRUE;
if (utc_test[i] < utc_now[i]) return FALSE;
}
return FALSE;
}
int main(void)
{
to t;
int mday_in, month_in, year_in;
int tensdigit, unitsdigit;
for (;;) {
printf("Enter day of month (eg 32), month (eg 12) & year (eg 1993):\n");
scanf("%i %i %i", &mday_in, &month_in, &year_in);
getdate(mday_in, month_in, year_in, &t);
tensdigit = (t.mday/10)%10;
unitsdigit = t.mday%10;
printf("That %s:\n%s, %i%s %s %i,\n", future((ot *)&t) ? "will be" : "was", day[t.wday-1], t.mday,
affix[unitsdigit>4 || tensdigit==1 ? 4 : unitsdigit], month[t.month-1], t.year);
tensdigit = (t.yday/10)%10;
unitsdigit = t.yday%10;
printf("The %i%s day of the year.\n\n", t.yday, affix[unitsdigit>4 || tensdigit==1 ? 4 : unitsdigit]);
}
return 0;
}
00000000 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f |.#include <stdio|
00000010 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 |.h>.#include <st|
00000020 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 |dlib.h>.#include|
00000030 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 | <string.h>.#inc|
00000040 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a 23 |lude <time.h>..#|
00000050 69 6e 63 6c 75 64 65 20 22 77 69 6d 70 74 2e 68 |include "wimpt.h|
00000060 22 0a 0a 23 69 6e 63 6c 75 64 65 20 22 73 77 69 |"..#include "swi|
00000070 73 2e 68 22 0a 0a 63 68 61 72 20 6d 6f 6e 74 68 |s.h"..char month|
00000080 5b 5d 5b 31 30 5d 20 3d 20 7b 22 4a 61 6e 75 61 |[][10] = {"Janua|
00000090 72 79 22 2c 22 46 65 62 72 75 61 72 79 22 2c 22 |ry","February","|
000000a0 4d 61 72 63 68 22 2c 22 41 70 72 69 6c 22 2c 22 |March","April","|
000000b0 4d 61 79 22 2c 22 4a 75 6e 65 22 2c 22 4a 75 6c |May","June","Jul|
000000c0 79 22 2c 22 41 75 67 75 73 74 22 2c 22 53 65 70 |y","August","Sep|
000000d0 74 65 6d 62 65 72 22 2c 22 4f 63 74 6f 62 65 72 |tember","October|
000000e0 22 2c 22 4e 6f 76 65 6d 62 65 72 22 2c 22 44 65 |","November","De|
000000f0 63 65 6d 62 65 72 22 7d 3b 0a 63 68 61 72 20 64 |cember"};.char d|
00000100 61 79 5b 5d 5b 31 30 5d 20 20 20 3d 20 7b 22 53 |ay[][10] = {"S|
00000110 75 6e 64 61 79 22 2c 22 4d 6f 6e 64 61 79 22 2c |unday","Monday",|
00000120 22 54 75 65 73 64 61 79 22 2c 22 57 65 64 6e 65 |"Tuesday","Wedne|
00000130 73 64 61 79 22 2c 22 54 68 75 72 73 64 61 79 22 |sday","Thursday"|
00000140 2c 22 46 72 69 64 61 79 22 2c 22 53 61 74 75 72 |,"Friday","Satur|
00000150 64 61 79 22 7d 3b 0a 63 68 61 72 20 61 66 66 69 |day"};.char affi|
00000160 78 5b 5d 5b 33 5d 20 20 3d 20 7b 22 74 68 22 2c |x[][3] = {"th",|
00000170 22 73 74 22 2c 22 6e 64 22 2c 22 72 64 22 2c 22 |"st","nd","rd","|
00000180 74 68 22 7d 3b 0a 0a 74 79 70 65 64 65 66 20 73 |th"};..typedef s|
00000190 74 72 75 63 74 20 7b 0a 20 20 69 6e 74 20 63 73 |truct {. int cs|
000001a0 65 63 2c 20 73 65 63 2c 20 6d 69 6e 2c 20 68 6f |ec, sec, min, ho|
000001b0 75 72 2c 20 6d 64 61 79 2c 20 6d 6f 6e 74 68 2c |ur, mday, month,|
000001c0 20 79 65 61 72 3b 0a 7d 20 6f 74 3b 20 2f 2a 66 | year;.} ot; /*f|
000001d0 6f 72 20 54 65 72 72 69 74 6f 72 79 5f 43 6f 6e |or Territory_Con|
000001e0 76 65 72 74 4f 72 64 69 6e 61 6c 73 54 6f 54 69 |vertOrdinalsToTi|
000001f0 6d 65 20 28 4e 42 20 54 69 6d 65 20 69 73 20 35 |me (NB Time is 5|
00000200 20 62 79 74 65 20 55 54 43 20 74 69 6d 65 29 2a | byte UTC time)*|
00000210 2f 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 |/..typedef struc|
00000220 74 20 7b 0a 20 20 69 6e 74 20 63 73 65 63 2c 20 |t {. int csec, |
00000230 73 65 63 2c 20 6d 69 6e 2c 20 68 6f 75 72 2c 20 |sec, min, hour, |
00000240 6d 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 |mday, month, yea|
00000250 72 2c 20 77 64 61 79 2c 20 79 64 61 79 3b 0a 7d |r, wday, yday;.}|
00000260 20 74 6f 3b 20 2f 2a 66 6f 72 20 54 65 72 72 69 | to; /*for Terri|
00000270 74 6f 72 79 5f 43 6f 6e 76 65 72 74 54 69 6d 65 |tory_ConvertTime|
00000280 54 6f 4f 72 64 69 6e 61 6c 73 2a 2f 0a 0a 2f 2a |ToOrdinals*/../*|
00000290 4e 6f 74 65 2c 20 74 68 65 20 61 62 6f 76 65 20 |Note, the above |
000002a0 73 74 72 75 63 74 75 72 65 20 65 6c 65 6d 65 6e |structure elemen|
000002b0 74 73 20 6d 65 61 6e 3a 0a 20 20 63 73 65 63 20 |ts mean:. csec |
000002c0 20 63 65 6e 74 69 2d 73 65 63 6f 6e 64 73 0a 20 | centi-seconds. |
000002d0 20 73 65 63 20 20 20 73 65 63 6f 6e 64 73 0a 20 | sec seconds. |
000002e0 20 6d 69 6e 20 20 20 6d 69 6e 75 74 65 73 0a 20 | min minutes. |
000002f0 20 68 6f 75 72 20 20 68 6f 75 72 73 0a 20 20 6d | hour hours. m|
00000300 64 61 79 20 20 64 61 79 20 6f 66 20 74 68 65 20 |day day of the |
00000310 6d 6f 6e 74 68 20 28 31 2d 33 31 29 0a 20 20 6d |month (1-31). m|
00000320 6f 6e 74 68 20 6d 6f 6e 74 68 20 28 31 2d 31 32 |onth month (1-12|
00000330 29 0a 20 20 79 65 61 72 20 20 79 65 61 72 0a 20 |). year year. |
00000340 20 77 64 61 79 20 20 77 65 65 6b 20 64 61 79 20 | wday week day |
00000350 28 31 2d 37 29 0a 20 20 79 64 61 79 20 20 79 65 |(1-7). yday ye|
00000360 61 72 20 64 61 79 20 28 31 2d 33 36 36 29 0a 2a |ar day (1-366).*|
00000370 2f 0a 0a 76 6f 69 64 20 67 65 74 64 61 74 65 28 |/..void getdate(|
00000380 69 6e 74 20 6d 64 61 79 2c 20 69 6e 74 20 6d 6f |int mday, int mo|
00000390 6e 74 68 2c 20 69 6e 74 20 79 65 61 72 2c 20 74 |nth, int year, t|
000003a0 6f 20 2a 74 29 0a 7b 0a 20 20 63 68 61 72 20 75 |o *t).{. char u|
000003b0 74 63 5b 35 5d 3b 0a 20 20 6f 74 20 69 3b 0a 20 |tc[5];. ot i;. |
000003c0 20 69 2e 63 73 65 63 20 3d 20 69 2e 73 65 63 20 | i.csec = i.sec |
000003d0 3d 20 69 2e 6d 69 6e 20 3d 20 30 3b 0a 20 20 69 |= i.min = 0;. i|
000003e0 2e 68 6f 75 72 20 3d 20 31 32 3b 0a 20 20 69 2e |.hour = 12;. i.|
000003f0 6d 64 61 79 20 3d 20 6d 64 61 79 3b 0a 20 20 69 |mday = mday;. i|
00000400 2e 6d 6f 6e 74 68 20 3d 20 6d 6f 6e 74 68 3b 0a |.month = month;.|
00000410 20 20 69 2e 79 65 61 72 20 3d 20 79 65 61 72 3b | i.year = year;|
00000420 0a 20 20 77 69 6d 70 74 5f 63 6f 6d 70 6c 61 69 |. wimpt_complai|
00000430 6e 28 6f 73 5f 73 77 69 33 28 54 65 72 72 69 74 |n(os_swi3(Territ|
00000440 6f 72 79 5f 43 6f 6e 76 65 72 74 4f 72 64 69 6e |ory_ConvertOrdin|
00000450 61 6c 73 54 6f 54 69 6d 65 20 7c 20 58 4f 53 5f |alsToTime | XOS_|
00000460 42 69 74 2c 20 2d 31 2c 20 28 69 6e 74 29 75 74 |Bit, -1, (int)ut|
00000470 63 2c 20 28 69 6e 74 29 26 69 29 29 3b 0a 20 20 |c, (int)&i));. |
00000480 77 69 6d 70 74 5f 63 6f 6d 70 6c 61 69 6e 28 6f |wimpt_complain(o|
00000490 73 5f 73 77 69 33 28 54 65 72 72 69 74 6f 72 79 |s_swi3(Territory|
000004a0 5f 43 6f 6e 76 65 72 74 54 69 6d 65 54 6f 4f 72 |_ConvertTimeToOr|
000004b0 64 69 6e 61 6c 73 20 7c 20 58 4f 53 5f 42 69 74 |dinals | XOS_Bit|
000004c0 2c 20 2d 31 2c 20 28 69 6e 74 29 75 74 63 2c 20 |, -1, (int)utc, |
000004d0 28 69 6e 74 29 74 29 29 3b 0a 20 20 72 65 74 75 |(int)t));. retu|
000004e0 72 6e 3b 0a 7d 0a 0a 42 4f 4f 4c 20 66 75 74 75 |rn;.}..BOOL futu|
000004f0 72 65 28 6f 74 20 2a 74 29 0a 7b 0a 20 20 63 68 |re(ot *t).{. ch|
00000500 61 72 20 75 74 63 5f 74 65 73 74 5b 35 5d 3b 0a |ar utc_test[5];.|
00000510 20 20 63 68 61 72 20 75 74 63 5f 6e 6f 77 5b 35 | char utc_now[5|
00000520 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 77 69 |];. int i;. wi|
00000530 6d 70 74 5f 63 6f 6d 70 6c 61 69 6e 28 6f 73 5f |mpt_complain(os_|
00000540 73 77 69 33 28 54 65 72 72 69 74 6f 72 79 5f 43 |swi3(Territory_C|
00000550 6f 6e 76 65 72 74 4f 72 64 69 6e 61 6c 73 54 6f |onvertOrdinalsTo|
00000560 54 69 6d 65 20 7c 20 58 4f 53 5f 42 69 74 2c 20 |Time | XOS_Bit, |
00000570 2d 31 2c 20 28 69 6e 74 29 75 74 63 5f 74 65 73 |-1, (int)utc_tes|
00000580 74 2c 20 28 69 6e 74 29 74 29 29 3b 0a 20 20 75 |t, (int)t));. u|
00000590 74 63 5f 6e 6f 77 5b 30 5d 20 3d 20 33 3b 0a 20 |tc_now[0] = 3;. |
000005a0 20 77 69 6d 70 74 5f 63 6f 6d 70 6c 61 69 6e 28 | wimpt_complain(|
000005b0 6f 73 5f 77 6f 72 64 28 31 34 2c 20 75 74 63 5f |os_word(14, utc_|
000005c0 6e 6f 77 29 29 3b 0a 20 20 66 6f 72 20 28 69 3d |now));. for (i=|
000005d0 34 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 20 7b 0a |4; i>=0; i--) {.|
000005e0 20 20 20 20 69 66 20 28 75 74 63 5f 74 65 73 74 | if (utc_test|
000005f0 5b 69 5d 20 3e 20 75 74 63 5f 6e 6f 77 5b 69 5d |[i] > utc_now[i]|
00000600 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 |) return TRUE;. |
00000610 20 20 20 69 66 20 28 75 74 63 5f 74 65 73 74 5b | if (utc_test[|
00000620 69 5d 20 3c 20 75 74 63 5f 6e 6f 77 5b 69 5d 29 |i] < utc_now[i])|
00000630 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 | return FALSE;. |
00000640 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 | }. return FALS|
00000650 45 3b 0a 7d 0a 0a 69 6e 74 20 6d 61 69 6e 28 76 |E;.}..int main(v|
00000660 6f 69 64 29 0a 7b 0a 20 20 74 6f 20 74 3b 0a 20 |oid).{. to t;. |
00000670 20 69 6e 74 20 6d 64 61 79 5f 69 6e 2c 20 6d 6f | int mday_in, mo|
00000680 6e 74 68 5f 69 6e 2c 20 79 65 61 72 5f 69 6e 3b |nth_in, year_in;|
00000690 0a 20 20 69 6e 74 20 74 65 6e 73 64 69 67 69 74 |. int tensdigit|
000006a0 2c 20 75 6e 69 74 73 64 69 67 69 74 3b 0a 20 20 |, unitsdigit;. |
000006b0 66 6f 72 20 28 3b 3b 29 20 7b 0a 20 20 20 20 70 |for (;;) {. p|
000006c0 72 69 6e 74 66 28 22 45 6e 74 65 72 20 64 61 79 |rintf("Enter day|
000006d0 20 6f 66 20 6d 6f 6e 74 68 20 28 65 67 20 33 32 | of month (eg 32|
000006e0 29 2c 20 6d 6f 6e 74 68 20 28 65 67 20 31 32 29 |), month (eg 12)|
000006f0 20 26 20 79 65 61 72 20 28 65 67 20 31 39 39 33 | & year (eg 1993|
00000700 29 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 63 61 6e |):\n");. scan|
00000710 66 28 22 25 69 20 25 69 20 25 69 22 2c 20 26 6d |f("%i %i %i", &m|
00000720 64 61 79 5f 69 6e 2c 20 26 6d 6f 6e 74 68 5f 69 |day_in, &month_i|
00000730 6e 2c 20 26 79 65 61 72 5f 69 6e 29 3b 0a 20 20 |n, &year_in);. |
00000740 20 20 67 65 74 64 61 74 65 28 6d 64 61 79 5f 69 | getdate(mday_i|
00000750 6e 2c 20 6d 6f 6e 74 68 5f 69 6e 2c 20 79 65 61 |n, month_in, yea|
00000760 72 5f 69 6e 2c 20 26 74 29 3b 0a 20 20 20 20 74 |r_in, &t);. t|
00000770 65 6e 73 64 69 67 69 74 20 20 3d 20 28 74 2e 6d |ensdigit = (t.m|
00000780 64 61 79 2f 31 30 29 25 31 30 3b 0a 20 20 20 20 |day/10)%10;. |
00000790 75 6e 69 74 73 64 69 67 69 74 20 3d 20 74 2e 6d |unitsdigit = t.m|
000007a0 64 61 79 25 31 30 3b 0a 20 20 20 20 70 72 69 6e |day%10;. prin|
000007b0 74 66 28 22 54 68 61 74 20 25 73 3a 5c 6e 25 73 |tf("That %s:\n%s|
000007c0 2c 20 25 69 25 73 20 25 73 20 25 69 2c 5c 6e 22 |, %i%s %s %i,\n"|
000007d0 2c 20 66 75 74 75 72 65 28 28 6f 74 20 2a 29 26 |, future((ot *)&|
000007e0 74 29 20 3f 20 22 77 69 6c 6c 20 62 65 22 20 3a |t) ? "will be" :|
000007f0 20 22 77 61 73 22 2c 20 64 61 79 5b 74 2e 77 64 | "was", day[t.wd|
00000800 61 79 2d 31 5d 2c 20 74 2e 6d 64 61 79 2c 0a 20 |ay-1], t.mday,. |
00000810 20 20 20 20 20 20 20 20 20 20 20 61 66 66 69 78 | affix|
00000820 5b 75 6e 69 74 73 64 69 67 69 74 3e 34 20 7c 7c |[unitsdigit>4 |||
00000830 20 74 65 6e 73 64 69 67 69 74 3d 3d 31 20 3f 20 | tensdigit==1 ? |
00000840 34 20 3a 20 75 6e 69 74 73 64 69 67 69 74 5d 2c |4 : unitsdigit],|
00000850 20 6d 6f 6e 74 68 5b 74 2e 6d 6f 6e 74 68 2d 31 | month[t.month-1|
00000860 5d 2c 20 74 2e 79 65 61 72 29 3b 0a 20 20 20 20 |], t.year);. |
00000870 74 65 6e 73 64 69 67 69 74 20 20 3d 20 28 74 2e |tensdigit = (t.|
00000880 79 64 61 79 2f 31 30 29 25 31 30 3b 0a 20 20 20 |yday/10)%10;. |
00000890 20 75 6e 69 74 73 64 69 67 69 74 20 3d 20 74 2e | unitsdigit = t.|
000008a0 79 64 61 79 25 31 30 3b 0a 20 20 20 20 70 72 69 |yday%10;. pri|
000008b0 6e 74 66 28 22 54 68 65 20 25 69 25 73 20 64 61 |ntf("The %i%s da|
000008c0 79 20 6f 66 20 74 68 65 20 79 65 61 72 2e 5c 6e |y of the year.\n|
000008d0 5c 6e 22 2c 20 74 2e 79 64 61 79 2c 20 61 66 66 |\n", t.yday, aff|
000008e0 69 78 5b 75 6e 69 74 73 64 69 67 69 74 3e 34 20 |ix[unitsdigit>4 |
000008f0 7c 7c 20 74 65 6e 73 64 69 67 69 74 3d 3d 31 20 ||| tensdigit==1 |
00000900 3f 20 34 20 3a 20 75 6e 69 74 73 64 69 67 69 74 |? 4 : unitsdigit|
00000910 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 |]);. }.. retur|
00000920 6e 20 30 3b 0a 7d 0a |n 0;.}.|
00000927
.