I have song based objects with their duration attribute saved in milliseconds. In short I was looking for a way to take a string like this: 3:25 (3 minutes/25 seconds) and convert it to milliseconds. The problem I'm facing is that taking such a value and using it with Time wouldn't work to my knowledge since it would consider it a time of day vs minutes delimited by a semicolon. Was wondering the best way to parse and convert a value like this to be presented. Was also wondering of a way to convert back.
duration string to total milliseconds:
def custom_convertor(duration_str) # format of duration_str -> "12:34"
mins, secs = duration_str.split(":").map(&:to_i)
1000*(60*mins+secs)
end
Related
I found strange date codes in a SQL database used by an old iOS app called Memo Lite. The column was labeled LMT, that might be an abbreviation for last modified time. How are they constructed and how can they be converted to readable formats, like yyyy-mm-dd HH:mm:ss?
I am aware of both Unix time and how Google Sheets handles time.
Example of date/time codes:
357931095.942149 = 2012-05-05
330432567.859129 = 2011-06-22
293964817.803674 = 2010-04-26
I don't know if the format has time, it looks so but it is not showed in the app.
Date/time format codes, reference example:
https://support.google.com/docs/answer/3094139?hl=en
A little math shows it's seconds since an epoch. Not the usual 1970-01-01, but 2001-01-01.
2001-01-01 + 293964817 seconds = 2010-04-25 08:53:37
2001-01-01 + 330432567 seconds = 2011-06-22 10:49:27
2001-01-01 + 357931095 seconds = 2012-05-05 17:18:15
The decimal portion is microseconds. For example, 293964817.803674 is 2010-04-25 08:53:37.803674.
You mentioned it's an iOS app, so it's likely CFAbsoluteTime.
my table has 3 columns: data type timestamp,
|created_At | final_time| duracion(difference between created at and final_time)
| | |
the column difference should save the difference in hours and minutes, in this format HH:MM
this is my controller:
def horario
horario.update(duracion: params[:duracion]) // this params is "00:59"
end
but in the table Horarios, in column duracion i have this:
2017-12-24 03:59:00
so i want to save 00:59 (59 minutes) but postgres save all current date and add 3 hours more.
i want to save so in the future i will be able tu sum column duracion. Or should i change data type for this column? In this case which datatype you recomend me for rails to save HH:MM??
thanks.
Rails 5 supports PostgreSQL's interval type to some extent. You can create interval columns in the usual way and they will be properly represented in db/schema.rb. You can also assign them values in the usual way so you can say things like:
model.some_interval = '6 hours'
and get 06:00:00 inside the database. However, there is nothing in Ruby or Rails that properly represents a time interval (we only have various timestamp and date classes) so when that interval comes out of the database, you'll have a string on your hands, i.e:
> model = Model.find(some_id)
> model.some_interval.class
=> String
so you might end up having to manually parse some strings in Ruby. For simple intervals like '6 hours', this will be easy but it won't be so easy with more complicated intervals like '6 years 23 days 11 hours'.
If you'll only be working with your time intervals inside the database then interval would be natural and easy, you can say things like:
select some_timestamp + some_interval
and
where some_timestamp + some_interval < some_other_timestamp
and everything will work nicely.
However, if you need to work with the intervals back in Ruby then you'd probably be better off storing the interval as a number of seconds in an integer column (or whatever resolution you need). Then you could say things like:
where some_timestamp + (some_interval_in_seconds || 'seconds')::interval < some_other_timestamp
inside the database and
some_time + model.some_interval_in_seconds
back in Ruby.
In any case, strings are probably the wrong approach unless you really like parsing strings everywhere all the time.
As others already pointed out, Rails handles the Postgres Interval type as a string. A string that, unfortunately, is not easy to parse.
If you do this:
u = Users.select('edited_at - created_at as time_dif')
puts u.first['time_dif']
You can get something like 168 days 12:51:20.851115. Ugly right?
Well, using Ruby to convert this string into an useful number is not easy, but you can use Postgres to do the job for you. You will need to do a plain SQL query though, but it's the best method I've found so far:
query = ActiveRecord::Base.connection.execute("
SELECT EXTRACT(epoch FROM time_dif)/3600 as hours_dif
FROM
(
SELECT (edited_at - created_at) as time_dif
FROM users
) AS MainQuery
")
In this example, Postgres' EXTRACT function will convert the Interval type into a number which represents the total seconds of the interval. If you divide this number by 3600 you will get the different in hours as in the example above.
Then, if you want to iterate over the results:
query.each do |r|
puts r['hours_dif']
end
You could save duracion as a float type, where duracion would equal something like final_time - created_at and this value would be the difference in seconds. You can then perform arithmetic with these values and always convert back to minutes, hours, or whatever you need.
I need to convert Swift Date object to date ticks string like "/Date(631148400000+0100)/".
The following link tells me how to convert date ticks string to Date object but not the vice-versa:
How to convert date like \/Date(1440156888750-0700)\/ to something that Swift can handle?
How can I do that?
Thanks in advance.
From Stand-Alone JSON Serialization:
DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970. The number may be negative to represent earlier times. The part that consists of "+0500" in the example is optional and indicates that the time is of the Local kind - that is, should be converted to the local time zone on deserialization. If it is absent, the time is deserialized as Utc. The actual number ("0500" in this example) and its sign (+ or -) are ignored.
And from Use JSON.NET to parse json date of format Date(epochTime-offset)
... In this screwy format, the timestamp portion is still based solely on UTC. The offset is extra information. It doesn't change the timestamp. You can give a different offset, or omit it entirely and it's still the same moment in time.
So the number of ticks is the number of milliseconds since Januar 1, 1970 GMT. Adding a time zone specification would only change how the
date is presented locally in .NET, and one can simply omit that part
when generating a JSON date string:
extension Date {
var jsonDate: String {
let ticks = lround(timeIntervalSince1970 * 1000)
return "/Date(\(ticks))/"
}
}
Example:
print(Date().jsonDate) // /Date(1481446227993)/
I am getting lost and nuts with the DATETIME in Informix.I have two problems which I can hardly solve:
I have a DATETIME column (e.g. starttime) which I need to convert into an int value, e.g. seconds of year or epoch, or whatever. I found some conversion into utc, but this depends on the timezone the server runs, which I have no idea how to specify for the conversion.... Any other hint, how to convert it into seconds would be appreciated.
I need to calculate the difference between two DATETIME-fields (e.g. endtime-starttime) and then sum it up. To my understanding the result is interval day(13) to fraction(3). I need to convert the sum once again into seconds, cause I need to update other values with this result.
So, can anybody help me as to how to convert within a SQL-statement the different result-types?
CAST it to INT, example:
select
((current + 5 units day - current)::interval second(9) to second)
,((current + 5 units day - current)::interval second(9) to second)::char(10)::int8
from systables
where tabid=1
I have an odd issue with an NSDateFormatter, I am passing the following string as a date format "dd/MM/yy"
If I enter 50 for the year I get a conversion to 1950 however anything below that for instance 49 results in 2049. Any ideas how I can remedy this?
Many thanks.
It sounds like you'll need to force a four digit response (or programmatically prepend two digits of "19") to wherever you're drawing your string from. Lots of people are using dates in the near to mid-term future like "12/21/12" (end of the Mayan Calendar era) so it's natural that a 2 digit year assumes 2000+ for digits 1-50 and 1999- for digits (50-99).
I'm also seeing a number of Google hits on the keyword terms "NSDateFormatter" & "century", b.t.w.