[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 |
|
---|
| 79 | /* put the given modified Julian date, jd, in out[] according to preference
|
---|
| 80 | * format.
|
---|
[2551] | 81 | * return number of characters written to out, not counting final '\0'.
|
---|
[1457] | 82 | */
|
---|
[2551] | 83 | int
|
---|
| 84 | fs_date (char out[], double jd)
|
---|
[1457] | 85 | {
|
---|
| 86 | int p = pref_get (PREF_DATE_FORMAT);
|
---|
[2551] | 87 | char *out0 = out;
|
---|
[1457] | 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:
|
---|
[2551] | 100 | out += sprintf (out, "%4d/%02d/%02.6g", y, m, d);
|
---|
[1457] | 101 | break;
|
---|
| 102 | case PREF_DMY:
|
---|
[2551] | 103 | out += sprintf (out, "%2.6g/%02d/%-4d", d, m, y);
|
---|
[1457] | 104 | break;
|
---|
| 105 | case PREF_MDY:
|
---|
[2551] | 106 | out += sprintf (out, "%2d/%02.6g/%-4d", m, d, y);
|
---|
[1457] | 107 | break;
|
---|
| 108 | default:
|
---|
| 109 | printf ("fs_date: bad date pref: %d\n", p);
|
---|
[2551] | 110 | abort();
|
---|
[1457] | 111 | }
|
---|
[2551] | 112 |
|
---|
| 113 | return (out - out0);
|
---|
[1457] | 114 | }
|
---|
| 115 |
|
---|
[2551] | 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.
|
---|
[1457] | 121 | */
|
---|
[2551] | 122 | int
|
---|
| 123 | f_scansexa (
|
---|
| 124 | const char *str0, /* input string */
|
---|
| 125 | double *dp) /* cracked value, if return 0 */
|
---|
[1457] | 126 | {
|
---|
[2551] | 127 | double a = 0, b = 0, c = 0;
|
---|
| 128 | char str[128];
|
---|
| 129 | char *neg;
|
---|
| 130 | int r;
|
---|
[1457] | 131 |
|
---|
[2551] | 132 | /* copy str0 so we can play with it */
|
---|
| 133 | strncpy (str, str0, sizeof(str)-1);
|
---|
| 134 | str[sizeof(str)-1] = '\0';
|
---|
[1457] | 135 |
|
---|
[2551] | 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);
|
---|
[1457] | 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,
|
---|
[2818] | 151 | * the slashes may also be spaces or colons.
|
---|
[2551] | 152 | * a lone component with a decimal point is considered a year.
|
---|
[1457] | 153 | */
|
---|
| 154 | void
|
---|
[2551] | 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)
|
---|
[1457] | 161 | {
|
---|
[2818] | 162 | double X,Y,Z; /* the three components */
|
---|
[2551] | 163 | int n;
|
---|
[1457] | 164 |
|
---|
[2818] | 165 | X = Y = Z = 0.0;
|
---|
| 166 | n = sscanf (bp, "%lf%*[/: ]%lf%*[/: ]%lf", &X, &Y, &Z);
|
---|
[2551] | 167 | if (n == 1 && (strchr(bp, '.')
|
---|
[2818] | 168 | || (pref == PREF_MDY && (X < 1 || X > 12))
|
---|
| 169 | || (pref == PREF_DMY && (X < 1 || X > 31)))) {
|
---|
[1457] | 170 | double Mjd;
|
---|
[2818] | 171 | year_mjd (X, &Mjd);
|
---|
[1457] | 172 | mjd_cal (Mjd, m, d, y);
|
---|
[2551] | 173 | } else {
|
---|
| 174 | switch (pref) {
|
---|
| 175 | case PREF_MDY:
|
---|
[2818] | 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;
|
---|
[2551] | 182 | break;
|
---|
| 183 | case PREF_YMD:
|
---|
[2818] | 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;
|
---|
[2551] | 190 | break;
|
---|
| 191 | case PREF_DMY:
|
---|
[2818] | 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;
|
---|
[2551] | 198 | break;
|
---|
| 199 | }
|
---|
[1457] | 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | /* For RCS Only -- Do Not Edit */
|
---|
[2818] | 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 $"};
|
---|