Ruby Timezones — to UTC and back - mysql

I run a scheduling site that allows users to set a timezone for their account. They can then schedule 'posts' on a calendar for that timezone. In the back-end, I take the time and convert it into UTC time (where a worker processes and picks up those posts on UTC time).
I run the site on Ruby (with Puma/Sinatra), but without Rails, and I'd like to stay away from Rails. There's HTML/ERB/JS in the front and a MYSQL backend.
I thought that simply adding/subtracting UTC offsets will solve all problems. I have a simple list that has TIMEZONE <--> UTCOFFSET and this worked very well — or so I thought.
And then I realised this does not work when a given timezone (or place) is in DST currently.
Vienna in the summer: UTC offset: +2h
Vienna in the winter: UTC offset: +1h
Is there a simple way to give Ruby (not Rails) a "timezone ID" and receive the correct adjusted UTC offset back?
Like:
user.timezone = "Europe/Vienna"
utc_adjusted_time = post.local_time.to_i + get_utc_offset(user.timezone)
Where get_utc_offset knows whether it is in DST or not. Any ideas?
I've gone through the IANA database (which is absolute overkill), I've searched through numerous Gists on Github, I've tried to abuse whatever Rails seemed to have, but didn't get very far.
How do you guys handle this? Ideally, as said, each post will be converted and saved to a DST-stripped UTC time. Thanks!

Just use the Ruby TZInfo gem.
Your use case is exactly the one shown in the readme:
require 'tzinfo'
tz = TZInfo::Timezone.get('America/New_York')
local = tz.utc_to_local(Time.utc(2005,8,29,15,35,0))
utc = tz.local_to_utc(local)
period = tz.period_for_utc(Time.utc(2005,8,29,15,35,0))
id = period.zone_identifier
Looking at the docs for TZInfo::TimePeriod you can see the other information you asked about:
period.offset
period.dst?
... etc.
FYI - the Rails time zone support is based on this gem anyway. Then Rails goes and puts some funky aliasing over the top (which I advise against using). See the section on Rails in the timezone tag wiki.

Related

Mediawiki bot to find out the changes of the last hours

Asking for:
I want to use a bot (mediawiki api or direct database access), that find out the changes of all pages of the last hours. I need a diff and want to process the text afterwards. A guess into the right direction (diff of last edits) is enough. I know how to iterate pages.
Background:
I want to process the diff to find out new download links as part of template call. For this step I don't need help.
You can use pywikibot, This should be a (maybe) good start:
from pywikibot import Site
from datetime import timedelta
my_site = Site(language, family) # Or set default site with pywikibot config file
# From this object you may get the changes by timestamp, pywikibot.Timestamp are datetime objects as well.
current_time = my_site.getcurrenttime()
my_site.recentchanges(start = current_time, end=current_time - timedelta(hours=6))
You can iterate the pages which changed in the last X (6) hours and for each one:
from pywikibot import Page
current_page = Page(my_site, page_title) # Page title written in the recentchanges
return object
revisions = current_page.revisions(total=some_number, content=True)
After that you can use some internals api to get the diff between two revisions:
from pywikibot.diff import PatchManager
PatchManager(first_rev_text, second_rev_text).print_hunks() # print_hunks is for interactive changes, but you can work with any internals api here (that might not be simple).
That not fully code as you need but you can work from here and add some more logic to solve your case,
Good luck:)

How to get the current time in UTC?

I'm trying to get the current time in UTC in a UWP application. This should be a simple matter of constructing a DateTime object
DateTime const now{ clock::now() };
and accessing its UniversalTime field. However, that field does not appear to have been projected into C++/WinRT.
How do I get the current time in UTC using C++/WinRT?
Looks like a documentation bug. That field doesn't exist in C++/WinRT. Instead, DateTime is projected as a std::chrono::time_point. But similar to C++/CX, the documentation for the struct (not the field) is still somewhat accurat - it has the same granularity as FILETIME. But even easier than extracting the value youself, winrt::clock provides static methods to_file_time and to_time_t that convert DateTime to
FILETIME or time_t, respectively.
I'll get the documentation fixed up, and I've been meaning to write a blog post about how C++/WinRT seamlessly interops with std::chrono for timekeeping. I'm a a fan of std::chrono, and incorporating it into C++/WinRT was my idea, so the haters know who to blame. :)

MediaWiki: Convert seconds to HH:MM:SS

How do I convert seconds to HH.MM:SS via MediaWiki (plus Semantic Media Wiki)?
I tried and tried and just can't find a way.
The duration in seconds comes via filling out a template.
As I can't install more extensions to the Wiki the only ways are via SMW and parser functions.
Thank you very much in advance!
You should be able to do this with the time parser function:
{{#time: H.i.s | #123 }}
It't not strickly treating it as a time duration, but rather a time from the unix epoch, but if you don't go over 24 hours it doesn't really matter.
If you need to go higher, then you can build up a custom expression using the trunc operator, for example to get the number of hours:
{{#expr: trunc (123/60/60) }}

What does an Alembic revision ID represent?

I have just started looking at Alembic, and coming from Django, where we have South to migrate our database schemas (which is soon to be included) which uses a friendly old fixed-width number like 0037_fix_my_schema.py to talk about the order in which migrations are to be applied, I am naturally intrigued by Alembic's revision ID. Is there a DAG backing Alembic, or can someone give a little overview of its internals in this respect?
I took a look myself. The source says:
def rev_id():
val = int(uuid.uuid4()) % 100000000000000
return hex(val)[2:-1]
Not so fascinating.

post timestamp in web_submit_data itemdata in loadrunner

In our hyperion application, we have a explore button.
Here is its post:
web_submit_data("explorer", //FIXME: id vlaue parameter
"Action=https://{host_url}/raframework/browse/explorer",
"Method=POST",
"TargetFrame=",
"RecContentType=application/x-json",
"Referer=https://{host_url}/workspace/index.jsp?framed=true",
"Snapshot=t19.inf",
"Mode=HTML",
ITEMDATA,
"Name=class", "Value=com.hyperion.tools.cds.explorer.ExplorerView", ENDITEM,
"Name=id", "Value=EV1390418511260", ENDITEM, //"Name=id", "Value=EV1389926310921", ENDITEM,
LAST);
This EV1390418511260 is from
this._rstExplorerViewId = "EV" + (new Date()).getTime();
from the loaded module.js file I guess, which is Unix time stamp
I tried lr_save_timestamp("timestamp", LAST ); to correlate value of id with Unix time stamp. like Value=EV{timestamp}. The request is being posted but the response body has
Content-Length: 0
X-ORACLE-BPMUI-CSRF: false
I want to try
typedef long time_t;
time_t t;
and correlate something like Value=EV{time(&t)} but here it converts to url encode and takes ascii values of those special chars.
What should I do?
Why are you using C code to replace the functionality of built in capability to LoadRunner?
See web_save_timestamp_param() for the number of milliseconds since Jan 1, 1970.
web_save_timestamp_param("tStamp", LAST );
web_submit_data("explorer",
...
ITEMDATA,
"Name=class", "Value=com.hyperion.tools.cds.explorer.ExplorerView", ENDITEM,
"Name=id", "Value=EV{tStamp}", ENDITEM,
LAST);
if this is your first trip into the Hyperion universe I would heartily suggest you find the most experienced LoadRunner professional available anywhere in the nation to support your efforts. You do not need to be struggling with tool use while at the same time working in one of the more difficult environments for any performance testing tool. Assuming you get someone who has been succesful with Hyperion recently, no matter what your organization spends (even as high as $300 per hour or more) it will be money well spent versus struggling with both tool mechanics and testing of the environment