playtime :
Not fully tested
Credit to Jon Flemming for a lisp routine ( JUL_CAL.LSP ;;; Jon Fleming 9/22/91 ) which I converted .
;;; The formulae for the date calculations are modified from a sample
;;; application shipped with Doug Hamilton's C Shell, which in turn
;;; are based on routines published in Computer Language, December
;;; 1990. The version here is modified to work properly with
;;; AutoCAD.
So this is almost full circle.
It could do with some optimisation, but I ran out of time today
// (C) CodeHimBelonga kdub
using System;
using System.Globalization;
using System.Windows;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Exception = System.Exception;
[assembly
: CommandClass
(typeof(kdubTesting
.JulianConversion))]
namespace kdubTesting
{
class JulianConversion
{
[CommandMethod("DoIt")]
public void JulianConversion_02()
{
var tdcreate = (double)AcadApp.GetSystemVariable("TDCREATE");
DateTime dt = ConvertAcadJulianToDateTime(tdcreate);
AcadApp.ShowAlertDialog(dt.ToString("G", DateTimeFormatInfo.InvariantInfo));
}
private DateTime ConvertAcadJulianToDateTime(double julianDate)
{
DateTime date;
try
{
double z = Math.Floor(julianDate);
double w = Math.Floor((z - 1867216.25) / 36524.25);
double x = Math.Floor(w / 4);
double a = (z + 1 + w - x);
double b = a + 1524;
double c = Math.Floor((b - 122.1) / 365.25);
double d = Math.Floor(365.25 * c);
double e = Math.Floor((b - d) / 30.6001);
double f = Math.Floor(30.6001 * e);
int day = Convert.ToInt32(b - d - f);
int m = Convert.ToInt32((e < 14) ? (e - 2) : (e - 14));
int month = (m + 1);
int y = Convert.ToInt32((m > 1) ? (c - 4716) : (c - 4715));
int year = (y == 0) ? (y - 1) : y;
//
// strip the integer
double t = (julianDate - z);
// hours since midnight
double hours = Math.Floor(t * 24.0);
// temp value
double mins = t - (hours / 24.0);
// 1440 minutes per day
double minutes = Math.Floor(mins * 1440.0);
//
double seconds = Math.Floor((mins - (minutes / 1440.0)) * 86400.0);
date
= new DateTime
(year, month, day, Convert
.ToInt32(hours
), Convert
.ToInt32(minutes
), Convert
.ToInt32(seconds
)); return date;
}
catch (ArgumentOutOfRangeException ex)
{
MessageBox.Show("Julian date could not be converted:\n" + ex.Message,
"Conversion Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (Exception ex)
{
MessageBox.Show("Error converting Julian date:\n" + ex.Message, "Conversion Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
return date;
}
}
}