1
+ #![ cfg_attr( not( test) , no_std) ]
1
2
#![ cfg_attr( docsrs, feature( doc_cfg) ) ]
2
3
//! `julian` is a Rust library for converting between [Julian day numbers][jdn]
3
4
//! and dates in the [Gregorian calendar][] (either proleptic or with the
12
13
//! Features
13
14
//! ========
14
15
//!
15
- //! The `julian` crate has the following optional feature:
16
+ //! The `julian` crate has the following optional features:
17
+ //!
18
+ //! - `std` — This feature is enabled by default; disable it to build in no-std
19
+ //! mode. When this feature is disabled, functions that use
20
+ //! [`std::time::SystemTime`] are not available.
16
21
//!
17
22
//! - `chrono` — Enables converting values of certain `julian` types to the
18
23
//! corresponding [`chrono`] types and *vice versa*.
@@ -172,10 +177,15 @@ pub mod iter;
172
177
pub mod ncal;
173
178
use crate :: errors:: * ;
174
179
use crate :: iter:: * ;
175
- use std:: cmp:: Ordering ;
176
- use std:: fmt;
177
- use std:: ops:: RangeInclusive ;
178
- use std:: str:: FromStr ;
180
+ use core:: cmp:: Ordering ;
181
+ use core:: fmt;
182
+ use core:: ops:: RangeInclusive ;
183
+ use core:: str:: FromStr ;
184
+
185
+ #[ cfg( feature = "std" ) ]
186
+ extern crate std;
187
+
188
+ #[ cfg( feature = "std" ) ]
179
189
use std:: time:: { SystemTime , UNIX_EPOCH } ;
180
190
181
191
/// Type used for Julian day numbers in this crate
@@ -396,6 +406,8 @@ impl Calendar {
396
406
/// converting the time. This can only happen if the system time in UTC is
397
407
/// before -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03
398
408
/// (5874777-10-17 O.S.).
409
+ #[ cfg( feature = "std" ) ]
410
+ #[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
399
411
pub fn now ( & self ) -> Result < ( Date , u32 ) , ArithmeticError > {
400
412
self . at_system_time ( SystemTime :: now ( ) )
401
413
}
@@ -409,6 +421,8 @@ impl Calendar {
409
421
/// converting the time. This can only happen if the system time in UTC is
410
422
/// before -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03
411
423
/// (5874777-10-17 O.S.).
424
+ #[ cfg( feature = "std" ) ]
425
+ #[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
412
426
pub fn at_system_time ( & self , t : SystemTime ) -> Result < ( Date , u32 ) , ArithmeticError > {
413
427
let ( jdn, secs) = system2jdn ( t) ?;
414
428
Ok ( ( self . at_jdn ( jdn) , secs) )
@@ -2134,21 +2148,32 @@ impl FromStr for Month {
2134
2148
/// Parses a month from either its English name or just the first three
2135
2149
/// letters of the name. Input is treated case-insensitively.
2136
2150
fn from_str ( s : & str ) -> Result < Month , ParseMonthError > {
2137
- use Month :: * ;
2138
- match s. to_ascii_lowercase ( ) . as_str ( ) {
2139
- "january" | "jan" => Ok ( January ) ,
2140
- "february" | "feb" => Ok ( February ) ,
2141
- "march" | "mar" => Ok ( March ) ,
2142
- "april" | "apr" => Ok ( April ) ,
2143
- "may" => Ok ( May ) ,
2144
- "june" | "jun" => Ok ( June ) ,
2145
- "july" | "jul" => Ok ( July ) ,
2146
- "august" | "aug" => Ok ( August ) ,
2147
- "september" | "sep" => Ok ( September ) ,
2148
- "october" | "oct" => Ok ( October ) ,
2149
- "november" | "nov" => Ok ( November ) ,
2150
- "december" | "dec" => Ok ( December ) ,
2151
- _ => Err ( ParseMonthError ) ,
2151
+ if s. eq_ignore_ascii_case ( "january" ) || s. eq_ignore_ascii_case ( "jan" ) {
2152
+ Ok ( Month :: January )
2153
+ } else if s. eq_ignore_ascii_case ( "february" ) || s. eq_ignore_ascii_case ( "feb" ) {
2154
+ Ok ( Month :: February )
2155
+ } else if s. eq_ignore_ascii_case ( "march" ) || s. eq_ignore_ascii_case ( "mar" ) {
2156
+ Ok ( Month :: March )
2157
+ } else if s. eq_ignore_ascii_case ( "april" ) || s. eq_ignore_ascii_case ( "apr" ) {
2158
+ Ok ( Month :: April )
2159
+ } else if s. eq_ignore_ascii_case ( "may" ) {
2160
+ Ok ( Month :: May )
2161
+ } else if s. eq_ignore_ascii_case ( "june" ) || s. eq_ignore_ascii_case ( "jun" ) {
2162
+ Ok ( Month :: June )
2163
+ } else if s. eq_ignore_ascii_case ( "july" ) || s. eq_ignore_ascii_case ( "jul" ) {
2164
+ Ok ( Month :: July )
2165
+ } else if s. eq_ignore_ascii_case ( "august" ) || s. eq_ignore_ascii_case ( "aug" ) {
2166
+ Ok ( Month :: August )
2167
+ } else if s. eq_ignore_ascii_case ( "september" ) || s. eq_ignore_ascii_case ( "sep" ) {
2168
+ Ok ( Month :: September )
2169
+ } else if s. eq_ignore_ascii_case ( "october" ) || s. eq_ignore_ascii_case ( "oct" ) {
2170
+ Ok ( Month :: October )
2171
+ } else if s. eq_ignore_ascii_case ( "november" ) || s. eq_ignore_ascii_case ( "nov" ) {
2172
+ Ok ( Month :: November )
2173
+ } else if s. eq_ignore_ascii_case ( "december" ) || s. eq_ignore_ascii_case ( "dec" ) {
2174
+ Ok ( Month :: December )
2175
+ } else {
2176
+ Err ( ParseMonthError )
2152
2177
}
2153
2178
}
2154
2179
}
@@ -2366,16 +2391,22 @@ impl FromStr for Weekday {
2366
2391
/// Parses a weekday from either its English name or just the first three
2367
2392
/// letters of the name. Input is treated case-insensitively.
2368
2393
fn from_str ( s : & str ) -> Result < Weekday , ParseWeekdayError > {
2369
- use Weekday :: * ;
2370
- match s. to_ascii_lowercase ( ) . as_str ( ) {
2371
- "sunday" | "sun" => Ok ( Sunday ) ,
2372
- "monday" | "mon" => Ok ( Monday ) ,
2373
- "tuesday" | "tue" => Ok ( Tuesday ) ,
2374
- "wednesday" | "wed" => Ok ( Wednesday ) ,
2375
- "thursday" | "thu" => Ok ( Thursday ) ,
2376
- "friday" | "fri" => Ok ( Friday ) ,
2377
- "saturday" | "sat" => Ok ( Saturday ) ,
2378
- _ => Err ( ParseWeekdayError ) ,
2394
+ if s. eq_ignore_ascii_case ( "sunday" ) || s. eq_ignore_ascii_case ( "sun" ) {
2395
+ Ok ( Weekday :: Sunday )
2396
+ } else if s. eq_ignore_ascii_case ( "monday" ) || s. eq_ignore_ascii_case ( "mon" ) {
2397
+ Ok ( Weekday :: Monday )
2398
+ } else if s. eq_ignore_ascii_case ( "tuesday" ) || s. eq_ignore_ascii_case ( "tue" ) {
2399
+ Ok ( Weekday :: Tuesday )
2400
+ } else if s. eq_ignore_ascii_case ( "wednesday" ) || s. eq_ignore_ascii_case ( "wed" ) {
2401
+ Ok ( Weekday :: Wednesday )
2402
+ } else if s. eq_ignore_ascii_case ( "thursday" ) || s. eq_ignore_ascii_case ( "thu" ) {
2403
+ Ok ( Weekday :: Thursday )
2404
+ } else if s. eq_ignore_ascii_case ( "friday" ) || s. eq_ignore_ascii_case ( "fri" ) {
2405
+ Ok ( Weekday :: Friday )
2406
+ } else if s. eq_ignore_ascii_case ( "saturday" ) || s. eq_ignore_ascii_case ( "sat" ) {
2407
+ Ok ( Weekday :: Saturday )
2408
+ } else {
2409
+ Err ( ParseWeekdayError )
2379
2410
}
2380
2411
}
2381
2412
}
@@ -2446,6 +2477,8 @@ impl From<Weekday> for chrono::Weekday {
2446
2477
/// conversion. This can only happen if the system time in UTC is before
2447
2478
/// -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03 (5874777-10-17
2448
2479
/// O.S.).
2480
+ #[ cfg( feature = "std" ) ]
2481
+ #[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
2449
2482
pub fn system2jdn ( t : SystemTime ) -> Result < ( Jdnum , u32 ) , ArithmeticError > {
2450
2483
let ts = match t. duration_since ( UNIX_EPOCH ) {
2451
2484
Ok ( d) => i64:: try_from ( d. as_secs ( ) ) ,
0 commit comments