[1457] | 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
|
---|
[2551] | 18 | * return number of characters written to out, not counting final '\0'.
|
---|
[1457] | 19 | */
|
---|
[2551] | 20 | int
|
---|
| 21 | fs_sexa (char *out, double a, int w, int fracbase)
|
---|
[1457] | 22 | {
|
---|
[2551] | 23 | char *out0 = out;
|
---|
[1457] | 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)
|
---|
[2551] | 43 | out += sprintf (out, "%*s-0", w-2, "");
|
---|
[1457] | 44 | else
|
---|
[2551] | 45 | out += sprintf (out, "%*d", w, isneg ? -d : d);
|
---|
[1457] | 46 |
|
---|
| 47 | /* do the rest */
|
---|
| 48 | switch (fracbase) {
|
---|
| 49 | case 60: /* dd:mm */
|
---|
| 50 | m = f/(fracbase/60);
|
---|
[2551] | 51 | out += sprintf (out, ":%02d", m);
|
---|
[1457] | 52 | break;
|
---|
| 53 | case 600: /* dd:mm.m */
|
---|
[2551] | 54 | out += sprintf (out, ":%02d.%1d", f/10, f%10);
|
---|
[1457] | 55 | break;
|
---|
| 56 | case 3600: /* dd:mm:ss */
|
---|
| 57 | m = f/(fracbase/60);
|
---|
| 58 | s = f%(fracbase/60);
|
---|
[2551] | 59 | out += sprintf (out, ":%02d:%02d", m, s);
|
---|
[1457] | 60 | break;
|
---|
| 61 | case 36000: /* dd:mm:ss.s*/
|
---|
| 62 | m = f/(fracbase/60);
|
---|
| 63 | s = f%(fracbase/60);
|
---|
[2551] | 64 | out += sprintf (out, ":%02d:%02d.%1d", m, s/10, s%10);
|
---|
[1457] | 65 | break;
|
---|
| 66 | case 360000: /* dd:mm:ss.ss */
|
---|
| 67 | m = f/(fracbase/60);
|
---|
| 68 | s = f%(fracbase/60);
|
---|
[2551] | 69 | out += sprintf (out, ":%02d:%02d.%02d", m, s/100, s%100);
|
---|
[1457] | 70 | break;
|
---|
| 71 | default:
|
---|
| 72 | printf ("fs_sexa: unknown fracbase: %d\n", fracbase);
|
---|
[2551] | 73 | abort();
|
---|
[1457] | 74 | }
|
---|
[2551] | 75 |
|
---|
| 76 | return (out - out0);
|
---|
[1457] | 77 | }
|
---|
| 78 |
|
---|
[3111] | 79 | /* put the given modified Julian date, jd, in out[] according to the given
|
---|
| 80 | * preference format.
|
---|
[2551] | 81 | * return number of characters written to out, not counting final '\0'.
|
---|
[1457] | 82 | */
|
---|
[2551] | 83 | int
|
---|
[3111] | 84 | fs_date (char out[], int format, double jd)
|
---|
[1457] | 85 | {
|
---|
[2551] | 86 | char *out0 = out;
|
---|
[1457] | 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 |
|
---|
[3111] | 97 | switch (format) {
|
---|
[1457] | 98 | case PREF_YMD:
|
---|
[2551] | 99 | out += sprintf (out, "%4d/%02d/%02.6g", y, m, d);
|
---|
[1457] | 100 | break;
|
---|
| 101 | case PREF_DMY:
|
---|
[2551] | 102 | out += sprintf (out, "%2.6g/%02d/%-4d", d, m, y);
|
---|
[1457] | 103 | break;
|
---|
| 104 | case PREF_MDY:
|
---|
[2551] | 105 | out += sprintf (out, "%2d/%02.6g/%-4d", m, d, y);
|
---|
[1457] | 106 | break;
|
---|
| 107 | default:
|
---|
[3111] | 108 | printf ("fs_date: bad date pref: %d\n", format);
|
---|
[2551] | 109 | abort();
|
---|
[1457] | 110 | }
|
---|
[2551] | 111 |
|
---|
| 112 | return (out - out0);
|
---|
[1457] | 113 | }
|
---|
| 114 |
|
---|
[2551] | 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.
|
---|
[1457] | 120 | */
|
---|
[2551] | 121 | int
|
---|
| 122 | f_scansexa (
|
---|
| 123 | const char *str0, /* input string */
|
---|
| 124 | double *dp) /* cracked value, if return 0 */
|
---|
[1457] | 125 | {
|
---|
[3111] | 126 | double a, b, c;
|
---|
| 127 | char str[256];
|
---|
[2551] | 128 | char *neg;
|
---|
[3111] | 129 | int isneg, r;
|
---|
[1457] | 130 |
|
---|
[2551] | 131 | /* copy str0 so we can play with it */
|
---|
| 132 | strncpy (str, str0, sizeof(str)-1);
|
---|
| 133 | str[sizeof(str)-1] = '\0';
|
---|
[1457] | 134 |
|
---|
[3111] | 135 | /* note first negative (but not fooled by neg exponent) */
|
---|
| 136 | isneg = 0;
|
---|
[2551] | 137 | neg = strchr(str, '-');
|
---|
[3111] | 138 | if (neg && (neg == str || (neg[-1] != 'E' && neg[-1] != 'e'))) {
|
---|
[2551] | 139 | *neg = ' ';
|
---|
[3111] | 140 | isneg = 1;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /* crack up to three components */
|
---|
| 144 | a = b = c = 0.0;
|
---|
[2551] | 145 | r = sscanf (str, "%lf%*[^0-9]%lf%*[^0-9]%lf", &a, &b, &c);
|
---|
| 146 | if (r < 1)
|
---|
| 147 | return (-1);
|
---|
[3111] | 148 |
|
---|
| 149 | /* back to one value, restoring neg */
|
---|
| 150 | *dp = (c/60.0 + b)/60.0 + a;
|
---|
| 151 | if (isneg)
|
---|
[2551] | 152 | *dp *= -1;
|
---|
| 153 | return (0);
|
---|
[1457] | 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,
|
---|
[2818] | 159 | * the slashes may also be spaces or colons.
|
---|
[2551] | 160 | * a lone component with a decimal point is considered a year.
|
---|
[1457] | 161 | */
|
---|
| 162 | void
|
---|
[2551] | 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)
|
---|
[1457] | 169 | {
|
---|
[2818] | 170 | double X,Y,Z; /* the three components */
|
---|
[2551] | 171 | int n;
|
---|
[1457] | 172 |
|
---|
[2818] | 173 | X = Y = Z = 0.0;
|
---|
| 174 | n = sscanf (bp, "%lf%*[/: ]%lf%*[/: ]%lf", &X, &Y, &Z);
|
---|
[2551] | 175 | if (n == 1 && (strchr(bp, '.')
|
---|
[2818] | 176 | || (pref == PREF_MDY && (X < 1 || X > 12))
|
---|
| 177 | || (pref == PREF_DMY && (X < 1 || X > 31)))) {
|
---|
[1457] | 178 | double Mjd;
|
---|
[2818] | 179 | year_mjd (X, &Mjd);
|
---|
[1457] | 180 | mjd_cal (Mjd, m, d, y);
|
---|
[2551] | 181 | } else {
|
---|
| 182 | switch (pref) {
|
---|
| 183 | case PREF_MDY:
|
---|
[2818] | 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;
|
---|
[2551] | 190 | break;
|
---|
| 191 | case PREF_YMD:
|
---|
[2818] | 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;
|
---|
[2551] | 198 | break;
|
---|
| 199 | case PREF_DMY:
|
---|
[2818] | 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;
|
---|
[2551] | 206 | break;
|
---|
| 207 | }
|
---|
[1457] | 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /* For RCS Only -- Do Not Edit */
|
---|
[3111] | 212 | static char *rcsid[2] = {(char *)rcsid, "@(#) $RCSfile: formats.c,v $ $Date: 2006-11-22 13:53:29 $ $Revision: 1.6 $ $Name: not supported by cvs2svn $"};
|
---|