目标是将时间戳与一系列时间进行比较。
对于postgresql数据库,rails模式中的时间范围定义为t.time
。但是,在查询控制台时返回的数据会将日期归因于记录的字段...
start_time: "2000-01-01 08:00:00"
end_time: "2000-01-01 17:59:59"
现在,如果我想验证记录created_at: "2017-03-18 03:44:04"
是否在时间范围内,我还要比较日期,即将查询抛入空数组域。
在这种情况下,可以使用哪种rails或ruby工具以数据库无关的方式使用?
答案 0 :(得分:1)
您可以强制每次在同一天解析对象(例如2000年1月1日):
require 'time'
def parse_time_not_date(string)
time = Time.parse(string)
Time.local(2000, 1, 1, time.hour, time.min, time.sec, time.usec)
end
start_time = parse_time_not_date("2000-01-01 08:00:00")
end_time = parse_time_not_date("2000-01-01 17:59:59")
my_time = parse_time_not_date("2017-03-18 03:44:04")
puts (start_time..end_time).cover?(my_time)
# false
puts (start_time..end_time).cover?(parse_time_not_date("2017-03-18 14:59"))
# true
答案 1 :(得分:1)
def within_time_range?(start_time, end_time, time_check)
t = time_check[11..-1]
t >= start_time[11..-1] && t <= end_time[11..-1]
end
start_time = "2000-01-01 08:00:00"
end_time = "2000-01-01 17:59:59"
within_time_range?(start_time, end_time, "2017-03-18 03:44:04")
#=> false
within_time_range?(start_time, end_time, "2017-03-18 09:05:01")
#=> true
within_time_range?(start_time, end_time, "2017-03-18 19:05:01")
#=> false
请注意
start_time[11..-1]
#=> "08:00:00"
我已使用String#<=
和String#>=
,这些String
和import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Header, Button, Card } from '../components';
import { TextInput } from '@shoutem/ui';
export default class GoalList extends Component {
constructor(props) {
super(props);
}
styles = {
nameGoalContainer: {
flexDirection: 'row'
},
center: {
flexDirection: 'column',
justifyContent: 'center'
},
rightLabels: {
fontWeight: 'bold',
fontSize: 17
}
}
render() {
return (
<View style={{ flex:1 }}>
<Header text="New Goal" color='#3f51b5' />
<Card>
<View style={this.styles.nameGoalContainer}>
<View style={ [this.styles.center, { marginRight: 15 }] }>
<Text style={this.styles.rightLabels}>Title</Text>
</View>
<TextInput
maxLength={30}
placeholder="Buy a new graphics card"
style={{ flex: 3, textAlign: 'center' }}
/>
</View>
</Card>
<Button
text="+"
onPress={ () => { console.log('Hello') } }
size='50'
fontSize='25'
color='#FFD600'
fontColor='white'
style= {{
bottom: 20,
right: 20
}}
/>
</View>
);
}
}
是从String#<=>获得的,并且import React, { Component } from 'react';
import { View, Platform } from 'react-native';
class Card extends Component {
constructor(props) {
super(props);
}
generateComponent() {
const { cardStyleAndroid } = this.styles;
const { style } = this.props;
switch (Platform.OS) {
case 'android':
return (
<View style={[ cardStyleAndroid, style ]}>
{ this.props.children }
</View>
);
case 'ios':
return 0;
}
}
render() {
return (
<View>
{ this.generateComponent() }
</View>
);
}
styles = {
cardStyleAndroid: {
elevation: 2,
padding: 10,
backgroundColor: 'white',
marginHorizontal: 10,
marginVertical: 7
}
}
}
Card.propTypes = {
platform: React.PropTypes.string.isRequired,
style: React.PropTypes.object.isRequired,
children: React.PropTypes.object.isRequired
};
export default Card;
类包含在import React, { Component } from 'react';
import { View, Platform } from 'react-native';
class Button extends Component {
constructor(props) {
super(props);
}
generateComponent() {
const { cardStyleAndroid } = this.styles;
const { style } = this.props;
switch (Platform.OS) {
case 'android':
return (
<View style={[ cardStyleAndroid, style ]}>
{ this.props.children }
</View>
);
case 'ios':
return 0;
}
}
render() {
return (
<View>
{ this.generateComponent() }
</View>
);
}
styles = {
cardStyleAndroid: {
elevation: 2,
padding: 10,
backgroundColor: 'white',
marginHorizontal: 10,
marginVertical: 7
}
}
}
Button.propTypes = {
platform: React.PropTypes.string.isRequired,
style: React.PropTypes.object.isRequired,
children: React.PropTypes.object.isRequired
};
export default Button;
类中。
答案 2 :(得分:0)
我打算参加,因为第一个答案的讨论是恰当的。卡里的答案适用于那种情况。这个问题在可能的解决方案方面是矛盾的:红宝石或铁轨。因此有些潜在的胶粘剂。以下是另一种方式。
对于rails,考虑时区存在问题。调用数据类型time
的对象实际上保持UTC。使用date_time
,你得到一个+或 - 小时的字符串(或者它的一小部分 - yay Newfoundland!)
因此,使用rails处理proper way to handle UTC data is to assign it the rails application time zone和in_time_zone
,链接到字符串,然后解压缩。因此,比较结果如下:
p = Time.parse(@interruptions[0].pause.to_s[11,8])
p >= Time.parse(d_s.start_time.in_time_zone.to_s[11,8]) && p <= Time.parse(d_s.end_time.in_time_zone.to_s[11,8])
注意:无法让[11..-1]
在此上下文中工作