Transforming date + time in a TimeStamp Object - ruby-on-rails

I'm having some trouble with making a timestamp from a date and a time
What i'm trying to do:
date = "2016 2 21"
time = "03:00 UTC"
output = "Thu, 21 Feb 2016 03:00:00 UTC +00:00"
I'm getting the date from a form_for:
f.date_field(:date_first)
But I'm not sure of how should I pick up the time.

To give you a headstart:
dt = "2016-2-21"
time = "03:00 UTC"
dtime = DateTime.parse(dt + 'T' + time)
output = dtime.rfc2822
and the result is:
#=> "Sun, 21 Feb 2016 03:00:00 +0000"

Maybe you could do something like this
strftime("%d.%m.%Y. %H:%M:%S")
and also get your locale yml file from here
https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale

Related

In Rails, how to convert time zone into an integer

I have time and time zone, what I need is I just want to convert like this Wed, 11 Dec 2019 19:00:00 +0530 and then I need to convert like this 1576071000.
So for I tried like this
time = "19"
hour = "00"
time_zone = "IST"
e = DateTime.now.change({hour: time, minute: hour})
I get the exact output, but I need to convert with timezone. that means something like this
DateTime.now.change({hour: time, minute: hour}).time_zone('IST')
credit to #engineersmnky
hour = 19
minute = 0
time_zone = 'Chennai'
e = Time.zone.now.change({hour: hour, minute: minute}).in_time_zone(time_zone).to_i
You need to convert it to UTC:
DateTime.now.change({hour: time, minute: hour}).time_zone('IST').utc
Or if you are in Rails 4 or above you can use in_time_zone:
DateTime.now.change({hour: time, minute: hour}).in_time_zone('IST').utc

Store date in neo4j

The given date format in CSV is '(Fri) 09 Jan 2018 (32)'. This should feed to database as a date column to allow order by date. How could convert above format to Neo4j date format at the insertion time ?
Solution
WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH parts[1] + ' ' + parts[2] + ' ' + parts[3] AS concatDate
RETURN apoc.date.parse(concatDate, 's',"dd MMM yyyy") as date;
Explanation:
line 1: defines a date for testing purpose
line 2: splits the given date into its pieces at each blank
line 3: concatenates the day, month and year
line 4: parse the built date of line 3 and convert it to a Neo4j date
Result
╒══════════╕
│"date" │
╞══════════╡
│1515456000│
└──────────┘
Alternative solution
WITH '(Fri) 09 Jan 2018 (32)' as inputString
WITH split(inputString, ' ') as parts
WITH reduce(s = "", x IN parts[1..4] | s + x) AS concatDate
RETURN apoc.date.parse(concatDate, 's',"ddMMMyyyy") as date;
Assuming the date substring always starts at offset 6 in the input string, this will return the date offset from 01 Jan 1970, which is adequate for comparing dates:
WITH '(Fri) 09 Jan 2018 (32)' as s
RETURN apoc.date.parse(SUBSTRING(s, 6, 11), 'd', "dd MMM yyyy") as date;

How to format a date range (with only one year string) in different locale's in iOS?

The date string in English: Jan 18 - Jan 26, 2018
Incorrect Korean date string: Jan 18 - 2018 Jan 26
What should happen in Korean: 2018 Jan 18 - Jan 26 (not exactly correct Korean, just referring to the location of the year. See accepted answer to see proper Korean date format)
Right now this requires to date formatters, but you have to hardcode which date formatter has the year, so the Korean date doesn't look right.
Is this possible to do in Swift/Objc without just putting the year string on both sides of the date range?
Use a DateIntervalFormatter:
let sd = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 18))!
let ed = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 26))!
let dif = DateIntervalFormatter()
dif.dateStyle = .medium
dif.timeStyle = .none
dif.locale = Locale(identifier: "en_US")
let resEN = dif.string(from: sd, to: ed)
dif.locale = Locale(identifier: "ko_KR")
let resKO = dif.string(from: sd, to: ed)
This results in:
Jan 18 – 26, 2018
2018. 1. 18. ~ 2018. 1. 26.
The output isn't exactly what you show in your question but the output is appropriate for the given locales.

Swift - Timezone off by one hour / secondsFromGMT incorrect

This should be a really simple question but I just can't seem to wrap my head around it.
Given my timezone is EDT (GMT-4), why does 04:00 in GMT turn into 23:00 and not 00:00?
// The offset is -4 hours
let offsetFromGMT = Calendar.current.timeZone.secondsFromGMT() / 60 / 60
// 2017-03-12 04:00
var destinationComponents = DateComponents()
destinationComponents.timeZone = TimeZone(secondsFromGMT: 0)
destinationComponents.year = 2017
destinationComponents.month = 03
destinationComponents.day = 12
destinationComponents.hour = -offsetFromGMT // 4 hours
// Why is this 2017-03-11 23:00 and not 2017-03-12 00:00?
let date = Calendar.current.date(from: destinationComponents)!
// Outputs 23
Calendar.current.dateComponents([.hour], from: date).hour
Calendar.current.timeZone.secondsFromGMT()
is the current GMT offset for your time zone. In your case that
is 4 hours, because the current time zone in New York is EDT = GMT-4,
with daylight saving time active.
So your destinationComponents and date are four o'clock in
the morning Greenwich time:
2017-03-12 04:00:00 +0000
At that point, the time zone in New York was EST = GMT-5, and
daylight saving time not active. Therefore that date is 2017-03-11 23:00 in your local time zone.
I would proceed differently, avoiding "secondsFromGMT".
Example: "2017-03-12 00:00:00" New York time is "2017-03-12 05:00:00" GMT.
var srcComponents = DateComponents()
srcComponents.timeZone = TimeZone(identifier: "America/New_York")!
srcComponents.year = 2017
srcComponents.month = 3
srcComponents.day = 12
srcComponents.hour = 0
srcComponents.minute = 0
let date = Calendar.current.date(from: srcComponents)!
print(date) // 2017-03-12 05:00:00 +0000

How to covert the date string range into datatime data type

I need to convert the following raw string (date range) into ruby datetime datetype.
How to finish it on Rails ?
raw string
"2014 April/July 24-1"
convert to ruby datetime variable
start_date = 2014-04-24
end_date = 2014-07-01
raw string
"2015 April 06-20"
convert to ruby datetime variable
start_date = 2015-04-06
end_date = 2015-04-20
This may help
# In order to generate
# year = 2014
# months = "April/July"
# days = "24-1"
/(?<year>\d{4})\s*(?<months>\w+\/\w+)\s*(?<days>\d{1,2}\-\d{1,2})/ =~ "2014 April/July 24-1"
date1 = "#{year} #{months.split('/')[0]} #{days.split('-')[0]}"
date2 = "#{year} #{months.split('/')[1]} #{days.split('-')[1]}"
start_date = Date.strptime(date1, "%Y %b %d") #=> Thu, 24 Apr 2014
end_date = Date.strptime(date2, "%Y %b %d") #=> Tue, 01 Jul 2014

Resources