How can I use the Chronic natural language date/time parser to parse "12:00" as 12:00 PM? - ruby

I am using the ruby gem "Chronic" to parse four digit strings as DateTime objects. I am using time in military format (ie: "0800") which seems from the documentaion to be a valid format.
In most cases, Chronic parses time in this format correctly - however it always parses a four digit string beginning with "12" as 00:XX AM of the next day, never as 12:XX PM of the current day.
For example:
>> Chronic.parse("1234")
=> Thu Sep 17 00:34:00 -0600 2009
I see that if I put a colon between the hours and minutes I get the desired output:
>> Chronic.parse("12:34")
=> Wed Sep 16 12:34:00 -0600 2009
I am however wanting to pass the value without a colon, like this:
>> Chronic.parse("1234")
=> Wed Sep 16 12:34:00 -0600 2009
What string do I have to pass to the parser in order for Chronic to interpret "1234" as 12:34 PM of the current day?

I'm not certain, but it looks like it might be a bug. My guess is you're ending up in this corner of the code:
http://github.com/mojombo/chronic/commit/c7d9591acf5179345cbc916bd509c48acee8e744

Related

How to perform date operations in bash

Basically, I want to take a time, and day of the week, in UTC+8, and adjust the datetime object to a given UTC offset, within bash, I don't have any code to show because I'm not sure how to start attempting this in the first place honestly
(I'm writing a custom script for a friend who lives in UTC+8 and want to make the input as easy as possible for them, basically they just give it a time in their timezone, and a day of the week, and it'll tell them what date and time that'll be in a different timezone, for an overarching purpose)
For a reference, look at the section 1 of the manual page for "date":
In your shell, just type: man 1 date
or see the online man page:
https://man7.org/linux/man-pages/man1/date.1.html
One way is to parse the date into the number of seconds since epoch (since 1970), and then convert that number of seconds into the format you want:
For example:
$ date +%s --date='2022-12-27 11:30:17 +008'
1672140137
$ date +%c --date='#1672140137'
Tue 27 Dec 2022 06:22:17 AM EST
or you could also convert to ISO format then back to local time
$ date -Iseconds --date='TZ="GMT" 2022-12-22 11:33:44 +08'
2022-12-21T22:33:44-05:00
$ date --date='2022-12-21T22:33:44-05:00'
Wed 21 Dec 2022 10:33:44 PM EST
I hope this helps you get started with some ideas for converting to/from different timezones.
Also, to help with user input, you can show the current month calendar using cal
$ cal
December 2022
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
$ date --date='Fri 08:30'
Fri 30 Dec 2022 08:30:00 AM EST
In the above example, I specified "Fri 08:30" which gets set to the next Friday at 08:30 in the morning for my local timezone.

Ruby unix date incorrect

I have the following Unix timestamp: 1478698378000
And I'm trying to show this as a datetime in Ruby, e.g.
<%= Time.at(#timestamp).to_datetime %>
Which should be returning a date of: Wed, 09 Nov 2016 13:32:58 GMT but the above code actually returns a date of: 48828-02-01T13:26:40+00:00 Ignore formatting!
As you can see it thinks that timestamp is 2nd Feb 48828 13:26:40.
Why is the datetime coming out completely incorrect and the year so far into the future like that? Checking the timestamp on http://www.epochconverter.com/ reveals the timestamp to be correct, so it's Ruby that's returning it incorrectly.
Time.at expects seconds as an argument and your timestamp is an amount of milliseconds. See documentation on Time.at
Why won’t you check the unix timestamp correctness against “Fashion Week Magazine” or “Cosmopolitan” Site?
Unix timestamp is an amount of seconds lasted since 1970-01-01 UTC:
date --date='#1478698378000'
mar feb 1 14:26:40 CET 48828
BTW, dropping last three zeroes gives you back what you’ve expected:
date --date='#1478698378'
mié nov 9 14:32:58 CET 2016

bash date last with week number

To get the date of the last sunday one could use date --date="last sun" for Linux and date -j -v-sun for BSD.
How is it done however if I do have week numbers (1 to 7) instead of strings? I could always use a case-statement to convert the numbers to strings, but I bet there is a better solution for this?!
Thanks for your help.
Use an array:
number=1
days=(zero sun mon tue wed thu fri sat)
date --date="last ${days[number]}"

Rails getting the current time without the date

I know there are a lot of questions regarding date and time for Rails, but I can't seem to solve the problem I have.
I'm probably just being stupid about this.. anyway, my problem is that I want to get the current time without the date attached.
For example, when I use the following I get:
Time.now => 2012-06-29 09:23:04 -0400
or
Time.current => Fri, 29 Jun 2012 09:23:23 EDT -04:00
I would like to obtain the current time without having the date. i.e. just
09:23:04 -0400
or
09:23:23 EDT -04:00
If there are questions out there that already addresses this, I'd appreciate being pointed to them.
Thanks!
Use strftime to get the bits of the time that you want, in the format you want them.
puts Time.now.strftime("%I:%M:%S %z") # "09:33:00 -0400"
puts Time.now.strftime("%I:%M:%S %Z %z") # "09:33:00 EDT -0400"
Time.now.strftime("%H:%M:%S") # "18:33:00"
Don't use %I without %p (won't know am/pm)

The right ISO 8601 format

I am refactoring some code for a Ruby library. This code includes a Date parser.
One of the tests was to parse this string "2008-02-20T8:05:00-010:00" which is supposed to be ISO 8601.
The previous code would actually output: "Wed Feb 20 18:05:00 UTC 2008".
My new code outputs that: "Wed Feb 20 16:05:00 UTC 2008".
My question is: which one is the right one?
Time.parse in Ruby gives the second one. But again, I want to be 100% sure that the previous code AND test were buggy.
Which one is right? (By maybe parsing the string with a library in another language? - I only know Ruby.)
The correct UTC time is 1805. The time group indicates 0805 in zone -10, so to get UTC add the 10 to the given time. Thus 1805. Since 1805 is less than 2400 it's the same day.
If your code is giving 1605, then you almost certainly have the timezone set incorrectly to zone -8, which happens to be Pacific Standard Time.
Aha, looks like your input format is messed up. Observe:
irb(main):003:0> Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
I happen to be in zone -7 so it's suiting that to my locale. But
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):005:0> t
=> Wed Feb 20 08:05:00 -0700 2008
irb(main):006:0> t.getutc
=> Wed Feb 20 15:05:00 UTC 2008
I'm getting an unexpected result. Now observe:
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
=> Wed Feb 20 11:05:00 -0700 2008
irb(main):008:0> t.getutc
=> Wed Feb 20 18:05:00 UTC 2008
There's the expected result. See the difference? First example vs second:
irb(main):004:0> t=Time.parse("2008-02-20T8:05:00-010:00")
irb(main):007:0> t=Time.parse("2008-02-20T8:05:00-10:00")
I took the spurious extra 0 out (which I certainly didn't notice either) and whoosh, it works.
I know this is pretty old, but I just ran across it.
I'll bet that something somewhere is interpreting 010 as an octal number with the value 8. Perhaps it's a bug in the implementation of Time.parse()?

Resources