I'm building a scraper a GWT based web app, to automatize some tasks we need to do on interval basis. There are two date pickers where to choose start date and end date. I see that there is an algorithm to convert Javascript dates to strings to send along the http request.
For instance:
11-08-2015 is VDkLYWA
11-09-2015 is VDpU$GA
11-10-2015 is VDuej2A
I tried to dig in the JS code but, as you may imagine, with not much luck.
I'm guessing there is an non-obfuscated version of the de-serialization in the GWT project source code so that I can reverse-engineer it, but I have no experience with GWT and don't know exactly where to look.
Maybe someone has already solved this problem or can point me to some resources?
Thanks!
GWT has a class com.google.gwt.lang.LongLib (inside gwt-dev.jar) which converts date long value to string conversion. This conversion is basically Base64 conversion. Below is a sample how it works:
Date d = new Date(115, 10, 8, 0, 0, 0); //2015-11-08 00:00:00
String s = LongLib.toBase64(d.getTime());
System.out.println(s); //Prints VDjNlRA
So, either you can include this jar in your application to do this conversion or you can write the same algorithm in javascript.
Related
I have an app that is currently running in Production. Suddenly, I have experienced a weird issue that I am not able to figure it out to prevent it. Any suggestion is highly appreciate.
This is the function where the error happened:
date_time: new Date(
this.selectedDate.getFullYear(),
this.selectedDate.getMonth(),
this.selectedDate.getDate(),
this.selectedHour.value.split(':')[0],
this.selectedHour.value.split(':')[1],
0
).toLocaleString(),
This date_time will be saved in database.
Output Example: 12/9/2021, 2:00:00 PM. Everything is working as expected.
However, today there is new record in database with different format: 12/9/2021 2:00:00 p.m. And it messed up my app. Do you know what happen to toLocaleString() ? Thank you.
Since you tagged this angular I assume this code is running client-side and sending the resulting string to the server for insertion into your database.
toLocaleString converts a date to a string formatted for the user's locale.
It is designed to display a human readable date.
If a user with their system set to a different locale (likely because they are from a different country to you) then it will give different results.
If you want a standard date in a format that is easily machine processable (i.e. good for storing in a database) then use toISOString.
You can parse it and convert it to a local string for display later.
I have been using 'date as a long' (getTime() / +) all the time when operating on dates in client side Javascript, for comparison, evaluation and also for transport of date from (not to) our Java server side to client side Javascript.
It saved me from the headache of browser misbehaviour in date handling.
But recently this practice was questioned and I still haven't got any reason why I should use string dates instead. Are there any problems in this usage of timestamps that I am unable to see?
There should not be any problem when your server side is Java.
Only thing I can add is - it is handy to use a javascript library like moment.js on client side. moment.js is a rich date handling library. Go for it if you have to do lot of date manipulations, display in different formats or read date from user.
JS timestamps are milliseconds-since-the-epoch. Most server systems (unless you're dealing with node.js) use plain seconds-since-the-epoch. Any JS timestamp representing "now"-type dates are therefore WAY past the size limits of a normal 32bit signed unix timestamp, and will be truncated/corrupted.
Sending a string avoids this, because pretty much any decent system can accept a date string and convert it back into its own native time system, whereas taking a corrupted truncated int is just a matter of garbage-in, garbage-out.
Of course, if your server-side systems are aware they'll be receiving a JS timestamp, they can accept as a string, then do some basic truncation to lop off the milliseconds component, leaving them with a normal seconds-based timetamp string that can be converted to an int.
This is a long-shot and I'm writing because I have not idea where to start.
I want to write some code that can automatically and on regular basis grab the 5 dates from this website and put them into my iCal calender.
Where should I start and end to do this?
I'm pretty good in RoR and Javascript, but have absolutely no idea what technology I should use to accomplish this.
Hope you can shed some light on my question.
Thanks
Assuming the HTML page is always going to keep the same basic structure, you could use something like nokogiri to locate the nodes containing the dates.
You can then use the Date.strptime or DateTime.strptime methods to convert the date from the particular format, into a Date or DateTime object, as required.
As for then adding the dates to your calendar, it's not something I have had to do, but you might want to check out How to interact with a CalDAV server from Ruby?
Use an XMLHttpRequest object in Javascript to download the page that you need and then use a regular expression to parse out the dates. It seems that the dates all have a fixed format:
<b>Mon Day Hr:Min UTC+4</b>
so it should be easy to write the regular expression for this. I don't know the exact Javascript Regex format but here's the .NET equivalent, it should be easy to tweak this to Javascript - hope this helps:
<b>(?<date>(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2} [0-9]{2}:[0-9]{2}) UTC[+-][0-9]+</b>
This finds all date fields in the page - once you have the date fields, each Regex match will have a sub-group named date that contains the actual date part.
If you go to this page: .NET Regex tester you can test the above expression to see how it returns the dates - just copy & paste your page's source with the dates. As I said, this is for .NET, not for Javascript but the differences are not terribly big.
Use a Ruby script. The Mechanize gem can scrape the dates from the web page. Then the ri_cal gem can add them to your calendar. A pure JavaScript approach like xxbbcc suggested may be possible but it will almost certainly be more involved. If you're already familiar with Ruby, I'd recommend taking advantage of the "magic" and let these gems do the dirty work for you.
I am using intalio editor in that I want to convert the Hijri date into a Gregorian date or vice-versa. we write code in javascript using jsx3 so please help me.
For Gregorian->Hijiri, see here
Bidirectional C# version here that could be converted to Javascript
EDIT: An excellent page and convertors can be found here. That page says:
All calculations are done in
JavaScript executed in your own
browser; complete source code is
embedded in or linked to this page,
and you're free to download these
files to your own computer and use
them even when not connected to the
Internet.
which suggests to me you can use the code, but you probably want to check with the page author
It helps to know that muslims refer to the Gregorian calendar as Masihi. Googling for "hijrah to masihi converter" turns up this page as the second hit: http://www.islamicity.com/PrayerTimes/defaultHijriConv.asp
The javascript is code contains two functions GregToIsl and IslToGreg that does what you want.
There is a copyright notice on that page so you shouldn't simply copy-paste the code. But the functions are short enough for you to extract the relevant maths out of.
The maths/algorithm itself is almost a thousand years old so it shouldn't have any legal restriction.
Try dojox.date.islamic
In Javascript the correct way for such conversion is to use Intl object (read more) as following:
a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)
Let's say I have a date that I can represent in a culture-invariant format (ISO 8601).
I'll pick July 6, 2009, 3:54 pm UTC time in Paris, a.k.a. 5:54 pm local time in Paris observing daylight savings.
2009-07-06T15:54:12.000+02:00
OK... is there any hidden gem of markup that will tell the browser to convert that string into a localized version of it?
The closest solution is using Javascript's Date.prototype.toLocaleString(). It certainly does a good job, but it can be slow to iterate over a lot of dates, and it relies on Javascript.
Is there any HTML, CSS, XSLT, or otherwise semantic markup that a browser will recognize and automatically render the correct localized string?
Edit:
The method I am currently using is replacing the text of an HTML element with a localized string:
Starting with:
<span class="date">2009/07/06 15:54:12 GMT</span>
Using Javascript (with jQuery):
var dates = $("span.date", context);
// use for loop instead of .each() for speed
for(var i=0,len=dates.length; i < len; i++) {
// parse the date
var d = new Date(dates.eq(i).text());
// set the text to the localized string
dates.eq(i).text(d.toLocaleString());
}
From a practical point of view, it makes the text "flash" to the new value when the Javascript runs, and I don't like it.
From a principles point of view, I don't get why we need to do this - the browser should be able to localize standard things like currency, dates, numbers, as long as we mark it up as such.
A follow up question: Why do browsers/the Web not have such a simple feature - take a standard data item, and format it according to the client's settings?
I use toLocaleString() on my site, and I've never had a problem with the speed of it. How are you getting the server date into the Date object? Parsing?
I add a comment node right before I display the date as the server sees it. Inside the comment node is the date/time of that post as the number of milliseconds since epoch. In Rails, for example:
<!--<%= post.created_at.to_i * 1000 %>-->
If they have JS enabled, I use jQuery to grab those nodes, get the value of the comment, then:
var date = new Date();
date.setTime(msFromEpoch);
// output date.toLocaleString()
If they don't have JS enabled, they can feel free to do the conversion in their head.
If you're trying to parse the ISO time, that may be the cause of your slowness. Also, how many dates are we talking?
Unfortunately, there is not.
HTML & CSS are strictly used for presentation, as such, there is no "smarts" built in to change the way things are displayed.
Your best bet would be to use a server side language (like .NET, Python, etc.) to emit the dates into the HTML in the format you want them shown to your user.
It is not possible to do this with HTML, it has no smart tags that can make any kind of decisions like this. It is strictly presentational. I do wonder, though, if HTML5 perhaps has a tag for something like this...
Anyways, the way I see it, you have 3 options:
Stick to the Javascript way. There's questions with more details on it on this website, such as How do I display a date/time in the user’s locale format and time offset? and How can I determine a web user’s time zone?
Try to use geolocation. That is, your server side script fires off a request to one of the many geolocator services out there on the user's first page visit to try and guess where the user is. The downside of this is that it will be wrong about 10% of the time, so it's not that much better than the market share Javascript is going to get you.... (all in all, then, not a very good method...)
Ask the user! You will see that most websites that want to display a tailored experience for you will ask you this sort of thing because it's just not possible to know. As a neat fallback, you could wrap the question around <noscript> tags so you only ask those with Javascript disabled while offering the Javascript experience to those that have it.
Dojo has some pretty good localizations for dates and currencies. Using this method also allows you to pick different formats (e.g.: short date vs long date) and force locales.
The language and the user's locale should be sent on the HTTP header. You can use those to create the correct date format server-side to be displayed to the user. However, this is often undesirable because many users completely ignore their locale settings in their OS and/or browser. So, you may be feeding USA style timestamps to New Zealanders.
I liked the trick posted in the comment above, but it sounds like a QA headache, since you could be dealing with a large number of clients that implement timestamps in very different ways.
The most effective solution I have seen, is to simple provide a panel to allow your users to choose what time format they like. Some users even ****gasp**** like ISO formats. Then you do the time format conversion server side. If your application language does not have good locale to timezone formatting mapping, check your database. Many databases provide locale-based customized timezone formatting as well.
Because this anwser still popups in google I share that this is now possible to do by using a readonly datetime-local input (see below) and you can then style the input the way you want:
<input type="datetime-local" value="2018-06-12T19:30" readonly />
For more information see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local