| 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 preference | 
|---|
| 80 | * format. | 
|---|
| 81 | * return number of characters written to out, not counting final '\0'. | 
|---|
| 82 | */ | 
|---|
| 83 | int | 
|---|
| 84 | fs_date (char out[], double jd) | 
|---|
| 85 | { | 
|---|
| 86 | int p = pref_get (PREF_DATE_FORMAT); | 
|---|
| 87 | char *out0 = out; | 
|---|
| 88 | int m, y; | 
|---|
| 89 | double d; | 
|---|
| 90 |  | 
|---|
| 91 | mjd_cal (jd, &m, &d, &y); | 
|---|
| 92 | /* beware of %g rounding day up */ | 
|---|
| 93 | if ((d < 1.0 && d - floor(d) >= .9999995) | 
|---|
| 94 | || (d < 10.0 && d - floor(d) >= .999995) | 
|---|
| 95 | || (d >= 10.0 && d - floor(d) >= .99995)) | 
|---|
| 96 | mjd_cal (mjd_day(jd+0.5), &m, &d, &y); | 
|---|
| 97 |  | 
|---|
| 98 | switch (p) { | 
|---|
| 99 | case PREF_YMD: | 
|---|
| 100 | out += sprintf (out, "%4d/%02d/%02.6g", y, m, d); | 
|---|
| 101 | break; | 
|---|
| 102 | case PREF_DMY: | 
|---|
| 103 | out += sprintf (out, "%2.6g/%02d/%-4d", d, m, y); | 
|---|
| 104 | break; | 
|---|
| 105 | case PREF_MDY: | 
|---|
| 106 | out += sprintf (out, "%2d/%02.6g/%-4d", m, d, y); | 
|---|
| 107 | break; | 
|---|
| 108 | default: | 
|---|
| 109 | printf ("fs_date: bad date pref: %d\n", p); | 
|---|
| 110 | abort(); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | return (out - out0); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 | /* convert sexagesimal string str AxBxC to double. | 
|---|
| 118 | *   x can be anything non-numeric. Any missing A, B or C will be assumed 0. | 
|---|
| 119 | *   optional - and + can be anywhere. | 
|---|
| 120 | * return 0 if ok, -1 if can't find a thing. | 
|---|
| 121 | */ | 
|---|
| 122 | int | 
|---|
| 123 | f_scansexa ( | 
|---|
| 124 | const char *str0,       /* input string */ | 
|---|
| 125 | double *dp)             /* cracked value, if return 0 */ | 
|---|
| 126 | { | 
|---|
| 127 | double a = 0, b = 0, c = 0; | 
|---|
| 128 | char str[128]; | 
|---|
| 129 | char *neg; | 
|---|
| 130 | int r; | 
|---|
| 131 |  | 
|---|
| 132 | /* copy str0 so we can play with it */ | 
|---|
| 133 | strncpy (str, str0, sizeof(str)-1); | 
|---|
| 134 | str[sizeof(str)-1] = '\0'; | 
|---|
| 135 |  | 
|---|
| 136 | neg = strchr(str, '-'); | 
|---|
| 137 | if (neg) | 
|---|
| 138 | *neg = ' '; | 
|---|
| 139 | r = sscanf (str, "%lf%*[^0-9]%lf%*[^0-9]%lf", &a, &b, &c); | 
|---|
| 140 | if (r < 1) | 
|---|
| 141 | return (-1); | 
|---|
| 142 | *dp = a + b/60 + c/3600; | 
|---|
| 143 | if (neg) | 
|---|
| 144 | *dp *= -1; | 
|---|
| 145 | return (0); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | /* crack a floating date string, bp, of the form X/Y/Z determined by the | 
|---|
| 149 | *   PREF_DATE_FORMAT preference into its components. allow the day to be a | 
|---|
| 150 | *   floating point number, | 
|---|
| 151 | * the slashes may also be spaces or colons. | 
|---|
| 152 | * a lone component with a decimal point is considered a year. | 
|---|
| 153 | */ | 
|---|
| 154 | void | 
|---|
| 155 | f_sscandate ( | 
|---|
| 156 | char *bp, | 
|---|
| 157 | int pref,       /* one of PREF_X for PREF_DATE_FORMAT */ | 
|---|
| 158 | int *m, | 
|---|
| 159 | double *d, | 
|---|
| 160 | int *y) | 
|---|
| 161 | { | 
|---|
| 162 | double X,Y,Z; /* the three components */ | 
|---|
| 163 | int n; | 
|---|
| 164 |  | 
|---|
| 165 | X = Y = Z = 0.0; | 
|---|
| 166 | n = sscanf (bp, "%lf%*[/: ]%lf%*[/: ]%lf", &X, &Y, &Z); | 
|---|
| 167 | if (n == 1 && (strchr(bp, '.') | 
|---|
| 168 | || (pref == PREF_MDY && (X < 1 || X > 12)) | 
|---|
| 169 | || (pref == PREF_DMY && (X < 1 || X > 31)))) { | 
|---|
| 170 | double Mjd; | 
|---|
| 171 | year_mjd (X, &Mjd); | 
|---|
| 172 | mjd_cal (Mjd, m, d, y); | 
|---|
| 173 | } else { | 
|---|
| 174 | switch (pref) { | 
|---|
| 175 | case PREF_MDY: | 
|---|
| 176 | if (n > 0 && X != 0) | 
|---|
| 177 | *m = (int)X; | 
|---|
| 178 | if (n > 1 && Y != 0) | 
|---|
| 179 | *d = Y; | 
|---|
| 180 | if (n > 2 && Z != 0) | 
|---|
| 181 | *y = (int)Z; | 
|---|
| 182 | break; | 
|---|
| 183 | case PREF_YMD: | 
|---|
| 184 | if (n > 0 && X != 0) | 
|---|
| 185 | *y = (int)X; | 
|---|
| 186 | if (n > 1 && Y != 0) | 
|---|
| 187 | *m = (int)Y; | 
|---|
| 188 | if (n > 2 && Z != 0) | 
|---|
| 189 | *d = Z; | 
|---|
| 190 | break; | 
|---|
| 191 | case PREF_DMY: | 
|---|
| 192 | if (n > 0 && X != 0) | 
|---|
| 193 | *d = X; | 
|---|
| 194 | if (n > 1 && Y != 0) | 
|---|
| 195 | *m = (int)Y; | 
|---|
| 196 | if (n > 2 && Z != 0) | 
|---|
| 197 | *y = (int)Z; | 
|---|
| 198 | break; | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | /* For RCS Only -- Do Not Edit */ | 
|---|
| 204 | static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: formats.c,v $ $Date: 2005-08-21 10:02:37 $ $Revision: 1.5 $ $Name: not supported by cvs2svn $"}; | 
|---|