I'm just wondering what could be the reason for this line to return a formatted string
print(os.time{year=2018, month=11, day=11, hour=11})
returns 2018-11-11 11:00:00
where if I go to lua demo it returns date in numbers
is that a reason because of system difference? What can I do to get these numbers? My main goal is to add certain of days to the date. In following format :
oldDate + (60*60*24*daysToAdd)
Probably you still can add days despite of non-standard os.time.
Try
local dt = os.date("*t") -- today
print(dt.day, dt.month) -- 28 nov
dt.day = dt.day + 111 -- add 111 days
dt = os.date("*t", os.time(dt))
print(dt.day, dt.month) -- 19 mar
Does it print March 19 ?
Related
There is a relatively new package that has come out called acmeR for producing estimates of mortality (for birds and bats), and it takes into consideration things like bleedthrough (was the carcass still there but undetected, and then found in a later search?), diminishing searcher efficiency, etc. This is extremely useful, except I can't seem to get it to work, despite it seeming to be pretty straightforward. The data structure should be like:
Date, in US format mm/dd/yyyy or ISO 8601 format yyyy-mm-dd
Time, in am/pm US format HH:MM:SS AM or 24-hr ISO format HH:MM:SS
ID, arbitrary distinct alpha strings unique to each carcas
Species, arbitrary distinct alpha strings (e.g. AOU, ABMP, IBP)
Event, “Place”, “Check”, or “Search” (only 1st letter counts)
Found, TRUE or FALSE (only 1st letter counts)
and look something like this:
“Date”,“Time”,“ID”,“Species”,“Event”,“Found”
“1/7/2011”,“08:00:00 PM”,“T091”,“UNBA”,“Place”,TRUE
“1/8/2011”,“12:00:00 PM”,“T091”,“UNBA”,“Check”,TRUE
“1/8/2011”,“16:00:00 PM”,“T091”,“UNBA”,“Search”,FALSE
My data look like this:
Date: Date, format: "2017-11-09" "2017-11-09" "2017-11-09" ...
Time: Factor w/ 644 levels "1:00 PM","1:01 PM",..: 467 491 518 89 164 176 232 39 53 247 ...
Species: Factor w/ 52 levels "AMCR","AMKE",..: 31 27 39 27 39 31 39 45 27 24 ...
ID: Factor w/ 199 levels "GHBT001","GHBT002",..: 1 3 2 3 2 1 2 7 3 5 ...
Event: Factor w/ 3 levels "Check","Place",..: 2 2 2 3 3 3 1 2 1 2 ...
Found: logi TRUE TRUE TRUE FALSE TRUE TRUE ...
I have played with the date, time, event, etc formats too, trying multiple formats because I have had some issues there. So here are some of the errors I have encountered, depending on what subset of data I use:
Error in optim(p0, f, rd = rd, method = "BFGS", hessian = TRUE) :non-finite value supplied by optim In addition: Warning message: In log(c(a0, b0, t0)) : NaNs produced
Error in read.data(fname, spec = spec, blind = blind) : Expecting date format YYYY-MM-DD (ISO) or MM/DD/YYYY (USA) USA
Error in solve.default(rv$hessian): system is computationally singular: reciprocal condition number = 1.57221e-20
Warning message: # In sqrt(diag(Sig)[2]) : NaNs produced
Error in solve.default(rv$hessian) : Lapack routine dgesv: system is exactly singular: U[2,2] = 0
The last error is most common (and note, my data are non-numeric, sooooo... I am not sure what math is being done behind the scenes to come up with these equations, then fail in the solve), but the others are persistent too. Sometimes, despite the formatting being exactly the same in one dataset, a subset of that data will return an error when the parent dataset does not (does not have anything to do with species being there/not being there in one or the other dataset, as far as I can tell).
I cannot find any bug reports or issues with the acmeR package out there - so maybe it runs perfectly and my data are the problem, but after three ecologists have vetted the data and pronounced it good, I am at a dead end.
Here is a subset of the data, so you can see what they look like:
Date Time Species ID Event Found
8 2017-11-09 1:39 PM VATH GHBT007 P T
11 2017-11-09 2:26 PM CORA GHBT004 P T
12 2017-11-09 2:30 PM EUST GHBT006 P T
14 2017-11-09 6:43 AM CORA GHBT004 S T
18 2017-11-09 8:30 AM EUST GHBT006 S T
19 2017-11-09 9:40 AM CORA GHBT004 C T
20 2017-11-09 10:38 AM EUST GHBT006 C T
22 2017-11-09 11:27 AM VATH GHBT007 S F
32 2017-11-09 10:19 AM EUST GHBT006 C F
I have a date value like 2016-11-19 and a time value like 2000-01-01 20:14:00 +0000
I want to convert it to proper date format which will show utc time like below:
2016-11-20 04:14:00 +0000
There lots of questions regarding this, but I am confused.
You need to convert your date value to a time value:
>> date = Date.new(2016, 11, 19)
>> time = date.to_time
=> 2016-11-19 00:00:00 -0700
Note that time will be set to midnight in the time zone to which your server is set. Now that you have a time object, you can add seconds to it:
>> time_increment = Time.new(2000, 1, 1, 20, 14, 0)
>> seconds_increment = time_increment.seconds_since_midnight
=> 72840.0
>> new_time = time + seconds_increment
=> Sat, 19 Nov 2016 20:14:00 MST -07:00
You can use Ruby strftime datetime format.
Like
date_value.strftime('%Y-%m-%d %H:%M:%S') #=> 24-hour clock
date_value.strftime('%Y-%m-%d %I:%M:%S') #=> 12-hour clock
Full documentation
Thanks everyone! But, I could solve the problem on my own!
time = bid_end_time.hour.to_s + ":" + bid_end_time.min.to_s + ":" + bid_end_time.sec.to_s
date_time = bid_end_date.to_s + " " + time
bid_end_date_time = Time.zone.parse(date_time).utc
I'm working on a thing that calculates that turns a number eg 900 into a human readable date.
I've got turning 365 into 1 year 0 months & 0 days.
But, how do I turn 365 into 20/3/15
Lua standard library os provides the functions time and date for such things.
But can use other libraries as well. Like wxLua e.g.
First you need the current time:
local currentTimeInSeconds = os.time()
Then you need to go back in time. Remeber 2016 is a leap year! So instead of 365 you have to go 366 days back.
local timeAgo = 366 * 24 * 60 * 60
Then call os.date() to convert the time in seconds to a date
print(os.date("%d/%m/%y", currentTimeInSeconds - timeAgo))
Which will give you the output
20/03/15
Please refer to the Lua 5.0 PIL for more info
local t = os.date("*t", os.time())
t.day = t.day - 900
local ago = os.time(t)
ago is the timestamp of the time 900 days ago. You can get the formatted date as you want:
print(os.date("%d/%m/%y", ago))
I have a breeze entity with a date in it, I need to get the year, month, day separately and was going to use momentjs to do it but I'm getting some strange results for something that I would have thought would be quite simple:
var dob = moment(observableDate());
console.log(observableDate() + ' to -> ' + dob.day() + ' - ' + dob.month() + ' - ' + dob.year());
//ouput
//Thu Dec 18 1975 11:00:00 GMT+1100 (AUS Eastern Summer Time) to -> 4 - 11 - 1975
I don't understand where the 4th of Nov is coming from....
The date is stored in Sql Server and the value is '1975-12-18 00:00:00.000'
Thanks in advance.
According to the moment.js documentation
day() returns the day of the week, i.e. a number between 0 and 6; (4 == thursday).
month() returns the month of the year but 0 origin. i.e. a number between 0 and 11 - (11 == december)
See: Moment.js docs
I see that typing 100.days gives me [edit: seems to give me] a Fixnum 8640000:
> 100.days.equal?(8640000)
=> true
I would have thought those two values were interchangable, until I tried this:
x = Time.now.to_date
=> Wed, 31 Oct 2012
> [x + 100.days, x + 8640000]
=> [Fri, 08 Feb 2013, Mon, 07 May 25668]
Why, and how, does adding apparently equal values to equal dates give different results?
The above results are from the Rails console, using Rails version 3.1.3 and Ruby version 1.9.2p320. (I know, I should upgrade to the latest version...)
100.days doesn't return a Fixnum, it returns an ActiveSupport::Duration, which tries pretty hard to look like a integer under most operations.
Date#+ and Time#+ are overridden to detect whether a Duration is being added, and if so does the calculation properly rather than just adding the integer value (While Time.+ expects a number of seconds, i.e. + 86400 advances by 1 day, Date.+ expects a number of days, so +86400 advances by 86400 days).
In addition some special cases like adding a day on the day daylight savings comes into effect are covered. This also allow Time.now + 1.month to advance by 1 calendar month irrespective of the number of days in the current month.
Besides what Frederick's answer supplies, adding 8640000 to a Date isn't the same as adding 8640000 to a Time, nor is 100.days the correct designation for 100 days.
Think of 100.days meaning "give me the number of seconds in 100 days", not "This value represents days". Rails used to return the number of seconds, but got fancy/smarter and changed it to a duration so the date math could do the right thing - mostly. That fancier/smarter thing causes problems like you encountered by masking what's really going on, and makes it harder to debug until you do know.
Date math assumes day values, not seconds, whereas Time wants seconds. So, working with 100 * 24 * 60 * 60 = 8640000:
100 * 24 * 60 * 60 => 8640000
date = Date.parse('31 Oct 2012') => Wed, 31 Oct 2012
time = Time.new(2012, 10, 31) => 2012-10-31 00:00:00 -0700
date + 8640000 => Mon, 07 May 25668
time + 8640000 => 2013-02-08 00:00:00 -0700
date + 100 => Fri, 08 Feb 2013
It's a pain sometimes dealing with Times and Dates, and you're sure to encounter bugs in code you've written where you forget. That's where the ActiveSupport::Duration part helps, by handling some of the date/time offsets for you. The best tactic is to use either Date/DateTime or Time, and not mix them unless absolutely necessary. If you do have to mix them, then bottleneck the code into methods so you have a single place to look if a problem crops up.
I use Date and DateTime if I need to handle larger ranges than Time can handle, plus DateTime has some other useful features, otherwise I use Time because it's more closely coupled to the OS and C. (And I revealed some of my roots there.)