Can anyone kindly enlighten me on how to get previous day's date in 'YYYY-MM-DD' format using Lua?
I.E., a snippet that will return the date of the previous day from the day during which it is run.
Try
print(os.date("%Y-%m-%d",os.time()-24*60*60))
Strictly speaking this is only guaranteed to work on a POSIX system, but it probably works in most systems.
There is a library LuaDate which can be very helpful for Date Manipulations
http://luaforge.net/projects/date/
It is very easy to use, since it is documented well!
Related
Is there a way in Elixir or Erlang to print out the name of current timezone? I know that I can get the local time in Elixir by calling the Erlang Calendar module.
:calendar.local_time
and I can get the current time in UTC in Elixir by using the Calendar package:
Calendar.DateTime.now_utc()
However, neither of these packages provide me with a method that will return the name of the current time zone. I would like to store my dates in UTC but display them in the local time zone. Where I live, the current timezone is called "MST7MDT" (and "MST" when DST is not in effect) but I don't want to hard code those strings into my program.
Is there a way to have Elixir tell me that my current timezone is "MST7MDT", so I can then use the Calendar.DateTime functions to format my DateTimes correctly?
I think the best approach would be to just use :calendar.universal_time_to_local_time when displaying the dates to your end user.
But if you really need to get the current time zone of the system, and it's a Unix-like system, you can always do something like:
def get_timezone() do
{zone, result} = System.cmd("date", ["+%Z"])
if result == 0, do: String.trim(zone)
end
Not the most elegant solution, but works. There doesn't seem to be anything equivalent to java.util.TimeZone.getDefault() available.
I don't think there is actually the official way how to get local timezone information as a string in erlang, but you may try qdate library
I have an ISO-8601 datetime stamp, and need to convert it into local time in GMT. What is the way to do it in Ruby on Rails? I have '1325233011', and need to convert it into local time in GMT standards.
I think what you're asking for is a locale time in GMT+5.
Given an ISO timestamp, 1325233011
When I convert this to a locale-based date/time
Time.at(1325233011) => '2011-12-30 03:16:51 -0500'
Take a look at the ruby-docs, http://www.ruby-doc.org/core-1.9.3/Time.html for more information. Ruby has robust Time and Date classes with many helper utilities. My machine is configured for GMT-5 so it returns the local time. It's easy to change the way timezone settings are interpreted in your program, but that's for another day. Hope this helps!
From Collegue's help got it
Time.at(1325233011).to_datetime
For Iso-8601:
Time.at(1325233011).to_datetime.iso8601
For verification of time correct conversion and comparision use this link
http://coderstoolbox.net/unixtimestamp/
I thought the Time object in Ruby on Rails stores the time but when I ran Time.now.beginning_of_day it gives me the date as well. I'm just trying to capture the time and not the date at all. Is there a way to do this? Thanks
Yes you have to do Rails Date Formats – strftime
please see this kink http://www.wetware.co.nz/2009/07/rails-date-formats-strftime/
and this link http://apidock.com/ruby/DateTime/strftime
Example:
// in your case, should do it
Time.now.beginning_of_day.strftime("%I:%M")
With preceding zero (capital I)
Time.now.strftime("%I:%M") # => 05:21
Without preceding zero (lowercase L)
Time.now.strftime("%l:%M") # => 5:21
hope it help.
UPDATE: // well for your question on the comment below Why is Time storing date information? Shouldn't it just be time?
Good point for you question, as vladr mention it above(I am sure that the group of people who create rails will think very similar to vladr mention above when they gonna design Time object in rail), when you are talking about time, it cannot be help that it will related to the time zone too. In your case you want to use Time.now.beginning_of_day right? I am now in Thailand and assume that you are in America, Time in America is slower than Thailand for 12 hours, so right now in Thailand it's Tue 10:00 am, then in America should be Mon 10:00 pm, so you use Time.now.beginning_of_day (let use Time.now for more clear picture), so what should be the answer? so that's why I think the group of people who create rails use UTC+0 as a standard, In your case I recommend using Time.zone.now, and recently I have found some blog with interesting topic, please see http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails, hope it help. anyone who read this if you feel that something is missing, feel free to edit my answer, thank you very much for you guys :).
I can't find it specified anywhere. I did find a Microsoft example that had "5/5/1955". Is that d/m/y or y/m/d.
I'm guessing that I probably ought to use ISO 8601, but it would be nice to know for sure.
According to the OASIS spec, the type associated with URI
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth is xs:date, so it should be a normal XML date format... which is indeed ISO-8601, according to XML Schema Part 2. (That talks about the time zone for a date being representable, which strikes me as a little odd, but never mind.)
I did some searching but haven't landed anything that looks useful yet but I am wondering if anyone knows of something (tool,lib etc) that can parse English phrases and translate them into a cron string.
For example: Every Tuesday at 15:00 converts to 0 15 * * 2
It seems like something that would have lots of gotchas and it would be preferable to benefit from someone elses work. You see it in a few nice sites/apps that can work out what you mean from a simple phrase rather than having some hideous user interface.
Thanks in advance.
Though this is an old question I would like to list out all libraries/tools that I know so far, so that this answer might help others who arrive on this page looking for the same thing:
JavaScript:
natural-cron.js (link)
friendly-cron (link)
PHP:
natural-cron-expression (link)
Ruby:
whenever (link)
Feel free to reply in comments, if you know about any other library which is not listed here :)
(Full disclosure: natural-cron.js has been developed by me & my friend, when no other library satisfied the needs of our project)
For Ruby there's "Whenever" which might provide a starting point: It translates quasi-english (actually it's valid Ruby) into cron strings.
Depending on how flexible you need it to be, and how willing to roll up your own sleeves you are, you could define a simple grammar for this.
Every would be a quantifier. You may need others but I can't think of any. Valid syntax might be:
Every (day-spec) AT (time)
Where day-spec could be a literal day (ie. Monday) or be a day of the month (ie. 30th Day) or some other syntax (I'd suggest fortnights but I'm not sure if Cron can represent those well).
Time could be specified using either 24 hour (16:00) or 12 hour (4:00pm) format.
Another syntax that you might want is:
Every (frequency) From (time) where frequency is basically (quantity) (unit) (ie. 10 Minutes). The from time enables you to set an offset (eg. Every 30 Minutes From 01:10am).
You'd probably need to sit down and figure out these details a bit more. But a rigid grammar could be implemented relatively easily using recursive descent.
Hmm, about those gotchas... How about also writing one that translates the cron params back to English? That way you can see if the parser "understood" you.