| 1 | #include <stdio.h> | 
|---|
| 2 | #include <math.h> | 
|---|
| 3 | #include <ctype.h> | 
|---|
| 4 | #include <stdlib.h> | 
|---|
| 5 | #include <string.h> | 
|---|
| 6 |  | 
|---|
| 7 | #include "astro.h" | 
|---|
| 8 | #include "preferences.h" | 
|---|
| 9 |  | 
|---|
| 10 | /* sprint the variable a in sexagesimal format into out[]. | 
|---|
| 11 | * w is the number of spaces for the whole part. | 
|---|
| 12 | * fracbase is the number of pieces a whole is to broken into; valid options: | 
|---|
| 13 | *      360000: <w>:mm:ss.ss | 
|---|
| 14 | *      36000:  <w>:mm:ss.s | 
|---|
| 15 | *      3600:   <w>:mm:ss | 
|---|
| 16 | *      600:    <w>:mm.m | 
|---|
| 17 | *      60:     <w>:mm | 
|---|
| 18 | * return number of characters written to out, not counting final '\0'. | 
|---|
| 19 | */ | 
|---|
| 20 | int | 
|---|
| 21 | fs_sexa (char *out, double a, int w, int fracbase) | 
|---|
| 22 | { | 
|---|
| 23 | char *out0 = out; | 
|---|
| 24 | unsigned long n; | 
|---|
| 25 | int d; | 
|---|
| 26 | int f; | 
|---|
| 27 | int m; | 
|---|
| 28 | int s; | 
|---|
| 29 | int isneg; | 
|---|
| 30 |  | 
|---|
| 31 | /* save whether it's negative but do all the rest with a positive */ | 
|---|
| 32 | isneg = (a < 0); | 
|---|
| 33 | if (isneg) | 
|---|
| 34 | a = -a; | 
|---|
| 35 |  | 
|---|
| 36 | /* convert to an integral number of whole portions */ | 
|---|
| 37 | n = (unsigned long)(a * fracbase + 0.5); | 
|---|
| 38 | d = n/fracbase; | 
|---|
| 39 | f = n%fracbase; | 
|---|
| 40 |  | 
|---|
| 41 | /* form the whole part; "negative 0" is a special case */ | 
|---|
| 42 | if (isneg && d == 0) | 
|---|
| 43 | out += sprintf (out, "%*s-0", w-2, ""); | 
|---|
| 44 | else | 
|---|
| 45 | out += sprintf (out, "%*d", w, isneg ? -d : d); | 
|---|
| 46 |  | 
|---|
| 47 | /* do the rest */ | 
|---|
| 48 | switch (fracbase) { | 
|---|
| 49 | case 60:        /* dd:mm */ | 
|---|
| 50 | m = f/(fracbase/60); | 
|---|
| 51 | out += sprintf (out, ":%02d", m); | 
|---|
| 52 | break; | 
|---|
| 53 | case 600:       /* dd:mm.m */ | 
|---|
| 54 | out += sprintf (out, ":%02d.%1d", f/10, f%10); | 
|---|
| 55 | break; | 
|---|
| 56 | case 3600:      /* dd:mm:ss */ | 
|---|
| 57 | m = f/(fracbase/60); | 
|---|
| 58 | s = f%(fracbase/60); | 
|---|
| 59 | out += sprintf (out, ":%02d:%02d", m, s); | 
|---|
| 60 | break; | 
|---|
| 61 | case 36000:     /* dd:mm:ss.s*/ | 
|---|
| 62 | m = f/(fracbase/60); | 
|---|
| 63 | s = f%(fracbase/60); | 
|---|
| 64 | out += sprintf (out, ":%02d:%02d.%1d", m, s/10, s%10); | 
|---|
| 65 | break; | 
|---|
| 66 | case 360000:    /* dd:mm:ss.ss */ | 
|---|
| 67 | m = f/(fracbase/60); | 
|---|
| 68 | s = f%(fracbase/60); | 
|---|
| 69 | out += sprintf (out, ":%02d:%02d.%02d", m, s/100, s%100); | 
|---|
| 70 | break; | 
|---|
| 71 | default: | 
|---|
| 72 | printf ("fs_sexa: unknown fracbase: %d\n", fracbase); | 
|---|
| 73 | abort(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | return (out - out0); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | /* put the given modified Julian date, jd, in out[] according to the given | 
|---|
| 80 | * preference format. | 
|---|
| 81 | * return number of characters written to out, not counting final '\0'. | 
|---|
| 82 | */ | 
|---|
| 83 | int | 
|---|
| 84 | fs_date (char out[], int format, double jd) | 
|---|
| 85 | { | 
|---|
| 86 | char *out0 = out; | 
|---|
| 87 | int m, y; | 
|---|
| 88 | double d; | 
|---|
| 89 |  | 
|---|
| 90 | mjd_cal (jd, &m, &d, &y); | 
|---|
| 91 | /* beware of %g rounding day up */ | 
|---|
| 92 | if ((d < 1.0 && d - floor(d) >= .9999995) | 
|---|
| 93 | || (d < 10.0 && d - floor(d) >= .999995) | 
|---|
| 94 | || (d >= 10.0 && d - floor(d) >= .99995)) | 
|---|
| 95 | mjd_cal (mjd_day(jd+0.5), &m, &d, &y); | 
|---|
| 96 |  | 
|---|
| 97 | switch (format) { | 
|---|
| 98 | case PREF_YMD: | 
|---|
| 99 | out += sprintf (out, "%4d/%02d/%02.6g", y, m, d); | 
|---|
| 100 | break; | 
|---|
| 101 | case PREF_DMY: | 
|---|
| 102 | out += sprintf (out, "%2.6g/%02d/%-4d", d, m, y); | 
|---|
| 103 | break; | 
|---|
| 104 | case PREF_MDY: | 
|---|
| 105 | out += sprintf (out, "%2d/%02.6g/%-4d", m, d, y); | 
|---|
| 106 | break; | 
|---|
| 107 | default: | 
|---|
| 108 | printf ("fs_date: bad date pref: %d\n", format); | 
|---|
| 109 | abort(); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | return (out - out0); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | /* convert sexagesimal string str AxBxC to double. | 
|---|
| 117 | *   x can be anything non-numeric. Any missing A, B or C will be assumed 0. | 
|---|
| 118 | *   optional - and + can be anywhere. | 
|---|
| 119 | * return 0 if ok, -1 if can't find a thing. | 
|---|
| 120 | */ | 
|---|
| 121 | int | 
|---|
| 122 | f_scansexa ( | 
|---|
| 123 | const char *str0,       /* input string */ | 
|---|
| 124 | double *dp)             /* cracked value, if return 0 */ | 
|---|
| 125 | { | 
|---|
| 126 | double a, b, c; | 
|---|
| 127 | char str[256]; | 
|---|
| 128 | char *neg; | 
|---|
| 129 | int isneg, r; | 
|---|
| 130 |  | 
|---|
| 131 | /* copy str0 so we can play with it */ | 
|---|
| 132 | strncpy (str, str0, sizeof(str)-1); | 
|---|
| 133 | str[sizeof(str)-1] = '\0'; | 
|---|
| 134 |  | 
|---|
| 135 | /* note first negative (but not fooled by neg exponent) */ | 
|---|
| 136 | isneg = 0; | 
|---|
| 137 | neg = strchr(str, '-'); | 
|---|
| 138 | if (neg && (neg == str || (neg[-1] != 'E' && neg[-1] != 'e'))) { | 
|---|
| 139 | *neg = ' '; | 
|---|
| 140 | isneg = 1; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /* crack up to three components */ | 
|---|
| 144 | a = b = c = 0.0; | 
|---|
| 145 | r = sscanf (str, "%lf%*[^0-9]%lf%*[^0-9]%lf", &a, &b, &c); | 
|---|
| 146 | if (r < 1) | 
|---|
| 147 | return (-1); | 
|---|
| 148 |  | 
|---|
| 149 | /* back to one value, restoring neg */ | 
|---|
| 150 | *dp = (c/60.0 + b)/60.0 + a; | 
|---|
| 151 | if (isneg) | 
|---|
| 152 | *dp *= -1; | 
|---|
| 153 | return (0); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | /* crack a floating date string, bp, of the form X/Y/Z determined by the | 
|---|
| 157 | *   PREF_DATE_FORMAT preference into its components. allow the day to be a | 
|---|
| 158 | *   floating point number, | 
|---|
| 159 | * the slashes may also be spaces or colons. | 
|---|
| 160 | * a lone component with a decimal point is considered a year. | 
|---|
| 161 | */ | 
|---|
| 162 | void | 
|---|
| 163 | f_sscandate ( | 
|---|
| 164 | char *bp, | 
|---|
| 165 | int pref,       /* one of PREF_X for PREF_DATE_FORMAT */ | 
|---|
| 166 | int *m, | 
|---|
| 167 | double *d, | 
|---|
| 168 | int *y) | 
|---|
| 169 | { | 
|---|
| 170 | double X,Y,Z; /* the three components */ | 
|---|
| 171 | int n; | 
|---|
| 172 |  | 
|---|
| 173 | X = Y = Z = 0.0; | 
|---|
| 174 | n = sscanf (bp, "%lf%*[/: ]%lf%*[/: ]%lf", &X, &Y, &Z); | 
|---|
| 175 | if (n == 1 && (strchr(bp, '.') | 
|---|
| 176 | || (pref == PREF_MDY && (X < 1 || X > 12)) | 
|---|
| 177 | || (pref == PREF_DMY && (X < 1 || X > 31)))) { | 
|---|
| 178 | double Mjd; | 
|---|
| 179 | year_mjd (X, &Mjd); | 
|---|
| 180 | mjd_cal (Mjd, m, d, y); | 
|---|
| 181 | } else { | 
|---|
| 182 | switch (pref) { | 
|---|
| 183 | case PREF_MDY: | 
|---|
| 184 | if (n > 0 && X != 0) | 
|---|
| 185 | *m = (int)X; | 
|---|
| 186 | if (n > 1 && Y != 0) | 
|---|
| 187 | *d = Y; | 
|---|
| 188 | if (n > 2 && Z != 0) | 
|---|
| 189 | *y = (int)Z; | 
|---|
| 190 | break; | 
|---|
| 191 | case PREF_YMD: | 
|---|
| 192 | if (n > 0 && X != 0) | 
|---|
| 193 | *y = (int)X; | 
|---|
| 194 | if (n > 1 && Y != 0) | 
|---|
| 195 | *m = (int)Y; | 
|---|
| 196 | if (n > 2 && Z != 0) | 
|---|
| 197 | *d = Z; | 
|---|
| 198 | break; | 
|---|
| 199 | case PREF_DMY: | 
|---|
| 200 | if (n > 0 && X != 0) | 
|---|
| 201 | *d = X; | 
|---|
| 202 | if (n > 1 && Y != 0) | 
|---|
| 203 | *m = (int)Y; | 
|---|
| 204 | if (n > 2 && Z != 0) | 
|---|
| 205 | *y = (int)Z; | 
|---|
| 206 | break; | 
|---|
| 207 | } | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /* For RCS Only -- Do Not Edit */ | 
|---|
| 212 | static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: formats.c,v $ $Date: 2009-07-16 10:34:37 $ $Revision: 1.8 $ $Name: not supported by cvs2svn $"}; | 
|---|