Skip to content

Commit

Permalink
fix: rename time_zone to timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor-lagrange committed Dec 25, 2023
1 parent 5b80f9a commit 22d7413
Show file tree
Hide file tree
Showing 27 changed files with 198 additions and 194 deletions.
4 changes: 2 additions & 2 deletions config/frontend.example.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Node running mode, see `standalone.example.toml`.
mode = "distributed"
# The default time zone of the server
# default_time_zone = "UTC"
# The default timezone of the server
# default_timezone = "UTC"

[heartbeat]
# Interval for sending heartbeat task to the Metasrv, 5 seconds by default.
Expand Down
4 changes: 2 additions & 2 deletions config/standalone.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
mode = "standalone"
# Whether to enable greptimedb telemetry, true by default.
enable_telemetry = true
# The default time zone of the server
# default_time_zone = "UTC"
# The default timezone of the server
# default_timezone = "UTC"

# HTTP server options.
[http]
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub enum Error {
source: common_meta::error::Error,
},

#[snafu(display("Failed to init default time zone"))]
InitTimeZone {
#[snafu(display("Failed to init default timezone"))]
InitTimezone {
location: Location,
source: common_time::error::Error,
},
Expand Down Expand Up @@ -274,7 +274,7 @@ impl ErrorExt for Error {
| Error::LoadLayeredConfig { .. }
| Error::IllegalConfig { .. }
| Error::InvalidReplCommand { .. }
| Error::InitTimeZone { .. }
| Error::InitTimezone { .. }
| Error::ConnectEtcd { .. }
| Error::NotDataFromOutput { .. }
| Error::CreateDir { .. }
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use client::client_manager::DatanodeClients;
use common_meta::heartbeat::handler::parse_mailbox_message::ParseMailboxMessageHandler;
use common_meta::heartbeat::handler::HandlerGroupExecutor;
use common_telemetry::logging;
use common_time::timezone::set_default_time_zone;
use common_time::timezone::set_default_timezone;
use frontend::frontend::FrontendOptions;
use frontend::heartbeat::handler::invalidate_table_cache::InvalidateTableCacheHandler;
use frontend::heartbeat::HeartbeatTask;
Expand All @@ -33,7 +33,7 @@ use servers::tls::{TlsMode, TlsOption};
use servers::Mode;
use snafu::{OptionExt, ResultExt};

use crate::error::{self, InitTimeZoneSnafu, MissingConfigSnafu, Result, StartFrontendSnafu};
use crate::error::{self, InitTimezoneSnafu, MissingConfigSnafu, Result, StartFrontendSnafu};
use crate::options::{CliOptions, Options};
use crate::App;

Expand Down Expand Up @@ -218,7 +218,7 @@ impl StartCommand {
logging::info!("Frontend start command: {:#?}", self);
logging::info!("Frontend options: {:#?}", opts);

set_default_time_zone(opts.default_time_zone.as_deref()).context(InitTimeZoneSnafu)?;
set_default_timezone(opts.default_timezone.as_deref()).context(InitTimezoneSnafu)?;

let meta_client_options = opts.meta_client.as_ref().context(MissingConfigSnafu {
msg: "'meta_client'",
Expand Down
14 changes: 7 additions & 7 deletions src/cmd/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use common_meta::wal::{WalOptionsAllocator, WalOptionsAllocatorRef};
use common_procedure::ProcedureManagerRef;
use common_telemetry::info;
use common_telemetry::logging::LoggingOptions;
use common_time::timezone::set_default_time_zone;
use common_time::timezone::set_default_timezone;
use datanode::config::{DatanodeOptions, ProcedureConfig, RegionEngineConfig, StorageConfig};
use datanode::datanode::{Datanode, DatanodeBuilder};
use file_engine::config::EngineConfig as FileEngineConfig;
Expand All @@ -51,7 +51,7 @@ use servers::Mode;
use snafu::ResultExt;

use crate::error::{
CreateDirSnafu, IllegalConfigSnafu, InitDdlManagerSnafu, InitMetadataSnafu, InitTimeZoneSnafu,
CreateDirSnafu, IllegalConfigSnafu, InitDdlManagerSnafu, InitMetadataSnafu, InitTimezoneSnafu,
Result, ShutdownDatanodeSnafu, ShutdownFrontendSnafu, StartDatanodeSnafu, StartFrontendSnafu,
StartProcedureManagerSnafu, StartWalOptionsAllocatorSnafu, StopProcedureManagerSnafu,
};
Expand Down Expand Up @@ -98,7 +98,7 @@ impl SubCommand {
pub struct StandaloneOptions {
pub mode: Mode,
pub enable_telemetry: bool,
pub default_time_zone: Option<String>,
pub default_timezone: Option<String>,
pub http: HttpOptions,
pub grpc: GrpcOptions,
pub mysql: MysqlOptions,
Expand All @@ -122,7 +122,7 @@ impl Default for StandaloneOptions {
Self {
mode: Mode::Standalone,
enable_telemetry: true,
default_time_zone: None,
default_timezone: None,
http: HttpOptions::default(),
grpc: GrpcOptions::default(),
mysql: MysqlOptions::default(),
Expand All @@ -149,7 +149,7 @@ impl StandaloneOptions {
fn frontend_options(self) -> FrontendOptions {
FrontendOptions {
mode: self.mode,
default_time_zone: self.default_time_zone,
default_timezone: self.default_timezone,
http: self.http,
grpc: self.grpc,
mysql: self.mysql,
Expand Down Expand Up @@ -370,8 +370,8 @@ impl StartCommand {

info!("Building standalone instance with {opts:#?}");

set_default_time_zone(opts.frontend.default_time_zone.as_deref())
.context(InitTimeZoneSnafu)?;
set_default_timezone(opts.frontend.default_timezone.as_deref())
.context(InitTimezoneSnafu)?;

// Ensure the data_home directory exists.
fs::create_dir_all(path::Path::new(&opts.data_home)).context(CreateDirSnafu {
Expand Down
16 changes: 8 additions & 8 deletions src/common/time/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use chrono::{Days, LocalResult, Months, NaiveDateTime, TimeZone as ChronoTimeZon
use serde::{Deserialize, Serialize};

use crate::error::{Error, InvalidDateStrSnafu, Result};
use crate::timezone::TimeZone;
use crate::timezone::Timezone;
use crate::util::{format_utc_datetime, local_datetime_to_utc};
use crate::{Date, Interval};

Expand Down Expand Up @@ -110,11 +110,11 @@ impl DateTime {
NaiveDateTime::from_timestamp_millis(self.0)
}

pub fn to_chrono_datetime_with_timezone(&self, tz: Option<TimeZone>) -> Option<NaiveDateTime> {
pub fn to_chrono_datetime_with_timezone(&self, tz: Option<Timezone>) -> Option<NaiveDateTime> {
let datetime = self.to_chrono_datetime();
datetime.map(|v| match tz {
Some(TimeZone::Offset(offset)) => offset.from_utc_datetime(&v).naive_local(),
Some(TimeZone::Named(tz)) => tz.from_utc_datetime(&v).naive_local(),
Some(Timezone::Offset(offset)) => offset.from_utc_datetime(&v).naive_local(),
Some(Timezone::Named(tz)) => tz.from_utc_datetime(&v).naive_local(),
None => Utc.from_utc_datetime(&v).naive_local(),
})
}
Expand Down Expand Up @@ -155,19 +155,19 @@ impl DateTime {
#[cfg(test)]
mod tests {
use super::*;
use crate::timezone::set_default_time_zone;
use crate::timezone::set_default_timezone;

#[test]
pub fn test_new_date_time() {
set_default_time_zone(Some("Asia/Shanghai")).unwrap();
set_default_timezone(Some("Asia/Shanghai")).unwrap();
assert_eq!("1970-01-01 08:00:00+0800", DateTime::new(0).to_string());
assert_eq!("1970-01-01 08:00:01+0800", DateTime::new(1000).to_string());
assert_eq!("1970-01-01 07:59:59+0800", DateTime::new(-1000).to_string());
}

#[test]
pub fn test_parse_from_string() {
set_default_time_zone(Some("Asia/Shanghai")).unwrap();
set_default_timezone(Some("Asia/Shanghai")).unwrap();
let time = "1970-01-01 00:00:00+0800";
let dt = DateTime::from_str(time).unwrap();
assert_eq!(time, &dt.to_string());
Expand Down Expand Up @@ -195,7 +195,7 @@ mod tests {

#[test]
fn test_parse_local_date_time() {
set_default_time_zone(Some("Asia/Shanghai")).unwrap();
set_default_timezone(Some("Asia/Shanghai")).unwrap();
assert_eq!(
-28800000,
DateTime::from_str("1970-01-01 00:00:00").unwrap().val()
Expand Down
16 changes: 8 additions & 8 deletions src/common/time/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub enum Error {
#[snafu(display("Timestamp arithmetic overflow, msg: {}", msg))]
ArithmeticOverflow { msg: String, location: Location },

#[snafu(display("Invalid time zone offset: {hours}:{minutes}"))]
InvalidTimeZoneOffset {
#[snafu(display("Invalid timezone offset: {hours}:{minutes}"))]
InvalidTimezoneOffset {
hours: i32,
minutes: u32,
location: Location,
Expand All @@ -66,18 +66,18 @@ pub enum Error {
location: Location,
},

#[snafu(display("Invalid time zone string {raw}"))]
ParseTimeZoneName { raw: String, location: Location },
#[snafu(display("Invalid timezone string {raw}"))]
ParseTimezoneName { raw: String, location: Location },
}

impl ErrorExt for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::ParseDateStr { .. }
| Error::ParseTimestamp { .. }
| Error::InvalidTimeZoneOffset { .. }
| Error::InvalidTimezoneOffset { .. }
| Error::ParseOffsetStr { .. }
| Error::ParseTimeZoneName { .. } => StatusCode::InvalidArguments,
| Error::ParseTimezoneName { .. } => StatusCode::InvalidArguments,
Error::TimestampOverflow { .. } => StatusCode::Internal,
Error::InvalidDateStr { .. } | Error::ArithmeticOverflow { .. } => {
StatusCode::InvalidArguments
Expand All @@ -96,9 +96,9 @@ impl ErrorExt for Error {
| Error::TimestampOverflow { location, .. }
| Error::ArithmeticOverflow { location, .. } => Some(*location),
Error::ParseDateStr { .. }
| Error::InvalidTimeZoneOffset { .. }
| Error::InvalidTimezoneOffset { .. }
| Error::ParseOffsetStr { .. }
| Error::ParseTimeZoneName { .. } => None,
| Error::ParseTimezoneName { .. } => None,
Error::InvalidDateStr { location, .. } => Some(*location),
Error::ParseInterval { location, .. } => Some(*location),
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ pub use interval::Interval;
pub use range::RangeMillis;
pub use timestamp::Timestamp;
pub use timestamp_millis::TimestampMillis;
pub use timezone::TimeZone;
pub use timezone::Timezone;
38 changes: 19 additions & 19 deletions src/common/time/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use chrono::{NaiveDateTime, NaiveTime, TimeZone as ChronoTimeZone, Utc};
use serde::{Deserialize, Serialize};

use crate::timestamp::TimeUnit;
use crate::timezone::{get_time_zone, TimeZone};
use crate::timezone::{get_timezone, Timezone};

/// Time value, represents the elapsed time since midnight in the unit of `TimeUnit`.
#[derive(Debug, Clone, Default, Copy, Serialize, Deserialize)]
Expand Down Expand Up @@ -108,26 +108,26 @@ impl Time {
self.as_formatted_string("%H:%M:%S%.f%z", None)
}

/// Format Time for system time zone.
/// Format Time for system timeszone.
pub fn to_system_tz_string(&self) -> String {
self.as_formatted_string("%H:%M:%S%.f", None)
}

/// Format Time for given timezone.
/// When timezone is None, using system time zone by default.
pub fn to_timezone_aware_string(&self, tz: Option<TimeZone>) -> String {
/// When timezone is None, using system timezone by default.
pub fn to_timezone_aware_string(&self, tz: Option<Timezone>) -> String {
self.as_formatted_string("%H:%M:%S%.f", tz)
}

fn as_formatted_string(self, pattern: &str, timezone: Option<TimeZone>) -> String {
fn as_formatted_string(self, pattern: &str, timezone: Option<Timezone>) -> String {
if let Some(time) = self.to_chrono_time() {
let date = Utc::now().date_naive();
let datetime = NaiveDateTime::new(date, time);
match get_time_zone(timezone) {
TimeZone::Offset(offset) => {
match get_timezone(timezone) {
Timezone::Offset(offset) => {
format!("{}", offset.from_utc_datetime(&datetime).format(pattern))
}
TimeZone::Named(tz) => {
Timezone::Named(tz) => {
format!("{}", tz.from_utc_datetime(&datetime).format(pattern))
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ mod tests {
use serde_json::Value;

use super::*;
use crate::timezone::set_default_time_zone;
use crate::timezone::set_default_timezone;

#[test]
fn test_time() {
Expand Down Expand Up @@ -310,7 +310,7 @@ mod tests {

#[test]
fn test_to_iso8601_string() {
set_default_time_zone(Some("+10:00")).unwrap();
set_default_timezone(Some("+10:00")).unwrap();
let time_millis = 1000001;
let ts = Time::new_millisecond(time_millis);
assert_eq!("10:16:40.001+1000", ts.to_iso8601_string());
Expand All @@ -334,7 +334,7 @@ mod tests {

#[test]
fn test_serialize_to_json_value() {
set_default_time_zone(Some("+10:00")).unwrap();
set_default_timezone(Some("+10:00")).unwrap();
assert_eq!(
"10:00:01+1000",
match serde_json::Value::from(Time::new(1, TimeUnit::Second)) {
Expand Down Expand Up @@ -370,7 +370,7 @@ mod tests {

#[test]
fn test_to_timezone_aware_string() {
set_default_time_zone(Some("+10:00")).unwrap();
set_default_timezone(Some("+10:00")).unwrap();

assert_eq!(
"10:00:00.001",
Expand All @@ -380,37 +380,37 @@ mod tests {
assert_eq!(
"08:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("SYSTEM").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("SYSTEM").unwrap()))
);
assert_eq!(
"08:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("+08:00").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("+08:00").unwrap()))
);
assert_eq!(
"07:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("+07:00").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("+07:00").unwrap()))
);
assert_eq!(
"23:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("-01:00").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("-01:00").unwrap()))
);
assert_eq!(
"08:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("Asia/Shanghai").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("Asia/Shanghai").unwrap()))
);
assert_eq!(
"00:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("UTC").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("UTC").unwrap()))
);
assert_eq!(
"03:00:00.001",
Time::new(1, TimeUnit::Millisecond)
.to_timezone_aware_string(Some(TimeZone::from_tz_string("Europe/Moscow").unwrap()))
.to_timezone_aware_string(Some(Timezone::from_tz_string("Europe/Moscow").unwrap()))
);
}
}
Loading

0 comments on commit 22d7413

Please sign in to comment.