pub struct Date { /* private fields */ }
Expand description
日付
カレンダーのベースとなる構造体。 文字列または(年, 月, 日)から生成する。
Implementations§
source§impl Date
impl Date
sourcepub fn parse(fmt: &str) -> KoyomiResult<Self>
pub fn parse(fmt: &str) -> KoyomiResult<Self>
文字列からオブジェクトを生成する
文字列は Y-m-d
か Y/m/d
形式のみ受け付ける
Examples
use koyomi::Date;
let date = Date::parse("2018-01-01");
assert!(date.is_ok());
let date = Date::parse("2018/01/01");
assert!(date.is_ok());
let date = Date::parse("2018-01-32");
assert!(date.is_err());
sourcepub fn from_ymd(year: i32, month: u32, day: u32) -> KoyomiResult<Self>
pub fn from_ymd(year: i32, month: u32, day: u32) -> KoyomiResult<Self>
年月日からオブジェクトを生成する
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1);
assert!(date.is_ok());
let date = Date::from_ymd(2018, 13, 1);
assert!(date.is_err());
sourcepub fn day(&self) -> u32
pub fn day(&self) -> u32
「日」を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 31).unwrap();
assert_eq!(date.day(), 31);
sourcepub fn era(&self) -> Option<Era>
pub fn era(&self) -> Option<Era>
「元号」を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
assert_eq!(date.era().unwrap().name(), "平成");
sourcepub fn holiday(&self) -> Option<String>
pub fn holiday(&self) -> Option<String>
「祝祭日」を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
assert_eq!(date.holiday().unwrap(), "元日");
let date = Date::from_ymd(2018, 1, 2).unwrap();
assert_eq!(date.holiday(), None);
sourcepub fn month(&self) -> u32
pub fn month(&self) -> u32
「月」を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 12, 1).unwrap();
assert_eq!(date.month(), 12);
sourcepub fn num_days(&self, date: &Date) -> i64
pub fn num_days(&self, date: &Date) -> i64
日付間の期間が何日あるかを返す
Examples
use koyomi::Date;
let from = Date::from_ymd(2018, 1, 1).unwrap();
let until = Date::from_ymd(2018, 12, 31).unwrap();
assert_eq!(until.num_days(&from), 364);
// うるう年
let from = Date::from_ymd(2016, 1, 1).unwrap();
let until = Date::from_ymd(2016, 12, 31).unwrap();
assert_eq!(until.num_days(&from), 365);
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
日付の文字列表現を返す
フォーマットは Y-m-d
形式となる
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
assert_eq!(date.to_string(), "2018-01-01");
sourcepub fn tomorrow(&self) -> KoyomiResult<Self>
pub fn tomorrow(&self) -> KoyomiResult<Self>
翌日の日付を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
let tomorrow = date.tomorrow();
assert_eq!(tomorrow.unwrap().to_string(), "2018-01-02");
sourcepub fn weekday(&self) -> &Weekday
pub fn weekday(&self) -> &Weekday
「曜日」を返す
Examples
use koyomi::{Date, Weekday};
let date = Date::from_ymd(2018, 1, 1).unwrap();
assert_eq!(date.weekday(), &Weekday::Monday);
sourcepub fn year(&self) -> i32
pub fn year(&self) -> i32
「年」を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
assert_eq!(date.year(), 2018);
sourcepub fn yesterday(&self) -> KoyomiResult<Self>
pub fn yesterday(&self) -> KoyomiResult<Self>
前日の日付を返す
Examples
use koyomi::Date;
let date = Date::from_ymd(2018, 1, 1).unwrap();
let yesterday = date.yesterday();
assert_eq!(yesterday.unwrap().to_string(), "2017-12-31");