Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Support for no_std #341

Merged
merged 15 commits into from Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions Cargo.toml
Expand Up @@ -24,25 +24,26 @@ appveyor = { repository = "chronotope/chrono" }
name = "chrono"

[features]
default = ["clock"]
clock = ["time"]
default = ["clock", "std"]
alloc = []
std = []
clock = ["time", "std"]
wasmbind = ["wasm-bindgen", "js-sys"]

[dependencies]
libc = { version = "0.2", default-features = false }
time = { version = "0.1.39", optional = true }
num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3.20", optional = true }
serde = { version = "1", optional = true }
serde = { version = "1.0.99", default-features = false, optional = true }

[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "0.2", optional = true }
js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API

[dev-dependencies]
serde_json = { version = "1" }
serde_derive = { version = "1" }
serde_json = { version = "1", default-features = false }
serde_derive = { version = "1", default-features = false }
bincode = { version = "0.8.0" }
num-iter = { version = "0.1.35", default-features = false }
doc-comment = "0.3"
Expand Down
7 changes: 3 additions & 4 deletions Makefile
@@ -1,6 +1,8 @@
# this Makefile is mostly for the packaging convenience.
# casual users should use `cargo` to retrieve the appropriate version of Chrono.

CHANNEL=stable

.PHONY: all
all:
@echo 'Try `cargo build` instead.'
Expand All @@ -20,11 +22,8 @@ README.md: src/lib.rs

.PHONY: test
test:
TZ=UTC0 cargo test --features 'serde rustc-serialize bincode' --lib
TZ=ACST-9:30 cargo test --features 'serde rustc-serialize bincode' --lib
TZ=EST4 cargo test --features 'serde rustc-serialize bincode'
CHANNEL=$(CHANNEL) ./ci/travis.sh

.PHONY: doc
doc: authors readme
cargo doc --features 'serde rustc-serialize bincode'

14 changes: 14 additions & 0 deletions ci/core-test/Cargo.toml
@@ -0,0 +1,14 @@
[package]
name = "core-test"
version = "0.1.0"
authors = [
"Kang Seonghoon <public+rust@mearie.org>",
"Brandon W Maister <quodlibetor@gmail.com>",
]
edition = "2018"

[dependencies]
chrono = { path = "../..", default-features = false, features = ["serde"] }

[features]
alloc = ["chrono/alloc"]
7 changes: 7 additions & 0 deletions ci/core-test/src/lib.rs
@@ -0,0 +1,7 @@
#![no_std]

use chrono::{TimeZone, Utc};

pub fn create_time() {
let _ = Utc.ymd(2019, 1, 1).and_hms(0, 0, 0);
}
157 changes: 122 additions & 35 deletions ci/travis.sh
Expand Up @@ -2,25 +2,50 @@

# This is the script that's executed by travis, you can run it yourself to run
# the exact same suite
#
# When running it locally the most important thing to set is the CHANNEL env
# var, otherwise it will run tests against every version of rust that it knows
# about (nightly, beta, stable, 1.13.0):
#
# $ CHANNEL=stable ./ci/travis.sh

set -e

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

channel() {
if [ -n "${TRAVIS}" ]; then
if [ "${TRAVIS_RUST_VERSION}" = "${CHANNEL}" ]; then
pwd
(set -x; cargo "$@")
fi
elif [ -n "${APPVEYOR}" ]; then
if [ "${APPVEYOR_RUST_CHANNEL}" = "${CHANNEL}" ]; then
pwd
(set -x; cargo "$@")

main() {
if [[ -n "$CHANNEL" ]] ; then
if [[ "$CHANNEL" == 1.13.0 ]]; then
banner "Building $CHANNEL"
build_only
else
banner "Building/testing $CHANNEL"
build_and_test
banner "Testing Core $CHANNEL"
build_core_test
fi
else
pwd
(set -x; cargo "+${CHANNEL}" "$@")
CHANNEL=nightly
matching_banner "Test $CHANNEL"
if [[ "${CLIPPY}" = y ]] ; then
run_clippy
else
build_and_test
fi

CHANNEL=beta
matching_banner "Test $CHANNEL"
build_and_test

CHANNEL=stable
matching_banner "Test $CHANNEL"
build_and_test
build_core_test

CHANNEL=1.13.0
matching_banner "Test $CHANNEL"
build_only
fi
}

Expand Down Expand Up @@ -54,14 +79,20 @@ build_and_test_nonwasm() {
TZ=Asia/Katmandu channel test -v --features serde,rustc-serialize

# without default "clock" feature
channel build -v --no-default-features
channel build -v --no-default-features --features std
TZ=ACST-9:30 channel test -v --no-default-features --lib
channel build -v --no-default-features --features rustc-serialize
channel build -v --no-default-features --features std,rustc-serialize
TZ=EST4 channel test -v --no-default-features --features rustc-serialize --lib
channel build -v --no-default-features --features serde
channel build -v --no-default-features --features std,serde
TZ=UTC0 channel test -v --no-default-features --features serde --lib
channel build -v --no-default-features --features serde,rustc-serialize
TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib
channel build -v --no-default-features --features std,serde,rustc-serialize
TZ=Asia/Katmandu channel test -v --no-default-features --features std,serde,rustc-serialize --lib

channel build -v --no-default-features --features 'serde'
TZ=UTC0 channel test -v --no-default-features --features 'serde' --lib

channel build -v --no-default-features --features 'alloc serde'
TZ=UTC0 channel test -v --no-default-features --features 'alloc serde' --lib
}

build_and_test_wasm() {
Expand All @@ -81,8 +112,16 @@ build_only() {
cargo clean
channel build -v
channel build -v --features rustc-serialize
channel build -v --features 'serde bincode'
channel build -v --no-default-features
channel build -v --features serde
channel build -v --no-default-features --features std
}

build_core_test() {
channel_run rustup target add thumbv6m-none-eabi --toolchain "$CHANNEL"
(
cd ci/core-test
channel build -v --target thumbv6m-none-eabi
)
}

run_clippy() {
Expand All @@ -92,30 +131,78 @@ run_clippy() {
exit
fi

cargo clippy --features 'serde bincode rustc-serialize' -- -Dclippy
cargo clippy --features 'serde rustc-serialize' -- -Dclippy
}

check_readme() {
make readme
(set -x; git diff --exit-code -- README.md) ; echo $?
}

rustc --version
cargo --version
node --version
# script helpers

banner() {
echo "======================================================================"
echo "$*"
echo "======================================================================"
}

CHANNEL=nightly
if [ "x${CLIPPY}" = xy ] ; then
run_clippy
else
build_and_test
fi
underline() {
echo "$*"
echo "${*//?/^}"
}

CHANNEL=beta
build_and_test
matching_banner() {
if channel_matches || ! is_ci ; then
banner "$*"
echo_versions
fi
}

CHANNEL=stable
build_and_test
echo_versions() {
channel_run rustc --version
channel_run cargo --version
node --version
}

channel() {
channel_run cargo "$@"
}

channel_run() {
if channel_matches ; then
local the_cmd="$ $*"
underline "$the_cmd"
"$@"
elif ! is_ci ; then
local cmd="$1"
shift
if [[ $cmd == cargo || $cmd == rustc ]] ; then
underline "$ $cmd +${CHANNEL} $*"
"$cmd" "+${CHANNEL}" "$@"
else
underline "$ $cmd $*"
"$cmd" "$@"
fi
fi
}

channel_matches() {
if is_ci ; then
if [[ "${TRAVIS_RUST_VERSION}" = "${CHANNEL}"
|| "${APPVEYOR_RUST_CHANNEL}" = "${CHANNEL}" ]] ; then
return 0
fi
fi
return 1
}

is_ci() {
if [[ -n "$TRAVIS" || -n "$APPVEYOR" ]] ; then
return 0
else
return 1
fi
}

CHANNEL=1.13.0
build_only
main
11 changes: 7 additions & 4 deletions src/date.rs
Expand Up @@ -3,16 +3,17 @@

//! ISO 8601 calendar date with time zone.

use std::{fmt, hash};
use std::cmp::Ordering;
use std::ops::{Add, Sub};
use core::{fmt, hash};
use core::cmp::Ordering;
use core::ops::{Add, Sub};
use oldtime::Duration as OldDuration;

use {Weekday, Datelike};
use offset::{TimeZone, Utc};
use naive::{self, NaiveDate, NaiveTime, IsoWeek};
use DateTime;
use format::{Item, DelayedFormat, StrftimeItems};
#[cfg(any(feature = "alloc", feature = "std", test))]
use format::{DelayedFormat, Item, StrftimeItems};

/// ISO 8601 calendar date with time zone.
///
Expand Down Expand Up @@ -255,6 +256,7 @@ fn map_local<Tz: TimeZone, F>(d: &Date<Tz>, mut f: F) -> Option<Date<Tz>>

impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
/// Formats the date with the specified formatting items.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[inline]
pub fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I>
where I: Iterator<Item=Item<'a>> + Clone {
Expand All @@ -264,6 +266,7 @@ impl<Tz: TimeZone> Date<Tz> where Tz::Offset: fmt::Display {
/// Formats the date with the specified format string.
/// See the [`format::strftime` module](./format/strftime/index.html)
/// on the supported escape sequences.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[inline]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
self.format_with_items(StrftimeItems::new(fmt))
Expand Down