365size_t strfMJD(
char *
buf,
size_t len,
const char *format,
const MJDtime *MJD,
int forceJulian,
int inleap )
377 int year, month, day, hour,
min, ysign, second, d, y;
379 int i, secsSince1970;
380 int nplaces, fmtlen, slen;
384 double sec, sec_fraction;
385 int w, doy, days_in_wk1;
387 const char *monthText;
390 size_t last = len - 1;
391 MJDtime nMJD_value, *nMJD = &nMJD_value;
392 char dynamic_format[10];
396 fmtlen = (int) strlen( format );
400 char next = format[i];
406 if ( isdigit( next ) != 0 )
408 nplaces = (int) strtol( &( format[i] ), NULL, 10 );
409 if ( nplaces > resolution )
410 resolution = nplaces;
412 else if ( next ==
'.' )
421 shiftPlaces = pow( 10, (
double) resolution );
423 nMJD->
time_sec += 0.5 / shiftPlaces;
431 breakDownMJD( &year, &month, &day, &hour, &
min, &sec, nMJD, forceJulian );
444 sec = floor( sec * shiftPlaces ) / shiftPlaces;
451 char next = format[i];
465 else if ( next ==
'a' )
469 strncat( &(
buf[posn] ), dayText, last - posn );
470 posn = strlen(
buf );
474 else if ( next ==
'A' )
478 strncat( &(
buf[posn] ), dayText, last - posn );
479 posn = strlen(
buf );
483 else if ( next ==
'b' || next ==
'h' )
487 strncat( &(
buf[posn] ), monthText, last - posn );
488 posn = strlen(
buf );
492 else if ( next ==
'B' )
496 strncat( &(
buf[posn] ), monthText, last - posn );
497 posn = strlen(
buf );
501 else if ( next ==
'c' )
507 sprintf( DateTime,
"%s %s %02d %02d:%02d:%02d %04d", dayText, monthText, day, hour,
min, second, year );
509 sprintf( DateTime,
"%s %s %02d %02d:%02d:%02d -%04d", dayText, monthText, day, hour,
min, second, year );
511 strncat( &(
buf[posn] ), DateTime, last - posn );
512 posn = strlen(
buf );
516 else if ( next ==
'C' )
519 int century = year / 100;
521 sprintf( DateTime,
"%02d", century );
523 sprintf( DateTime,
"-%02d", century + 1 );
525 strncat( &(
buf[posn] ), DateTime, last - posn );
526 posn = strlen(
buf );
530 else if ( next ==
'd' )
533 sprintf( DateTime,
"%02d", day );
535 strncat( &(
buf[posn] ), DateTime, last - posn );
536 posn = strlen(
buf );
540 else if ( next ==
'D' )
545 sprintf( DateTime,
"%02d/%02d/%02d", month + 1, day, y );
547 sprintf( DateTime,
"%02d/%02d/-%02d", month + 1, day, y );
549 strncat( &(
buf[posn] ), DateTime, last - posn );
550 posn = strlen(
buf );
554 else if ( next ==
'e' )
558 sprintf( DateTime,
" %01d", day );
560 sprintf( DateTime,
"%02d", day );
562 strncat( &(
buf[posn] ), DateTime, last - posn );
563 posn = strlen(
buf );
567 else if ( next ==
'F' )
571 sprintf( DateTime,
"%04d-%02d-%02d", year, month + 1, day );
573 sprintf( DateTime,
"-%04d-%02d-%02d", year, month + 1, day );
575 strncat( &(
buf[posn] ), DateTime, last - posn );
576 posn = strlen(
buf );
580 else if ( next ==
'H' )
583 sprintf( DateTime,
"%02d", hour );
585 strncat( &(
buf[posn] ), DateTime, last - posn );
586 posn = strlen(
buf );
590 else if ( next ==
'I' )
594 sprintf( DateTime,
"%02d", hour + 12 );
595 else if ( hour > 12 )
596 sprintf( DateTime,
"%02d", hour - 12 );
598 sprintf( DateTime,
"%02d", hour );
600 strncat( &(
buf[posn] ), DateTime, last - posn );
601 posn = strlen(
buf );
605 else if ( next ==
'j' )
608 getYAD( &y1, &ifleapyear, &doy, nMJD, forceJulian );
609 sprintf( DateTime,
"%03d", doy );
611 strncat( &(
buf[posn] ), DateTime, last - posn );
612 posn = strlen(
buf );
616 else if ( next ==
'k' )
620 sprintf( DateTime,
" %01d", hour );
622 sprintf( DateTime,
"%02d", hour );
624 strncat( &(
buf[posn] ), DateTime, last - posn );
625 posn = strlen(
buf );
629 else if ( next ==
'l' )
633 sprintf( DateTime,
"%02d", hour + 12 );
634 else if ( hour < 10 )
635 sprintf( DateTime,
" %01d", hour );
636 else if ( hour <= 12 )
637 sprintf( DateTime,
"%02d", hour );
638 else if ( hour < 22 )
639 sprintf( DateTime,
" %01d", hour - 12 );
641 sprintf( DateTime,
"%02d", hour - 12 );
643 strncat( &(
buf[posn] ), DateTime, last - posn );
644 posn = strlen(
buf );
648 else if ( next ==
'm' )
651 sprintf( DateTime,
"%02d", month + 1 );
653 strncat( &(
buf[posn] ), DateTime, last - posn );
654 posn = strlen(
buf );
658 else if ( next ==
'M' )
661 sprintf( DateTime,
"%02d",
min );
663 strncat( &(
buf[posn] ), DateTime, last - posn );
664 posn = strlen(
buf );
668 else if ( next ==
'n' )
676 else if ( next ==
'p' )
680 sprintf( DateTime,
"AM" );
682 sprintf( DateTime,
"PM" );
684 strncat( &(
buf[posn] ), DateTime, last - posn );
685 posn = strlen(
buf );
689 else if ( next ==
'r' )
693 sprintf( DateTime,
"%02d:%02d:%02d AM", hour + 12,
min, second );
694 else if ( hour > 12 )
695 sprintf( DateTime,
"%02d:%02d:%02d PM", hour - 12,
min, second );
696 else if ( hour == 12 )
697 sprintf( DateTime,
"%02d:%02d:%02d PM", hour,
min, second );
699 sprintf( DateTime,
"%02d:%02d:%02d AM", hour,
min, second );
701 strncat( &(
buf[posn] ), DateTime, last - posn );
702 posn = strlen(
buf );
706 else if ( next ==
'R' )
709 sprintf( DateTime,
"%02d:%02d", hour,
min );
711 strncat( &(
buf[posn] ), DateTime, last - posn );
712 posn = strlen(
buf );
716 else if ( next ==
'S' )
719 if ( i + 2 < fmtlen && format[i + 1] ==
'%' && ( format[i + 2] ==
'.' || isdigit( format[i + 2] ) != 0 ) )
722 if ( format[i + 2] ==
'.' )
726 nplaces = (int) strtol( &( format[i + 2] ), NULL, 10 );
736 sprintf( DateTime,
"%02d", (
int) ( sec + 0.5 ) );
740 sprintf( dynamic_format,
"%%0%d.%df", nplaces + 3, nplaces );
741 sprintf( DateTime, dynamic_format, sec );
742 if ( format[i] ==
'.' )
744 slen = (int) strlen( DateTime ) - 1;
745 while ( DateTime[slen] ==
'0' && DateTime[slen - 1] !=
'.' )
747 DateTime[slen] =
'\0';
753 strncat( &(
buf[posn] ), DateTime, last - posn );
754 posn = strlen(
buf );
758 else if ( next ==
's' )
762 sprintf( DateTime,
"%d", secsSince1970 );
764 strncat( &(
buf[posn] ), DateTime, last - posn );
765 posn = strlen(
buf );
769 else if ( next ==
't' )
777 else if ( next ==
'T' )
780 sprintf( DateTime,
"%02d:%02d:%02d", hour,
min, second );
782 strncat( &(
buf[posn] ), DateTime, last - posn );
783 posn = strlen(
buf );
787 else if ( next ==
'U' )
790 getYAD( &y1, &ifleapyear, &doy, nMJD, forceJulian );
791 days_in_wk1 = ( nMJD->
base_day - doy - 4 ) % 7;
793 w = ( doy + 6 - days_in_wk1 ) / 7;
795 sprintf( DateTime,
"%02d", w );
797 strncat( &(
buf[posn] ), DateTime, last - posn );
798 posn = strlen(
buf );
802 else if ( next ==
'u' )
807 sprintf( DateTime,
"%01d", d );
809 strncat( &(
buf[posn] ), DateTime, last - posn );
810 posn = strlen(
buf );
814 else if ( next ==
'v' )
823 sprintf( DateTime,
" %01d-%s-%04d", day, monthText, year );
825 sprintf( DateTime,
"%02d-%s-%04d", day, monthText, year );
830 sprintf( DateTime,
" %01d-%s-(-)%04d", day, monthText, year );
832 sprintf( DateTime,
"%02d-%s-(-)%04d", day, monthText, year );
835 strncat( &(
buf[posn] ), DateTime, last - posn );
836 posn = strlen(
buf );
840 else if ( next ==
'V' )
843 getYAD( &y1, &ifleapyear, &doy, nMJD, forceJulian );
844 days_in_wk1 = ( nMJD->
base_day - doy - 3 ) % 7;
846 if ( days_in_wk1 <= 3 )
847 w = ( doy + 6 - days_in_wk1 ) / 7;
849 w = 1 + ( doy + 6 - days_in_wk1 ) / 7;
853 sprintf( DateTime,
"%02d", w );
855 strncat( &(
buf[posn] ), DateTime, last - posn );
856 posn = strlen(
buf );
860 else if ( next ==
'w' )
865 sprintf( DateTime,
"%01d", d );
867 strncat( &(
buf[posn] ), DateTime, last - posn );
868 posn = strlen(
buf );
872 else if ( next ==
'W' )
875 getYAD( &y1, &ifleapyear, &doy, nMJD, forceJulian );
876 days_in_wk1 = ( nMJD->
base_day - doy - 3 ) % 7;
878 w = ( doy + 6 - days_in_wk1 ) / 7;
880 sprintf( DateTime,
"%02d", w );
882 strncat( &(
buf[posn] ), DateTime, last - posn );
883 posn = strlen(
buf );
887 else if ( next ==
'x' )
893 sprintf( DateTime,
"%s %s %02d, %04d", dayText, monthText, day, year );
895 sprintf( DateTime,
"%s %s %02d, -%04d", dayText, monthText, day, year );
897 strncat( &(
buf[posn] ), DateTime, last - posn );
898 posn = strlen(
buf );
902 else if ( next ==
'X' )
905 sprintf( DateTime,
"%02d:%02d:%02d", hour,
min, second );
907 strncat( &(
buf[posn] ), DateTime, last - posn );
908 posn = strlen(
buf );
912 else if ( next ==
'y' )
918 sprintf( DateTime,
"%02d", y );
920 sprintf( DateTime,
"-%02d", y );
922 strncat( &(
buf[posn] ), DateTime, last - posn );
923 posn = strlen(
buf );
927 else if ( next ==
'Y' )
931 sprintf( DateTime,
"%04d", year );
933 sprintf( DateTime,
"-%04d", year );
935 strncat( &(
buf[posn] ), DateTime, last - posn );
936 posn = strlen(
buf );
940 else if ( next ==
'Z' )
944 strncat( &(
buf[posn] ),
"UTC Julian", last - posn );
946 strncat( &(
buf[posn] ),
"UTC Gregorian", last - posn );
948 posn = strlen(
buf );
952 else if ( next ==
'z' )
955 strncat( &(
buf[posn] ),
"+0000", last - posn );
956 posn = strlen(
buf );
960 else if ( next ==
'+' )
966 sprintf( DateTime,
"%s %s %02d %02d:%02d:%02d UTC %04d", dayText, monthText, day, hour,
min, second, year );
968 sprintf( DateTime,
"%s %s %02d %02d:%02d:%02d UTC -%04d", dayText, monthText, day, hour,
min, second, year );
970 strncat( &(
buf[posn] ), DateTime, last - posn );
971 posn = strlen(
buf );
975 else if ( next ==
'.' || isdigit( next ) != 0 )
982 nplaces = (int) strtol( &( format[i] ), NULL, 10 );
985 sec_fraction = sec - (int) sec;
986 sprintf( dynamic_format,
"%%-%d.%df", nplaces + 2, nplaces );
988 sprintf( DateTime, dynamic_format, sec_fraction );
989 while ( ( ptr = strrchr( &( DateTime[0] ),
' ' ) ) != NULL )
994 slen = (int) strlen( DateTime ) - 1;
995 while ( DateTime[slen] ==
'0' && DateTime[slen - 1] !=
'.' )
997 DateTime[slen] =
'\0';
1002 ptr = strchr( DateTime,
'.' );
1006 strncat( &(
buf[posn] ), ptr, last - posn );
1007 posn = strlen(
buf );