1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
//! # Nusantara Calendar Core
//!
//! This crate provides the foundational types, traits, and utilities for the
//! nusantara-calendar workspace, which implements traditional Indonesian
//! calendar systems with modern Rust ergonomics.
//!
//! ## Julian Day Number (JDN) Pivot
//!
//! This crate uses the Julian Day Number (JDN) system as the central pivot for
//! all calendar conversions. JDN provides a continuous count of days since
//! noon Universal Time on January 1, 4713 BCE (Julian calendar), making it
//! ideal for converting between different calendar systems.
//!
//! The JDN system allows us to:
//! - Convert any date to/from Gregorian calendar
//! - Implement bidirectional conversions between calendar systems
//! - Perform date arithmetic with consistent results
//! - Handle historical dates across different calendar reforms
//!
//! ## Algorithm Source
//!
//! The Gregorian to JDN conversion algorithm implemented in this crate follows
//! the standard formula from:
//!
//! **Meeus, Jean.** *Astronomical Algorithms*, 2nd Edition.
//! Willmann-Blohm, 1998. Chapter 7: "Julian Day".
//!
//! This reference implementation provides accurate conversions for the full
//! range of historical dates supported by the JDN system (approximately
//! 262,000 BCE to 262,000 CE).
//!
//! ## Core Components
//!
//! ### Types
//! - [`JDN`] - Julian Day Number type alias (`i64`)
//! - [`CycleYear`] - Type for cycle-year fields (`u32`)
//! - [`SubYearPosition`] - Type for sub-year positions (`u8`)
//!
//! ### Traits
//! - [`CalendarDate`] - Core interface for calendar implementations
//! - [`CalendarMetadata`] - Access to calendar metadata and cultural context
//! - [`HasAuspiciousness`] - Auspiciousness calculations for Indonesian calendars
//!
//! ### Error Handling
//! - [`CalendarError`] - Comprehensive error types with detailed context
//! - [`stub!`] macro for marking unimplemented features
//!
//! ### Cultural Features
//! - [`Activity`] - Indonesian cultural activities for auspiciousness evaluation
//! - [`AuspiciousnessLevel`] - Favorability levels for activities and dates
//!
//! ## Platform Support
//!
//! This crate supports multiple compilation targets:
//! - **std**: Standard library with full functionality
//! - **`no_std`**: Embedded systems with `alloc` support
//! - **WASM**: WebAssembly targets for browser usage
//!
//! ## Example Usage
//!
//! ```rust
//! use calendar_core::{gregorian_to_jdn, jdn_to_gregorian, CalendarDate};
//!
//! // Convert Gregorian to JDN
//! let jdn = gregorian_to_jdn(2024, 3, 15);
//!
//! // Convert back to Gregorian
//! let (year, month, day) = jdn_to_gregorian(jdn);
//!
//! // Use with CalendarDate trait implementations
//! // let calendar_date = MyCalendar::from_gregorian(2024, 3, 15)?;
//! ```
//!
//! ## Indonesian Calendar Context
//!
//! This crate is specifically designed to support the rich diversity of
//! Indonesian calendar systems, including:
//! - Javanese calendar (Saka and Islamic integration)
//! - Balinese calendar (Pawukon cycle)
//! - Hijri/Islamic calendar
//! - Chinese calendar integration
//! - Various regional ethnic calendars
//!
//! Each calendar system can implement the core traits while maintaining
//! cultural authenticity and computational accuracy.
extern crate alloc;
// Re-export commonly used items
pub use Error;
use String;
// Modules
// Re-export auspiciousness types
pub use ;
/// Julian Day Number type alias
///
/// Uses i64 to support the full range of historical dates
/// from approximately 262,000 BCE to 262,000 CE
pub type JDN = i64;
/// Type for cycle-year fields (e.g., year in a 60-year cycle)
///
/// Uses u32 to support large cycles while remaining efficient
pub type CycleYear = u32;
/// Type for sub-year positions (e.g., month, day, weekday)
///
/// Uses u8 for compact storage of values 0-255
pub type SubYearPosition = u8;
/// Core error types for calendar operations
/// Trait for calendar date implementations
///
/// This trait defines the core interface that all calendar systems
/// in the nusantara-calendar workspace must implement. It provides
/// bidirectional conversion with Julian Day Numbers and Gregorian dates,
/// serving as the foundation for all calendar implementations.
///
/// # Required Implementations
///
/// Calendar types must implement:
/// - [`Self::from_jdn()`] - Convert from JDN to calendar date
/// - [`Self::to_jdn()`] - Convert from calendar date to JDN
/// - [`Self::calendar_name()`] - Return the calendar system name
/// - [`Self::validate_range()`] - Check if date is within supported range
///
/// # Provided Implementations
///
/// Default implementations are provided for:
/// - [`Self::from_gregorian()`] - Convert via JDN intermediate
/// - [`Self::to_gregorian()`] - Convert via JDN intermediate
///
/// # Implementation Guidelines
///
/// When implementing this trait:
///
/// 1. **Use JDN as the canonical representation** - All conversions should
/// go through JDN to ensure consistency across calendar systems
///
/// 2. **Validate date ranges** - Each calendar should define reasonable
/// bounds and return appropriate errors for out-of-range dates
///
/// 3. **Handle cultural specifics** - Account for calendar-specific
/// rules like leap months, intercalary days, etc.
///
/// 4. **Provide clear error messages** - Use descriptive error messages
/// that help users understand validation failures
///
/// # Example Implementation
///
/// ```rust
/// use calendar_core::{CalendarDate, CalendarError, JDN};
///
/// #[derive(Debug, Clone, PartialEq, Eq)]
/// struct MyCalendarDate {
/// year: i32,
/// month: u8,
/// day: u8,
/// }
///
/// impl CalendarDate for MyCalendarDate {
/// fn from_jdn(jdn: JDN) -> Result<Self, CalendarError> {
/// // Convert JDN to calendar date
/// // Implementation depends on calendar rules
/// todo!("Implement JDN to calendar conversion")
/// }
///
/// fn to_jdn(&self) -> JDN {
/// // Convert calendar date to JDN
/// // Implementation depends on calendar rules
/// todo!("Implement calendar to JDN conversion")
/// }
///
/// fn calendar_name() -> &'static str {
/// "My Calendar System"
/// }
///
/// fn validate_range(&self) -> Result<(), CalendarError> {
/// // Check if date is within supported range
/// if self.year < 1 || self.year > 9999 {
/// return Err(CalendarError::OutOfRange(
/// "Year must be between 1 and 9999".to_string()
/// ));
/// }
/// Ok(())
/// }
/// }
/// ```
///
/// # Performance Considerations
///
/// - JDN conversions are the most computationally expensive operations
/// - Cache results when performing repeated conversions
/// - Use the provided Gregorian conversion methods for convenience
/// - Consider lazy evaluation for complex calendar calculations
/// Trait for calendar metadata and information
///
/// This trait provides access to calendar-specific metadata such as
/// epoch information, cycle information, and cultural context. Implementers
/// should provide accurate historical and cultural information about their
/// calendar systems.
///
/// # Implementation Requirements
///
/// Calendar implementations must:
/// - Provide accurate epoch dates with historical sources
/// - Document cycle lengths with cultural references
/// - Include cultural origin information
/// - Optionally provide reference sources for verification
///
/// # Reference Sources Contract
///
/// Implementers of this trait should document their reference sources for:
/// - Epoch dates and historical accuracy
/// - Cycle calculations and astronomical basis
/// - Cultural practices and calendar rules
/// - Regional variations and historical changes
///
/// Recommended reference sources include:
/// - Historical astronomical records
/// - Cultural and religious texts
/// - Academic research on calendar systems
/// - Government or institutional standards
///
/// # Example
///
/// ```rust
/// use calendar_core::{CalendarMetadata, JDN};
///
/// struct MyCalendar;
///
/// impl CalendarMetadata for MyCalendar {
/// fn epoch() -> JDN {
/// // Epoch based on historical records
/// 1948439 // Example: March 22, 2024 CE
/// }
///
/// fn cycle_length() -> Option<u32> {
/// Some(60) // 60-year cycle common in Indonesian calendars
/// }
///
/// fn description() -> &'static str {
/// "Traditional Indonesian calendar with 60-year cycle"
/// }
///
/// fn cultural_origin() -> &'static str {
/// "Javanese court calendar system, integrated with Islamic calendar"
/// }
/// }
/// ```
/// Trait for calendars that have auspiciousness calculations
///
/// This trait enables Indonesian calendar systems to provide cultural
/// auspiciousness evaluations for various activities. Many Indonesian
/// traditional calendars include concepts of auspicious and inauspicious
/// days that influence important life events and activities.
///
/// # Cultural Context
///
/// Indonesian calendar systems often incorporate auspiciousness concepts
/// from various cultural and religious traditions:
///
/// - **Javanese calendar**: Balancing elements, market days, and spiritual aspects
/// - **Balinese calendar**: Complex system of auspicious/inauspicious days
/// - **Chinese integration**: Feng shui and zodiac considerations
/// - **Islamic integration**: Religious observances and favorable timing
///
/// # Implementation Requirements
///
/// Calendar systems implementing this trait should:
///
/// 1. **Define Activity Types**: Specify which cultural activities are relevant
/// 2. **Provide Auspiciousness Levels**: Use the standard 5-level system
/// 3. **Implement Cultural Logic**: Apply authentic cultural rules and calculations
/// 4. **Document Sources**: Reference cultural texts or expert knowledge
///
/// # Example Implementation
///
/// ```rust
/// use calendar_core::{HasAuspiciousness, Activity, AuspiciousnessLevel};
///
/// struct MyCalendarDate {
/// // Calendar date fields
/// }
///
/// impl HasAuspiciousness for MyCalendarDate {
/// type Activity = Activity;
/// type AuspiciousnessLevel = AuspiciousnessLevel;
///
/// fn auspiciousness_for(&self, activity: &Activity) -> Self::AuspiciousnessLevel {
/// match activity {
/// Activity::Marriage => {
/// // Check if this date is auspicious for marriage
/// // Based on cultural rules and calculations
/// // Implementation would go here
/// AuspiciousnessLevel::Auspicious // Placeholder
/// }
/// Activity::Building => {
/// // Check auspiciousness for construction
/// AuspiciousnessLevel::Neutral // Placeholder
/// }
/// // Handle other activities...
/// _ => AuspiciousnessLevel::Neutral,
/// }
/// }
///
/// fn is_auspicious_day(&self) -> bool {
/// // General auspiciousness for the day
/// // Implementation would calculate daily auspiciousness
/// true // Placeholder
/// }
/// }
/// ```
///
/// # Performance Considerations
///
/// - Auspiciousness calculations can be computationally intensive
/// - Consider caching results for frequently accessed dates
/// - Some calculations may depend on complex astronomical data
/// - Balance accuracy with performance for real-time applications
/// Macro for stub implementations
///
/// Used to mark features that are not yet implemented but
/// have defined interfaces.
/// Basic Gregorian to Julian Day Number conversion
///
/// Implements the standard Gregorian calendar algorithm
/// from the Julian Day Number Wikipedia page and astronomical sources.
///
/// # Arguments
/// * `year` - Gregorian year (CE, can be negative for BCE)
/// * `month` - Gregorian month (1-12)
/// * `day` - Gregorian day (1-31, depending on month)
///
/// # Returns
/// Julian Day Number for the given Gregorian date
/// Basic Julian Day Number to Gregorian conversion
///
/// Implements the canonical Fliegel & van Flandern (1968) algorithm
/// from the U.S. Naval Observatory, which is the authoritative inverse
/// of the Gregorian to JDN conversion.
///
/// # Arguments
/// * `jdn` - Julian Day Number
///
/// # Returns
/// Tuple of (year, month, day) in Gregorian calendar
///
/// # Panics
/// Panics if JDN is outside the supported range for Gregorian conversion
/// (approximately -2,147,483,648 to 2,147,483,647)
///
/// # Reference
/// Fliegel, H. F. & van Flandern, T. C. 1968, "A Machine Algorithm for
/// Processing Calendar Dates", Communications of the ACM, 11, 657.
/// [https://aa.usno.navy.mil/faq/JD_formula](https://aa.usno.navy.mil/faq/JD_formula)