Skip to content

Instantly share code, notes, and snippets.

@aadeshere1
Last active August 7, 2018 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadeshere1/91d7fe5829f328757dd723f2d0389e4c to your computer and use it in GitHub Desktop.
Save aadeshere1/91d7fe5829f328757dd723f2d0389e4c to your computer and use it in GitHub Desktop.
Converts AD to BS in batch using hamropatro.com date converter API endpoint.
# HamroPatro might change it's api endopint
require 'rest-client'
require 'loofah'
require 'loofah/helpers'
eng_date = File.readlines('nepdate.txt')
mahina = {"बैशाख" => 1, "जेठ" => 2, "आषाढ" => 3, "श्रावण" => 4,"भाद्र" => 5, "आश्विन" => 6, "कार्तिक" => 7, "मंसिर" => 8, "पौष" => 9, "माघ" => 10, "फाल्गुण" => 11, "चैत्र" => 12}
mahinaharu = Regexp.new(mahina.keys.map {|x| Regexp.escape(x) }.join('|'))
anka = {"०" => 0, "१" => 1, "२" => 2, "३" => 3, "४" => 4, "५" => 5, "६" => 6, "७" => 7, "८" => 8, "९" => 9}
File.open("nepali_date.txt", 'w') do |file|
eng_date.each do |date|
resp = RestClient.post 'http://www.hamropatro.com/getMethod.php', {actionName: 'dconverter',
datefield: "#{date}",
convert_option: 'eng_to_nep',
state: '0.9944881830164107'}
converted_date = resp.split("|")[1][1..-1]
scrubbed_date = Loofah.fragment(converted_date).scrub!(:strip).to_text
nep_converted = scrubbed_date.split(" ")
nep_year = nep_converted[0]
nep_month = nep_converted[1]
nep_day = nep_converted[2]
puts "#{nep_year.gsub(/[०१२३४५६७८९]/, anka)}-#{nep_month.gsub(mahinaharu, mahina)}-#{nep_day.gsub(/[०१२३४५६७८९]/, anka)}"
file.write("#{nep_year.gsub(/[०१२३४५६७८९]/, anka)}-#{nep_month.gsub(mahinaharu, mahina)}-#{nep_day.gsub(/[०१२३४५६७८९]/, anka)}\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment