appletdate.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletdate.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include "appletdefs.h"
00008 #include "appletstrings.h"
00009 #include "appletmemory.h"
00010 #include "appletdate.h"
00011 #include <Packages.h>
00012 
00013 
00014 void datetomacdate (tydaterecord rec, unsigned long *secs) {
00015     
00016     DateTimeRec macrec;
00017     
00018     macrec.year = rec.year;
00019     
00020     macrec.month = rec.month;
00021     
00022     macrec.day = rec.day;
00023     
00024     macrec.hour = rec.hour;
00025     
00026     macrec.minute = rec.minute;
00027     
00028     macrec.second = rec.second;
00029     
00030     macrec.dayOfWeek = 0;
00031 
00032     Date2Secs (&macrec, secs);
00033     } /*datetomacdate*/
00034     
00035 
00036 void appletdatetostring (tydaterecord rec, bigstring bs) {
00037     
00038     unsigned long secs;
00039     bigstring bstime;
00040         
00041     datetomacdate (rec, &secs);
00042     
00043     IUDateString (secs, abbrevDate, bs);
00044     
00045     IUTimeString (secs, true, bstime);
00046     
00047     pushstring ("\p;", bs);
00048     
00049     pushstring (bstime, bs);
00050     } /*appletdatetostring*/
00051     
00052     
00053 void getcurrenttimestring (bigstring bs) {
00054     
00055     unsigned long now;
00056     bigstring bstime;
00057     
00058     GetDateTime (&now);
00059 
00060     IUDateString (now, shortDate, bs);
00061     
00062     IUTimeString (now, true, bstime);
00063     
00064     pushstring ("\p;", bs);
00065     
00066     pushstring (bstime, bs);
00067     } /*getcurrenttimestring*/
00068         
00069     
00070 static void popnumber (bigstring bs, long *num) {
00071     
00072     long x = 0;
00073     
00074     while (true) {
00075         
00076         short len = bs [0];
00077         char ch;
00078         
00079         if (len == 0) { /*ran out of characters*/
00080             
00081             *num = x;
00082             
00083             return;
00084             }
00085         
00086         ch = bs [1];
00087         
00088         moveleft (&bs [2], &bs [1], --len);
00089         
00090         bs [0] = len;
00091         
00092         if ((ch < '0') || (ch > '9')) { /*not a numeric character*/
00093             
00094             *num = x;
00095             
00096             return;
00097             }
00098         
00099         x = (x * 10) + (ch - '0');
00100         } /*while*/
00101     } /*popnumber*/
00102     
00103     
00104 static void popshort (bigstring bs, short *num) {
00105     
00106     long x;
00107     
00108     popnumber (bs, &x);
00109     
00110     *num = (short) x;
00111     } /*popshort*/
00112     
00113     
00114 boolean stringtoappletdate (bigstring bs, tydaterecord *rec) {
00115     
00116     popshort (bs, &(*rec).month);
00117     
00118     popshort (bs, &(*rec).day);
00119     
00120     popshort (bs, &(*rec).year);
00121     
00122     popshort (bs, &(*rec).hour);
00123     
00124     popshort (bs, &(*rec).minute);
00125     
00126     popshort (bs, &(*rec).second);
00127     
00128     return (true);
00129     } /*stringtoappletdate*/
00130 
00131 
00132 boolean stringtotime (bigstring bsdate, unsigned long *time) {
00133     
00134     /*
00135     9/13/91 dmb: use the script manager to translate a string to a 
00136     time in seconds since 12:00 AM 1904.
00137     
00138     if a date is provided, but no time, the time is 12:00 AM
00139     
00140     if a time is provided, but no date, the date is 1/1/1904.
00141     
00142     we return true if any time/date information was extracted.
00143     */
00144     
00145     Ptr p = (Ptr) bsdate + 1;
00146     long n = stringlength (bsdate);
00147     long used = 0;
00148     DateCacheRecord cache;
00149     LongDateRec longdate;
00150     boolean flgotdate;
00151     boolean flgottime;
00152     DateTimeRec shortdate;
00153     
00154     *time = 0; /*default return value*/
00155     
00156     clearbytes (&longdate, longsizeof (longdate));
00157     
00158     if (InitDateCache (&cache) != noErr) /*must do this before calling String2Date*/
00159         return (false);
00160     
00161     flgotdate = ((String2Date (p, n, &cache, &used, &longdate) & fatalDateTime) == 0);
00162     
00163     flgottime = ((String2Time (p + used, n - used, &cache, &used, &longdate) & fatalDateTime) == 0);
00164     
00165     if (!flgotdate && !flgottime) /*nuthin' doin'*/
00166         return (false);
00167     
00168     if (flgotdate) {
00169     
00170         shortdate.day = longdate.ld.day;
00171         
00172         shortdate.month = longdate.ld.month;
00173         
00174         shortdate.year = longdate.ld.year;
00175         }
00176     else        
00177         Secs2Date (0, &shortdate);
00178     
00179     shortdate.hour = longdate.ld.hour; /*time fields will be zero if !flgottime*/
00180     
00181     shortdate.minute = longdate.ld.minute;
00182     
00183     shortdate.second = longdate.ld.second;
00184     
00185     Date2Secs (&shortdate, time);
00186     
00187     return (true);
00188     } /*stringtotime*/
00189 
00190 
00191 unsigned long netscapedatetomacdate (unsigned long date) {
00192     
00193     return (date + 2082844800);
00194     } /*netscapedatetomacdate*/
00195     
00196 
00197 unsigned long macdatetonetscapedate (unsigned long date) {
00198 
00199     return (date - 2082844800);
00200     } /*macdatetonetscapedate*/
00201 
00202 

Generated on Wed May 31 18:19:40 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6