Monday, January 18, 2021

isLeapYear in PHP

// Returns TRUE if a year is a leap year
// The year must be evenly divisible by 4
// if the year can be evenly divisible by 100 is not a leap year
// unless it is also evenly divisible by 400 then it is a leap year
// https://en.wikipedia.org/wiki/Leap_year
public static function isLeapYear(int $year):bool {
    return (($year % 4 == 0) && (($year % 100 != 0) || ($year % 400 == 0)));
}

 

Friday, June 12, 2020

VBA Excel module for reading INI files.

Attribute VB_Name = "ReadINIFile"
Option Explicit

' Retrieves a string from the specified section in an initialization file.
' See here: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring?redirectedfrom=MSDN
' lpApplicationName     The name of the section containing the key name.
' lpKeyName             The name of the key whose associated string is to be retrieved.
' lpDefault             A default string.
' lpReturnedString      A pointer to the buffer that receives the retrieved string.
' nSize                 The size of the buffer pointed to by the lpReturnedString parameter, in characters.
' lpFileName            The name of the initialization file.

Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" ( _
    ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, _
    ByVal lpDefault As String, _
    ByVal lpReturnedString As String, _
    ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

' Given a key it returns the associated value from an INI file.
' iniFileName should specify a valid path.
' jesus.aneiros@gmail.com
' 2020.06.11
Public Function readIniFileString(ByVal iniFileName As String, ByVal sectionName As String, ByVal keyName As String) As String

    Dim lResult As Long
    Dim retString As String * 255
    Dim retStringSize As Long

    ' The buffer
    retString = Space(255)
       
    ' Returns the number of caracters copied to the buffer retString
    lResult = GetPrivateProfileString(sectionName, keyName, "", retString, Len(retString), iniFileName)
       
    If (lResult) Then
        readIniFileString = Left$(retString, lResult)
    Else
        readIniFileString = ""
    End If

End Function

Public Sub test()
    Debug.Print readIniFileString(ThisWorkbook.Path & "\model_c.ini", "DB", "host")
    Debug.Print readIniFileString(ThisWorkbook.Path & "\model_c.ini", "EMAIL", "host")
End Sub

Thursday, June 11, 2020

VBA Excel copy sheets from closed workbook

Option Explicit

' Copies sheets ranges (values and formats) from a closed workbook into
' a sheet on the opened workbook.
Private Sub copySheets(fileName As String, sheetName As String, dstSheetName As String, rng As String)

    Sheets(dstSheetName).Cells.Clear
   
    Dim srcWB As Workbook
   
    ' Open the source workbook and copy the values
    Set srcWB = Workbooks.Open(fileName)

    srcWB.Sheets(sheetName).Range(rng).Copy

    ThisWorkbook.Activate
   
    ' Paste values and formats
    With Sheets(dstSheetName)
        .Range(rng).PasteSpecial Paste:=xlPasteFormats
        .Range(rng).PasteSpecial Paste:=xlPasteColumnWidths
        .Range(rng).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    End With
   
    ' Get out of the copy mode
    Application.CutCopyMode = False
   
    ' Close the source workbook without saving
    srcWB.Close savechanges:=False

End Sub



' An example of use

Public Sub diff()
    Dim fileName As String
   
    ' Copy 2 sheets from 2 different files
   
    ' The first file
    ' Get the file names from a cell
    fileName = Sheets("Main").Range("B1").Value
    copySheets fileName, "Summary", "Summary", "A1:M26"
    copySheets fileName, "Day Positions", "DayPositions", "A1:N32"
   
    ' The second file
    fileName = Sheets("Main").Range("B2").Value
    copySheets fileName, "Summary", "SummaryNew", "A1:M26"
    copySheets fileName, "Day Positions", "DayPositionsNew", "A1:N32"
   
    ThisWorkbook.Sheets("Diff").Activate
    ThisWorkbook.Save
   
End Sub

Saturday, April 4, 2020

Monday, January 13, 2020

2020 Holiday Schedule

DateHoliday
Wednesday, January 1 New Year’s Day
Monday, January 20 Birthday of Martin Luther King, Jr.
Monday, February 17* Washington’s Birthday
Monday, May 25 Memorial Day
Friday, July 3** Independence Day
Monday, September 7 Labor Day
Monday, October 12 Columbus Day
Wednesday, November 11 Veterans Day
Thursday, November 26 Thanksgiving Day
Friday, December 25 Christmas Day

*This holiday is designated as "Washington’s Birthday" in section 6103(a) of title 5 of the United States Code, which is the law that specifies holidays for Federal employees. Though other institutions such as state and local governments and private businesses may use other names, it is our policy to always refer to holidays by the names designated in the law.

**July 4, 2020 (the legal public holiday for Independence Day), falls on a Saturday. For most Federal employees, Friday, July 3, will be treated as a holiday for pay and leave purposes. (See 5 U.S.C. 6103(b).)

Friday, May 31, 2019

GNU date and relatives dates

I was writing a simple script to delete some log files, all the log filenames contain the date of creation in the form YYYYMMDD and I needed to delete the files from previous month keeping the current month. The script will run on the last day of every month.

I thought the task will be very easy as I could use the GNU date command to build the date string, something like:

# The year
date +%Y

Use the date relative option for getting the previous month, something like:

date --date="-1 month" +%m

Then concatenate both strings and build a file pattern using wildcards like ? and *.

The system date was 2019-05-31 ahd I was expecting the 04 string as the output, instead 05 was printed.

I tried several variants and all printed the same, 05:

$ date --date="-1 month" +%m
05

$ date --date="last month" +%m
05

I DuckDuckGoed the Internet and found this answer:

https://stackoverflow.com/questions/13168463/using-date-command-to-get-previous-current-and-next-month

This is from the coreutils manual online:

https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings

$ date --date="$(date +%Y%m15) -1 month" +%m
04

Wednesday, May 8, 2019

PDFs don't open automatically in Chrome

You can normally open PDFs automatically in Chrome by clicking on the file you want to see. If your PDFs are downloading instead of opening automatically in Chrome, Chrome PDF viewer could be turned off.

Open PDFs in Chrome

  1. On your computer, open Chrome.
  2. At the top right, click More More and then Settings.
  3. At the bottom, click Advanced.
  4. Under "Privacy and security," click Site settings.
  5. Near the bottom, click PDF documents.
  6. Turn off Download PDF files instead of automatically opening them in Chrome.
Chrome will now open PDFs automatically when you click them.