How can I convert unix time to Hindu calendarWikipedia time and the other way round in php, Perl or Python or Java? I know I can convert to Hebrew and Jewish. But Hindu is not an option.
To be more specific, I'm talking about the Hindu lunar calendar. The following website is working and does exactly what I want: http://web.meson.org/calendars/. For example, it 'translates' 28-1-2012 (Gregorian) to 5-11-2068 (Hind. Lun.). How can I accomplish the same task? And if there are absolutely no scrips out there, how can I write it myself?
Did you check DateTime-Indic-0.1 family of modules? At least DateTime::Indic::Chandramana
seems to have a method to convert traditional date into UTC values (utc_rd_values).
UPDATE:
I suppose Calendar::Saka may be useful as well for many users (as I have known, it's the Indian national calendar), in particular, to_gregorian() and from_gregorian() methods.
For Python, use calendar2 (note: this is not the built-in calendar module).
Sample use:
>>> from calendar2 import *
>>> old_hindu_solar_from_absolute(absolute_from_gregorian(3,1,2012))
(11, 16, 5112)
>>> old_hindu_lunar_from_absolute(absolute_from_gregorian(3,1,2012))
(12, 0, 8, 5112)
Paper: Indian Calendrical Calculations
Provides Common Lisp code in the appendix.
While a Python (or other language) solution could be written according to the paper, the authors enumerate the Indian calendar rules pretty well, so it's a pretty solid paper if you're willing to consider taking the provided Common Lisp code.
Seems to be a difficult task. According to this discussion at bytes.com there is no clear way to accomplish a 100% correct conversion. But it seems that they are wrong when they assume that the Hindu calendar has only 364 days instead of 365 (or 366 in leap years).
Here you can find a good conversion table including the handling of leap years: http://hinduism.about.com/od/basics/a/monthsdayseras.htm
If it is as easy as written there you can try something like this (php code):
<?php
function convertDateToHinduDate($date) {
$beginningDayOfMonth = array(
1 => 21,
2 => 20,
3 => 22 + (dateIsLeapYear($date) ? -1 : 0), /* 21 in leap years */
4 => 21,
5 => 22,
6 => 22,
7 => 23,
8 => 23,
9 => 23,
10 => 23,
11 => 22,
12 => 22,
);
$daysOfHinduMonth = array(
1 => 30 + (dateIsLeapYear($date) ? 1 : 0), /* 31 in leap years */
2 => 31,
3 => 31,
4 => 31,
5 => 31,
6 => 31,
7 => 30,
8 => 30,
9 => 30,
10 => 30,
11 => 30,
12 => 30,
);
$day = (int) date('d', strtotime($date));
$month = (int) date('m', strtotime($date));
$year = (int) date('Y', strtotime($date));
$monthBefore = $day < $beginningDayOfMonth[$month];
$yearBefore = $month < 3 || ($month == 3 && $day < $beginningDayOfMonth[3]);
$newYear = $year + 57 + ($yearBefore ? -1 : 0);
$newMonth = $month - 2 + ($monthBefore ? -1 : 0);
if($newMonth < 1) $newMonth = 12 + $newMonth;
$newDay = $day - $beginningDayOfMonth[$month];
if($newDay < 1) $newDay = $daysOfHinduMonth[$newMonth] + $newDay;
return date("d-m-Y", mktime(11, 59, 0, $newMonth, $newDay, $newYear));
}
function dateIsLeapYear($date) {
return date('L', strtotime($date));
}
$date = date("d-m-Y", strtotime('2012-01-28'));
echo 'Date: ', $date, ' (is leap year: ', dateIsLeapYear($date) ? 'yes' : 'no', ')<br />';
echo 'Converted Hindu date: ', convertDateToHinduDate($date);
?>
Output of this code:
Date: 28-01-2012 (is leap year: yes)
Converted Hindu date: 07-11-2068
But according to the calculator of this Java applet, it should be 05-11-2068 instead of 07-11-2068. So, there are still some conversion rules missing. Maybe you can give me some more information so that i can correct the code above.
I dont know whether it is correct approach or not but
Please go to http://calendarhome.com/converter/ site and download two javascript files astro.js and calendar.js and follow onchange events of Gregorian Date and fetch Indian Civil Date parameters.
Related
I have test:
#Test
public void sixTest() {
FlowerList flowerList = new FlowerList();
Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
Response response = given()
.when()
.get("/api/unknown")
.then()
.log().all()
.body("data.year", hasKey("2001"))
.extract().response().as((Type) FlowerList.class);
I need to check that at least one year field contained the value 2001. But I get an exception:
Expected: map containing ["2001,"->ANYTHING]
Actual: [2000, 2001, 2002, 2003, 2004, 2005]
What am I doing wrong? In theory hasKey should return a single value 2001
get:
{
page: 1,
per_page: 6,
total: 12,
total_pages: 2,
data: [
{
id: 1,
name: "cerulean",
year: 2000,
color: "#98B2D1",
pantone_value: "15-4020"
},
{
id: 2,
name: "fuchsia rose",
year: 2001,
color: "#C74375",
pantone_value: "17-2031"
},
...
The error message give you the answers
Expected: map containing ["2001,"->ANYTHING]
Actual: [2000, 2001, 2002, 2003, 2004, 2005]
The actual result is an array, instead of a map. hence
Matchers.hasItems (match if Iterable has items)
should be used instead of
Matchers.hasKeys (match if the Map has key)
Another problem is, since year is Integer, 2001 should be the expected value instead of "2001".
So the assertion should be
.body("data.year", hasItems(2001))
I get a pca-model
> library(sparklyr)
> library(dplyr)
> sc <- spark_connect("local", version="2.0.0")
> iris_tbl <- copy_to(sc, iris, "iris", overwrite = TRUE)
The following columns have been renamed:
- 'Sepal.Length' => 'Sepal_Length' (#1)
- 'Sepal.Width' => 'Sepal_Width' (#2)
- 'Petal.Length' => 'Petal_Length' (#3)
- 'Petal.Width' => 'Petal_Width' (#4)
> pca_model <- tbl(sc, "iris") %>%
+ select(-Species) %>%
+ ml_pca()
> print(pca_model)
Explained variance:
PC1 PC2 PC3 PC4
0.924618723 0.053066483 0.017102610 0.005212184
Rotation:
PC1 PC2 PC3 PC4
Sepal_Length -0.36138659 -0.65658877 0.58202985 0.3154872
Sepal_Width 0.08452251 -0.73016143 -0.59791083 -0.3197231
Petal_Length -0.85667061 0.17337266 -0.07623608 -0.4798390
Petal_Width -0.35828920 0.07548102 -0.54583143 0.7536574
But can`t use the resulting model to forecast.
sdf_predict(pca_model)
Source: query [?? x 6]
Database: spark connection master=local[4] app=sparklyr local=TRUE
Ends with an error
java.lang.IllegalArgumentException: requirement failed:
The columns of A don't match the number of elements of x. A: 4, x: 0
Inserting data for the forecast does not help
sdf_predict(pca_model, tbl(sc, "iris") %>% select(-Species))
Source: query [?? x 5]
Database: spark connection master=local[4] app=sparklyr local=TRUE
Ends with an error
java.lang.IllegalArgumentException: requirement failed:
The columns of A don't match the number of elements of x. A: 4, x: 0
It is generally possible to use PCA to predict in spark?
Instead of sdf_predict, use sdf_project.
> pca_projected <- sdf_project(pca_model, tbl(sc, "iris") %>% select(-Species),
+ features=rownames(pca_model$components))
> pca_projected %>% collect %>% head
# A tibble: 6 x 8
Sepal_Length Sepal_Width Petal_Length Petal_Width PC1 PC2 PC3 PC4
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 5.10 3.50 1.40 0.200 -2.82 -5.65 0.660 -0.0311
2 4.90 3.00 1.40 0.200 -2.79 -5.15 0.842 0.0657
3 4.70 3.20 1.30 0.200 -2.61 -5.18 0.614 -0.0134
4 4.60 3.10 1.50 0.200 -2.76 -5.01 0.600 -0.109
5 5.00 3.60 1.40 0.200 -2.77 -5.65 0.542 -0.0946
6 5.40 3.90 1.70 0.400 -3.22 -6.07 0.463 -0.0576
I try to create mpegts presentation timestamp. It is 5 bytes length. I found solution in source code of VLC player. It's looks like this (in C lang code):
bits_write( &bits, 4, i_pts_dts ); // '0010' or '0011'
bits_write( &bits, 3, i_pts >> 30 );
bits_write( &bits, 1, 0x01 ); // marker
bits_write( &bits, 15, i_pts >> 15 );
bits_write( &bits, 1, 0x01 ); // marker
bits_write( &bits, 15, i_pts );
bits_write( &bits, 1, 0x01 ); // marker
i_header_size -= 0x5;
That means I must to collect 5 bytes from 40 bits.
For example, I need to 5 bytes from 2350 decimal number. Binary view:
1001 0010 1110
After VLC manipulation I must have this binary view:
0010 000 1 000000000000000 1 000100100101110 1
Hex view:
21 00 01 12 5D
How can I do it in Java?
Also I found Java-solution on GitHub: https://github.com/taktod/myLib/blob/master/myLib.MIT/myLib.container.mpegts/src/main/java/com/ttProject/container/mpegts/field/PtsField.java
But this realization is too difficult. For a one-time operation it is necessary to create too many helper classes like Bit1, Bit2, Bit3, etc...
This is simple bit-manipulation:
int dts = 2; // must be 2 or 3
long pts = 2350; // must be less than 8,589,934,592
byte[] output = new byte[] {
(byte) (dts << 4 | pts >> 30 | 1),
(byte) (pts >> 22),
(byte) (pts >> 15 | 1),
(byte) (pts >> 7),
(byte) (pts << 1 | 1)
};
for (byte b : output)
System.out.printf("%02x ", b); // prints: 21 00 01 12 5d
I have following RDD in my Java Code.
(1, List(1596, 1617, 1929, 2399, 2674))
(2, List(1702, 1785, 1933, 2054, 2583, 2913))
(3, List(1982, 2002, 2048, 2341, 2666))
What I am trying to do is to create another RDD. The contents should look like this.(not necessarily in same order)
1596
1617
1929
2399
2674
1702
1785
1933
2054
2583
2913
1982
2002
2048
2341
2666
I am not sure how do transform one RDD (JavaRDD<ArrayList<String>>) with collection of Objects to single RDD (JavaRDD<String>) with all objects in it. I would highly appreciate if anyone could point me to some JAVA resource.
You can do the same in scala as follows
val data = List((1, List(1596, 1617, 1929, 2399, 2674)),
(2, List(1702, 1785, 1933, 2054, 2583, 2913)),
(3, List(1982, 2002, 2048, 2341, 2666)))
val rdd_data = sc.parallelize(data)
val rdd_flattened = rdd_data.flatMap((index, value) => value)
When I run my program, I get this:
run:
Heat Index: Key West, Florida
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
____________________________________________________________________________________________________________________________________
Temperature: 70.3 70.8 73.8 77.0 80.7 83.4 84.5 84.4 83.4 80.2 76.3 72.0
Humidity: 69.0 67.0 66.0 64.0 66.0 69.0 67.0 67.0 70.0 69.0 69.0 70.0BUILD SUCCESSFUL (total time: 0 seconds)
I want to display this so that the months align with each column of numbers, like Jan aligns with the first column of numbers and so on. I know that there are 7 blank spaces between each column of number if the months are aligned. The thing is, I don't know how to create blank spaces, like blank spaces so the months won't show up on top of the Temperature: heading, with printf. Any help will be greatly appreciated.
You need to use System.out.printf() and passing the appropriate flags for the width, see here for the syntax for formatting.
String[] months =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
Double[] temper =
{70.3, 70.8, 73.8, 77.0, 80.7, 83.4, 84.5, 84.4, 83.4, 80.2, 76.3, 72.0};
Double[] humid =
{69.0, 67.0, 66.0, 64.0, 66.0, 69.0, 67.0, 67.0, 70.0, 69.0, 69.0, 70.0};
System.out.printf(" %7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s%7s\n",
(Object[])months);
System.out.printf("________________________________________"
+ "________________________________________________________\n");
System.out.printf("Temperature:%7.1f%7.1f%7.1f%7.1f%7.1f"
+ "%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n",
(Object[])temper);
System.out.printf(" Humidity:%7.1f%7.1f%7.1f%7.1f%7.1f"
+ "%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f\n",
(Object[])humid);
try this.. you can specify spaces between each print..
String[] day = {"Sun", "Mon", "Tues", "Friday","Saturday"};
for(String s :day){
System.out.printf("%15s", s);
}
This will print exactly four spaces plus the three taken up by the month. Also, if you just want to print Strings you don't really need printf. Below are two options.
System.out.printf(" %s", month);
or
System.out.print(" " + month);
I would also recommend coming up with a way to do this without hard coding the spaces. Maybe set up a loop to print spaces which can change the width.
try this
System.out.println("\n");