Temos Archyvai: informacinės technologijos

Apie linux ir kitas operacines sistemas, techninę ir programinę įrangą

PHP function to check if a date is a holiday in Lithuania

Here is a simple function for PHP to check if a date, given in standard ISO format (Y-m-d) is a public holiday in Lithuania. I needed one for a project, so here it is for everyone:

 
/**
 * function checks if a passed date is a public holiday 
 * (or weekend) in Lithuania
 * passed values:
 * $weekends - true if you want the function to include weekends
 * default - false
 * $date - 'Y-m-d' format date (or full ISO 8601 datetime string)
 * defaults to current date
 * returns 1 (holiday) or 0 (not a holiday) or error on wrong input
*/
 
function is_lt_holiday(bool $weekends = false, string $date = null): int
{
    //date is today if it was not passed to the function
    if (!isset($date)) $date = date('Y-m-d');
 
    //check if the date is valid
    if (
         strlen($date) < 10 
      || !checkdate(substr($date,5,2), substr($date,8,2), substr($date,0,4))
    ) 
    {
        trigger_error('You shoud pass a valid date in \'Y-m-d\' format');
        return 0; // or should it be null to trigger additional error?
    } 
 
    $year = substr($date,0,4);
    $mday = substr($date,5,5);
    $easter = date('Y-m-d',easter_date($year));
    $easter2 = date('Y-m-d', strtotime($easter . ' +1 day'));
    $public_holidays = [
        '01-01', //new year
        '02-16', //independence day
        '03-11', //restoration of independence day
        substr($easter,5,5), //easter day1
        substr($easter2,5,5), //easter day2
        '05-01', //int worker's day
        '06-24', //summer solstice
        '07-06', //state day
        '08-15', //mary's assumption
        '11-01', //all saints day
        '11-02', //day of the dead
        '12-24', //christmas eve
        '12-25', // christmas day1
        '12-26'  //christmas day2
       ];
 
    if (in_array($mday,$public_holidays)) {
        return 1;
    }
    elseif (
       $weekends == true 
       && (date('D',strtotime($date)) == 'Sat' 
       || date('D',strtotime($date)) == 'Sun')
    ) 
    {
        return 1;
    }
    return 0;
}
 
 
// and now let's test it:
 
echo is_lt_holiday(true);
//today; it depends :)
echo is_lt_holiday(false,'2022-01-01');
//returns 1
echo is_lt_holiday(false,'2022-01-02');
//returns 0
echo is_lt_holiday(false,'2022-01-32');
//returns 0 and error notice - an impossible date

Downloading Manga Comics Using a Bash Script

My teenage daughter got immersed into manga, that is, Japanese comics books. That is not something that I would enjoy doing myself (never was much of a fan of comics books), but since she asked me to download the comics for offline reading, it became a father-and-daughter thing for us. So I decided to show her some Linux command line magic. After some tinkering I wrote a bash script that can download manga books from many popular manga sites to read offline, on an ebook reader. Toliau skaityti Downloading Manga Comics Using a Bash Script

Kaip nusipirkau suklastotą planšetę (ir kaip pavyko jos atsikratyti)

Prieš tris savaites pirkau dukrai planšetinį kompiuterį. Deja, pataikiau ant klastotės. Ypatingai buvo apmaudu ir nesmagu, nes kas jau kas, o aš turėjau suprasti iškart, kad su tuo kompiuteriu kažkas ne taip. Tai, kad to nesupratau laiku, kainavo man dvi savaites apmaudo.

Štai skelbimas „Išpardavimas Tik 149€ 64gb 4gb RAM 10 branduolių“, kuriame reklamuotą kompiuterį pirkau:

Skelbime nurodyti tokie pagrindiniai planšetės parametrai:

Linux audio: individualizuotas sprendimas

Su garso problemomis nuolat tenka susidurti savo prižiūrimuose Linux kompiuteriuose. Pirmą kartą čia apie tai rašiau dar 2010 metais Tai garso nėra, tai jis siunčiamas ne į tą įrenginį, į kurį norėčiau, tai nustatomas ne tas audio profilis… Šių problemų esmė – labai bendro pobūdžio, konkrečiai aparatinei įrangai nepritaikyti Linux garso posistemės Pulse Audio sprendimai. (Tiesa, ir prie Windows garso galima gerokai pavargti, tačiau šį kartą kalba ne apie Windows).

Linux, ir konkrečiau – mano naudojama darbastalio aplinka KDE, turi gerus audio tvarkymo įrankius, tačiau juose susigaudyti ne specialistui – ne taip paprasta. Štai, pvz., pasirinkite – kuris audio profilis jums reikalingas šiame paveikslėlyje?

Iš viso didžiulio sąrašo man tenka naudoti viso labo du profilius (dažniausiai – „Analoginė dvipusė stereo“, o prijungus išorinį televizorių – „Digital Stereo (HDMI) išvestis + Analoginė stereo įsvestis), tačiau tai teko išmokti ilgu bandymų ir klaidų keliu. Toliau skaityti Linux audio: individualizuotas sprendimas

Codeigniter form with integrated data and file validation

In one application built on Codeigniter framework I needed to create a special form. The form had to allow a user to both enter text data and upload an image. It was actually the first time I had any experience with programming forms that would include file upload. And I found out it is not a completely trivial task in Codeigniter. There are multiple reasons for this, but the main one is this: image validation and form data validation are done separately in Codeigniter. Not only the validation is done by two different classes, but the error messages are also independent.

So I went on to create a form that would integrate both types of validation (or rather, the error messages). On the way I had to decide on one major usability problem: what to do with image if the user submits the right image but the rest of the form does not validate (for example, forgets to enter name or email). I decided to go with the simplest option – the image is discarded, and the user is warned to not forget to attach the image again (since the file upload field cannot be repopulated, as other fields can be).

Admittedly, the image could be saved to save user the trouble of attaching it again, but in that case you have to solve another problem – what to do with the images that the user (a malicious user) submits and subsequently fails to submit the properly filled in form. I know, something could be done at the browser level using Javascript validation, but the server-side validation is still necessary… Toliau skaityti Codeigniter form with integrated data and file validation

Kaip „keitimų sekimo“ žymes Word dokumente paversti paprastai formatuotu „lyginamuoju variantu“ – su makrokomanda

Prieš keletą metų tinklaraštyje publikavau įrašą apie tai, kaip Microsoft Word dokumentą su įjungtu keitimų paversti teisingu dokumento „lyginamuoju variantu“ – tokiu, kokie daromi rengiant teisės aktų pakeitimus. Mano aprašytas kelias buvo sąlyginai sudėtingas – Word dokumentą reikėjo įrašyti kaip HTML failą, tada atlikti jame paiešką ir keitimus, tada dokumentą vėl atverti su Word, ir galiausiai tekdavo kažkiek taisyti formatavimą. Paprasčiau, nei ilgame dokumente skrupulingai įrašinėti tekstą, jį ryškinti ir braukyti, bet visgi.

Bet štai neseniai radau būdą, kaip Word arba LibreOffice makrokomandos pagalba tai galima padaryti keliais pelės spragtelėjimais, neužsiimant visom sudėtingom manipuliacijom. Toliau skaityti Kaip „keitimų sekimo“ žymes Word dokumente paversti paprastai formatuotu „lyginamuoju variantu“ – su makrokomanda