Free Rest API to retrieve current datetime as string (timezone irrelevant) [closed] - rest

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I am seeking a reliable REST API that can provide world time and time zone information across platforms.
I need the current time as a string. I'd like it to return the result in under a second, regardless of the user's location worldwide.
Among other implementations I want to use this for a consistent countdown timer, to be more accurate than a user's [possibly-inaccurate] computer time. It can be GMT or another time zone, as long as the time zone and offset is specified, like 2012-11-05 16:16:50 EST.
I would build this API myself, but have concerns of potential latency issues (as well as inelegance) when filtering someone through a whole big software stack like Rails just to return a simple String.
Excessive latency for users far away from the US east coast would offset the benefit of accuracy that the task requires.
Any suggestions and/or examples are appreciated.

TimezoneDb provides a free API: http://timezonedb.com/api
GenoNames also has a RESTful API available to get the current time for a given location: http://www.geonames.org/export/ws-overview.html.
You can use Greenwich, UK if you'd like GMT.

This API gives you the current time and several formats in JSON - https://market.mashape.com/parsify/format#time. Here's a sample response:
{
"time": {
"daysInMonth": 31,
"millisecond": 283,
"second": 42,
"minute": 55,
"hour": 1,
"date": 6,
"day": 3,
"week": 10,
"month": 2,
"year": 2013,
"zone": "+0000"
},
"formatted": {
"weekday": "Wednesday",
"month": "March",
"ago": "a few seconds",
"calendar": "Today at 1:55 AM",
"generic": "2013-03-06T01:55:42+00:00",
"time": "1:55 AM",
"short": "03/06/2013",
"slim": "3/6/2013",
"hand": "Mar 6 2013",
"handTime": "Mar 6 2013 1:55 AM",
"longhand": "March 6 2013",
"longhandTime": "March 6 2013 1:55 AM",
"full": "Wednesday, March 6 2013 1:55 AM",
"fullSlim": "Wed, Mar 6 2013 1:55 AM"
},
"array": [
2013,
2,
6,
1,
55,
42,
283
],
"offset": 1362534942283,
"unix": 1362534942,
"utc": "2013-03-06T01:55:42.283Z",
"valid": true,
"integer": false,
"zone": 0
}

If you're using Rails, you can just make an empty file in the public folder and use ajax to get that. Then parse the headers for the Date header. Files in the Public folder bypass the Rails stack, and so have lower latency.

Related

How to calculate countdown timer with a single timezone for all users in dart?

I'm creating an auction page.As of now the code works fine and Its showing a countdown timer which only calculates the difference according to the local timezone but not all users will have the same timezone. So I tried .toUtc but it still calculates from the device local timezone.
Im using this package to calculate and display timer.
https://pub.dev/packages/flutter_countdown_timer
All these codes are from the package.
_diffTime(Duration duration) {
value = max(value - duration.inMilliseconds, 0);
_lastTimestamp = DateTime.now().toUtc().millisecond;
if (value <= 0) {
stop();
onEnd?.call();
return;
}
}
This is the input for the timer.
CountdownTimer(
endTime: DateTime(2020, 10, 22, 12, 48, 00).toUtc().millisecondsSinceEpoch,
textStyle: TextStyle(fontSize: 30, color: Colors.pink),
)
Your CountdownTimer object hard-codes the end time to be DateTime(2020, 10, 22, 12, 48, 00).toUtc(). This creates a DateTime object representing October 22, 2020 12:48:00 PM in your local time and then returns the corresponding UTC time. Your hard-coded end-time therefore would represent different points in time when your code is run on systems using different local time zones.
If you instead want October 22, 2020 12:48:00 PM as a UTC time so that it represents the same moment in time regardless of the system's local time zone, then you should construct it as DateTime.utc(2020, 10, 22, 12, 48, 00). DateTime.utc(...) and DateTime(...).toUtc(...) are not the same thing.
Also, even though your _diffTime function is completely irrelevant to your question, I'll point out that:
_lastTimestamp = DateTime.now().toUtc().millisecond;
doesn't do what you probably intend. DateTime.now().toUtc().millisecond returns the millisecond component of the current DateTime. You likely instead intended to use DateTime.now().millisecondsSinceEpoch. Note that .toUtc() is unnecessary.

Graph multicolor area line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
i need to create graph as showed on image.
One line with color filled area,
if point value is more than zero than color of line and area is green, else red.
How can i do this? JS (some plugin?) or PHP (imagick, gd)
You can use Highcharts area series type and define negativeColor for it. Check the docs and example posted below.
HTML:
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
JS:
Highcharts.chart('container', {
chart: {
type: 'area'
},
series: [{
color: 'rgba(0, 255, 0, 0.7)',
negativeColor: 'rgba(255, 0, 0, 0.7)',
fillOpacity: 0.2,
marker: {
enabled: false
},
data: [5, 3, 4, 7, 2, -3, -5, -2, -7, -4, 0, 3, 4, 2, 5, 1]
}]
});
Demo:
https://jsfiddle.net/BlackLabel/xa91d8o7/4/
Docs:
https://www.highcharts.com/demo/area-negative
https://api.highcharts.com/highcharts/series.area

MATLAB Cut 3D array of daily data into monthly segments

I have a 1437x159x1253 large matrix (let's call it A) of daily sea ice data for a little over 2 years. I need to write a code that takes the daily data from each month and does mean(A, 3) on it. So basically, 1253 is the t in days. If I start from January, I need to do mean(A,3) of the first 31 days, then the mean(A,3) of February, the next 28 or 29 days. Because the days alternate between 31 and 30 (and 28 or 29 for February), I don't know how to write a code to do this. I can do it manually, but that would take a while.
Thanks!
You can initialize an array containing the number of days in each month, Mon = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] using boolean to check whether it's a leap year (to set Mon(2) = 29). The number of days will help you index each month appropriately, using a loop like:
index=1;
for i=1:12
average = mean(A(:,:,index:(index+Mon(i)-1),3);
index = index+M(i); % Starting location of the next month
end

Schema for opening hours MongoDB

How would you store opening hours on a document, say a Library, in mongoDB, that's easily queryable with Mongoid? I've read this thread, but I'm not sure how it would be implemented with my needs.
I need to have the ability to add multiple opening and closing times per day since the Library should be able to close some hours during the day and then reopen.
I need to be able to add exceptions to these opening hours. For example; close monday on a specific date.
Please share some best practices and experiences on how one could do this the most flexible way.
Thank you, Yeggeps, for the list of requirements.
This is a revised answer based on your requirements. Of course there is no holy grail to schemae, but I would like to motivate my answer before revision (keeping a flat structure is easy to query and maintain) with some sample data + queries based on your requirement list. I reiterate, I am not saying this is the best solution, but it is a solution which is straightforward to query and easy to maintain (imho).
Code is a little quick and dirty, apologies. The data:
[
# library "lib1" open on wednesdays from 8:00 until 17:00
{"lib_id" => "lib1", "type" => "hours", "opening" => 800, "closing" => 1700, "day_of_week" => 3},
# library "lib1" open on wednesdays from 19:00 until 22:15
{"lib_id" => "lib1", "type" => "hours", "opening" => 1900, "closing" => 2215, "day_of_week" => 3},
{"lib_id" => "lib1", "type" => "hours", "opening" => 800, "closing" => 1700, "day_of_week" => 4},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1100, "closing" => 1700, "day_of_week" => 3},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1400, "closing" => 1700, "day_of_week" => 4},
{"lib_id" => "lib2", "type" => "hours", "opening" => 1900, "closing" => 2100, "day_of_week" => 4},
# library lib1 closed on wednesday december 7th 2011
{"lib_id" => "lib1", "type" => "closed_on", "reason" => "Rearranging the shelves", "closed_date" => Time.utc(2011, 12, 8)},
{"lib_id" => "lib2", "type" => "closed_on", "reason" => "We are closed for the holidays", "closed_date" => Time.utc(2011, 12, 7)}
].each do |schedule|
coll.save(schedule)
end
Show opening hours and exceptional dates separately:
# List all the library id's distinctly
coll.distinct("lib_id").each do |lib_id|
puts "\nLibrary #{lib_id} opening hours:\n--- "
# I need to be able to show the opening hours in correlation with the Library
# Find all the opening hour information for current library
coll.find({"lib_id" => lib_id, "type" => "hours"}).each do |schedule|
puts " #{Date::DAYNAMES[schedule["day_of_week"]]}s: #{schedule["opening"]} - #{schedule["closing"]}" if schedule["type"] == "hours"
end
# I need to show an indication if it's open or closed in correlation with the Library.
puts "This library will be closed on: "
# Find all the exceptions for current lib_id -- introduce a time-period restriction using Date.utc (...)
coll.find({"lib_id" => lib_id, "type" => "closed_on"}).each do |closed|
puts " #{closed["closed_date"].strftime("%a %B%e, %Y")}: #{closed["reason"]}"
end
end
Which libraries are open today?
# I need to be able to query on what's open right now or some time in the future with minute granularity
# here I'll also need to be able to exclude the Librarys that has added exceptions for the given time/day
puts "---"
qtime = (Time.now.hour * 100) + Time.now.min # minute granularity
qwday = Time.now.wday # this example only shows today
qclosed = Time.utc(Time.now.year, Time.now.mon, Time.now.mday)
# Query for all library ids which have opening times for this weekday, at this hour (+minutes)
coll.find({"opening" => {"$lte" => qtime}, "closing" => {"$gte" => qtime}, "day_of_week" => qwday}, {:fields => "lib_id"}).each do |lib|
# Check whether current library has an exception for this specific day
closed = coll.find_one({"lib_id" => lib["lib_id"], "closed_date" => qclosed})
if closed
# If an exception record was encountered, print the reason
puts "Library #{lib["lib_id"]} is normally open right now, but is now closed: '#{closed["reason"]}'"
else
# Else: the library is open
puts "Library #{lib["lib_id"]} is open right now! (#{Time.now.strftime("%a %B%e %Y, %H:%M")})"
end
end
Produces output as follows:
Library lib1 opening hours:
---
Wednesdays: 800 - 1700
Wednesdays: 1900 - 2215
Thursdays: 800 - 1700
This library will be closed on:
Thu December 8, 2011: Rearranging the shelves
Library lib2 opening hours:
---
Wednesdays: 1100 - 1700
Thursdays: 1400 - 1700
Thursdays: 1900 - 2100
This library will be closed on:
Wed December 7, 2011: We are closed for the holidays
---
Library lib1 is open right now! (Wed December 7 2011, 13:12)
Library lib2 is normally open right now, but is now closed: 'We are closed for the holidays'
Admittedly, the downside to my proposed solution is that it does not capture every requirement in one query.
It's difficult to provide a good solution without knowing the exact queries you'd like to run. For instance, if you're asking "what businesses are open now (5:32 PM, 5/11/2011)?" you'd want a different schema than if you were asking "when is business XYZ open next?"
In the first case, you'll want to be able to efficiently pose range queries on the current hour, minute, and day -- as well as negative queries on an exception list. Alternatively, you can handle exceptions in client code.
Last, what is the level of granularity needed? What is the smallest exception possible? Minutes? Hours? Days?
I'd post the above as a comment but I just created a user account. With additional information, I'll update this to provide an actual answer.

Is it ideal that MongoDB is using 150 MB memory?

This is the first project by me which is using MongoDB.
I have hosted it on a linode (a VPS which uses XEN) and I'm checking memory usage with "top".
The mongod process seem to use around 150 MB of memory. There were no connections to it when I checked. I use RockMongo to administer it. My main database stats are -
Size - 464m
Storage Size - 83.99m
Data Size - 66.4m
Index Size - 49.33m
Collections - 5
Objects - 584850
A lot of queries happen when the cron job is running, around 75 per minute or even more. But, as I said earlier, when I checked the memory usage, there were no connections.
Output of db.serverStatus();
Note - I had restarted mongod before running db.serverStatus(); and the memory usage was 40 MB.
{
"retval": {
"version": "1.6.5",
"uptime": 790,
"uptimeEstimate": 783,
"localTime": "Mon, 07 Feb 2011 00: 51: 04 -0500",
"globalLock": {
"totalTime": 790027671,
"lockTime": 376381,
"ratio": 0.00047641495838188,
"currentQueue": {
"total": 0,
"readers": 0,
"writers": 0
}
},
"mem": {
"bits": 64,
"resident": 38,
"virtual": 957,
"supported": true,
"mapped": 288
},
"connections": {
"current": 2,
"available": 9598
},
"extra_info": {
"note": "fields vary by platform",
"heap_usage_bytes": 152448,
"page_faults": 0
},
"indexCounters": {
"btree": {
"accesses": 1,
"hits": 1,
"misses": 0,
"resets": 0,
"missRatio": 0
}
},
"backgroundFlushing": {
"flushes": 13,
"total_ms": 1,
"average_ms": 0.076923076923077,
"last_ms": 0,
"last_finished": "Mon, 07 Feb 2011 00: 50: 54 -0500"
},
"cursors": {
"totalOpen": 0,
"clientCursors_size": 0,
"timedOut": 0
},
"opcounters": {
"insert": 0,
"query": 57,
"update": 0,
"delete": 0,
"getmore": 0,
"command": 46
},
"asserts": {
"regular": 0,
"warning": 0,
"msg": 0,
"user": 0,
"rollovers": 0
},
"ok": 1
},
"ok": 1
}
A friend of mine runs his WordPress blog on a linode with same amount of ram (1024 MB). His MySQL usage show mere 20.48 and approx. 12 users are like "always-surfing" (as in always-on) on his site.
This makes me feel MongoDB isn't a nice choice for me and I should have sticked to MySQL!
Thank you, all.
"Using" that much memory isn't as bad as it seems ... MongoDB will (at least seem to) use up a lot of available memory, but it leaves it up to the OS's VMM to tell it to release the memory when need. (see Caching in the MongoDB docs.)
For the most part it's "using" that memory for cache, which dramatically speeds things up.
You should be able to release any and all memory by restarting MongoDB.
However, to some extent MongoDB isn't really actively "using" the memory ... read on for a lot more details in this anwser ..
How to release the caching which is used by Mongodb?
Memory management is soley up to the OS.
Read
http://blog.mongodb.org/post/101911655/mongo-db-memory-usage
There is basically no way right now to influence the memory usage....and mentioned: learn about memory-mapped files and don't mix up the memory usage of memory mapped files with the actual memory usage.