Python datetime.date.strftime() Examples

The following are 21 code examples of datetime.date.strftime(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module datetime.date , or try the search function .
Example #1
Source File: detector.py    From pipeline with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_means(means, date):
    """Save means atomically. Protocol 4
    """
    tstamp = date.strftime(".%Y-%m-%d") if date else ""
    pf = conf.pickledir / f"means{tstamp}.pkl"
    pft = pf.with_suffix(".tmp")
    if not means:
        log.error("No means to save")
        return
    log.info("Saving %d means to %s", len(means), pf)
    latest = max(m[0] for m in means.values())
    log.info("Latest mean: %s", latest)
    with pft.open("wb") as f:
        pickle.dump(means, f, protocol=4)
    pft.rename(pf)
    log.info("Saving completed")


# FIXME: for performance reasons we want to minimize heavy DB queries.
# Means are cached in a pickle file to allow restarts and we pick up where
# we stopped based on measurement_start_time. Yet the timestamp are untrusted
# as they are generated by the probes. 
Example #2
Source File: shodanseeker.py    From shodan-seeker with GNU General Public License v3.0 6 votes vote down vote up
def log(self, action="", data=""):
        date = datetime.now()
        if action == "Error":
            msg = ('[Error] ') + date.strftime("%c") + ' - ' + str(data)
        elif action == "newline":
            msg = "\n"
        else:
            msg = ('[ Log ] ') + date.strftime("%c") + ' - ' + str(action) + str(data)
        filelogname = date.strftime("%m-%Y")
        for logpath in paths:
            self.logpath = paths['logpath']
        filelogpath = str(self.logpath) + filelogname + ".txt"
        if os.path.isfile(filelogpath):
            file = open(filelogpath, "a")
            file.write(msg + '\n')
            file.close()
        else:
            file = open(filelogpath, "w")
            file.write(msg + '\n')
            file.close()
        if action != "newline":
            print msg  # Console output 
Example #3
Source File: test_custom_scalars.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def serialize_date(date):
    return date.strftime("%Y-%m-%d") 
Example #4
Source File: esCacheMaint.py    From MozDef with Mozilla Public License 2.0 5 votes vote down vote up
def clearESCache():
    es = esConnect(None)
    indexes = es.get_open_indices()
    # assums index names  like events-YYYYMMDD etc.
    # used to avoid operating on current indexes
    dtNow = datetime.utcnow()
    indexSuffix = date.strftime(dtNow, '%Y%m%d')
    previousSuffix = date.strftime(dtNow - timedelta(days=1), '%Y%m%d')
    for targetindex in sorted(indexes):
        if indexSuffix not in targetindex and previousSuffix not in targetindex:
            url = '{0}/{1}/_stats'.format(random.choice(options.esservers), targetindex)
            r = requests.get(url)
            if r.status_code == 200:
                indexstats = json.loads(r.text)
                if indexstats['_all']['total']['search']['query_current'] == 0:
                    fielddata = indexstats['_all']['total']['fielddata']['memory_size_in_bytes']
                    if fielddata > 0:
                        logger.info('target: {0}: field data {1}'.format(targetindex, indexstats['_all']['total']['fielddata']['memory_size_in_bytes']))
                        clearurl = '{0}/{1}/_cache/clear'.format(random.choice(options.esservers), targetindex)
                        clearRequest = requests.post(clearurl)
                        logger.info(clearRequest.text)
                        # stop at one?
                        if options.conservative:
                            return
                else:
                    logger.debug('{0}: <ignoring due to current search > field data {1}'.format(targetindex, indexstats['_all']['total']['fielddata']['memory_size_in_bytes']))
            else:
                logger.error('{0} returned {1}'.format(url, r.status_code)) 
Example #5
Source File: pruneIndexes.py    From MozDef with Mozilla Public License 2.0 5 votes vote down vote up
def esPruneIndexes():
    logger.debug('started')
    try:
        es = ElasticsearchClient((list('{0}'.format(s) for s in options.esservers)))
        indices = es.get_indices()
        # do the pruning
        for (index, dobackup, rotation, pruning) in zip(options.indices, options.dobackup, options.rotation, options.pruning):
            try:
                if pruning != '0':
                    index_to_prune = index
                    if rotation == 'daily':
                        idate = date.strftime(toUTC(datetime.now()) - timedelta(days=int(pruning)), '%Y%m%d')
                        index_to_prune += '-%s' % idate
                    elif rotation == 'monthly':
                        idate = date.strftime(datetime.utcnow() - timedelta(days=31 * int(pruning)), '%Y%m')
                        index_to_prune += '-%s' % idate

                    if index_to_prune in indices:
                        logger.debug('Deleting index: %s' % index_to_prune)
                        es.delete_index(index_to_prune, True)
                    else:
                        logger.error('Error deleting index %s, index missing' % index_to_prune)
            except Exception as e:
                logger.error("Unhandled exception while deleting %s, terminating: %r" % (index_to_prune, e))

    except Exception as e:
        logger.error("Unhandled exception, terminating: %r" % e) 
Example #6
Source File: record.py    From WeatherCollector with MIT License 5 votes vote down vote up
def export_in_bigquerry(self):
        access_key = path.realpath("../../access_key.json")
        bigquery_client = bigquery.Client.from_service_account_json(access_key)
        dataset_ref = bigquery_client.dataset("weather")
        table_ref = bigquery_client.get_table(dataset_ref.table("noaa"))

        data = []
        for date, datum in self.data:
            entry = {
                "city": self.name,
                "date": date.strftime("%Y-%m-%d")
            }

            for attribute in Record.attributes:
                if not attribute in datum:
                    raise Exception("Corrupted record data")

                if datum[attribute] is None:
                    entry[attribute] = None 
                else:
                    entry[attribute] = "%.3f" % datum[attribute]
            
            data.append(entry)

        result = bigquery_client.create_rows(table_ref, data)
        if  result != []:
            print("Error storing weather")
            print(result)
        else:
            print("Storage successful") 
Example #7
Source File: record.py    From WeatherCollector with MIT License 5 votes vote down vote up
def export_as_csv(self, filepath):
        just_size = 11
        csvfile = open(filepath, 'w')
        
        # Write the header
        csvfile.write(self.collection_info + "\n")
        csvfile.write("DATE,".ljust(just_size))
        for attribute in Record.attributes:
            attribute = attribute.upper() + ("," if attribute != Record.attributes[-1] else "") 
            csvfile.write(attribute.ljust(just_size))
        csvfile.write("\n")
        
        # Write each row
        for date, datum in self.data:
            csvfile.write((date.strftime(Record.date_format)+ ", ").ljust(just_size))
            
            for attribute in Record.attributes:
                if not attribute in datum:
                    raise Exception('Corrupted record data')
                
                if datum[attribute] is None:
                    text = "NA"
                else:
                    text = "%.3f" % datum[attribute]
                    
                if attribute != Record.attributes[-1]:
                    text += ", "
                    
                csvfile.write(text.ljust(just_size))
            csvfile.write("\n")
            
        print("\nSuccesfully written " + filepath + "!")
        csvfile.close() 
Example #8
Source File: shodanseeker.py    From shodan-seeker with GNU General Public License v3.0 5 votes vote down vote up
def add_scanid(self, id):
        for scanidpath in paths:
            self.scanidpath = paths['scanidpath'] + "scanID.txt"
        date = datetime.now()
        if os.path.isfile(self.scanidpath):
            file = open(self.scanidpath, "a")
            file.write(id + '\t' + date.strftime("%c") + '\n')
            file.close()
        else:
            file = open(self.scanidpath, "w")
            file.write('    Scan ID              Date      \n')
            file.write(id + '\t' + date.strftime("%c") + '\n')
            file.close() 
Example #9
Source File: datelib.py    From whatsapp-parser with MIT License 5 votes vote down vote up
def date_to_weekday(date_str):
    if date_str.count("/") > 0:
        day, month, year = date_str.split("/")
        parsed_date = "%s-%s-%s" % (year, month, day)
        date_str = parsed_date

    # if year format is: 2012, 2000
    if len(year) == 4:
        return time.strftime("%A", time.strptime(date_str, "%Y-%d-%m"))
    # if year format is: 12, 00 
    return time.strftime("%A", time.strptime(date_str, "%y-%d-%m")) 
Example #10
Source File: datelib.py    From whatsapp-parser with MIT License 5 votes vote down vote up
def date_to_str(date_str):
    return date.strftime('%Y-%m-%d') 
Example #11
Source File: storage.py    From indico-plugins with MIT License 5 votes vote down vote up
def _replace_bucket_placeholders(self, name, date):
        name = name.replace('<year>', date.strftime('%Y'))
        name = name.replace('<month>', date.strftime('%m'))
        name = name.replace('<week>', date.strftime('%W'))
        return name 
Example #12
Source File: StatusUpdateReporter.py    From cms with GNU General Public License v3.0 5 votes vote down vote up
def generateDailyReport(self):
        date = self.date
        thread = self.thread
        updates = Message.objects.filter(date=date, thread=thread).order_by('timestamp')
        first = UserProfile.objects.get(user=updates[0].member)
        last = UserProfile.objects.get(user=list(reversed(updates))[0].member)
        try:
            log = DailyLog.objects.get(date=date, thread=thread)
            allowKick = Thread.objects.get(name=thread).allowBotToKick

            totalMembers = log.members.count()
            didNotSendCount = log.didNotSend.count()
            invalidUpdatesCount = log.invalidUpdates.count()
            sendCount = totalMembers - (didNotSendCount + invalidUpdatesCount)

            message = '<b>Daily Status Update Report</b> \n\n &#128197; ' + date.strftime(
                '%d %B %Y') + ' | &#128228; ' + str(sendCount) + '/' + str(totalMembers) + ' Members'

            message += '\n\n<b>' + self.getPercentageSummary(sendCount, totalMembers) + '</b>'
            message += self.getInvalidUpdatesReport(log.invalidUpdates)
            if allowKick:
                message += self.getKickMembersReport(self.membersToBeKicked)
            if updates.count() > 0:
                message += '\n\n<b>&#11088; First : </b>' + first.first_name + ' ' + first.last_name + \
                           ' (' + updates[0].timestamp.astimezone(timezone('Asia/Kolkata')).strftime(
                    '%I:%M %p') + ')' + '\n'
                message += '<b>&#128012; Last : </b>' + last.first_name + ' ' + last.last_name + \
                           ' (' + list(reversed(updates))[0].timestamp.astimezone(timezone('Asia/Kolkata')).strftime(
                    '%I:%M %p') + ')' + '\n'
            message += self.generateDidNotSendReport(log.didNotSend)
            if thread.footerMessage:
                message += '\n<i>' + thread.footerMessage + '</i>'

            return message

        except ObjectDoesNotExist:
            raise 
Example #13
Source File: StatusUpdateReporter.py    From cms with GNU General Public License v3.0 5 votes vote down vote up
def getLateReport(self, late_members):
        lateCount = late_members.count()
        message = ''
        if lateCount > 0:
            message += '\n\n<b>&#8987; LATE (' + str(lateCount) + ') : </b> \n\n'
            i = 0
            for member in late_members.all():
                i = i + 1
                timestamp = Message.objects.filter(thread=self.thread, member=member, date=self.date).order_by(
                    '-timestamp').first().timestamp
                message += str(i) + '. ' + self.getName(member) + ' [' + timestamp.astimezone(
                    timezone('Asia/Kolkata')).strftime('%I:%M %p') + '] \n'
        return message 
Example #14
Source File: StatusUpdateReporter.py    From cms with GNU General Public License v3.0 5 votes vote down vote up
def generateDidNotSendReport(self, members):
        now = datetime.now()
        year = int(now.strftime("%Y"))
        didNotSendCount = members.count()
        message = ''
        if didNotSendCount > 0:
            message = '\n\n<b>&#128561; DID NOT SEND (' + str(didNotSendCount) + ') : </b> \n'
            message += self.generateBatchWiseDNSReport(members, year)
            message += self.generateBatchWiseDNSReport(members, year - 1)
            message += self.generateBatchWiseDNSReport(members, year - 2)
            message += self.generateBatchWiseDNSReport(members, year - 3)
        return message 
Example #15
Source File: StatusUpdateReporter.py    From cms with GNU General Public License v3.0 5 votes vote down vote up
def getBatchName(y):
        now = datetime.now()
        year = int(now.strftime("%Y"))
        if y + 1 == year:
            return 'First Year Batch'
        elif y + 2 == year:
            return 'Second Year Batch'
        elif y + 3 == year:
            return 'Third Year Batch'
        elif y + 4 == year:
            return 'Fourth Year Batch' 
Example #16
Source File: bdbag_api.py    From bdbag with Apache License 2.0 5 votes vote down vote up
def extract_bag(bag_path, output_path=None, temp=False):
    if not os.path.exists(bag_path):
        raise RuntimeError("Specified bag path not found: %s" % bag_path)

    bag_dir = os.path.splitext(os.path.basename(bag_path))[0]
    if os.path.isfile(bag_path):
        if temp:
            output_path = tempfile.mkdtemp(prefix='bag_')
        elif not output_path:
            output_path = os.path.splitext(bag_path)[0]
            if os.path.exists(output_path):
                newpath = ''.join([output_path, '-', datetime.strftime(datetime.now(), "%Y-%m-%d_%H.%M.%S")])
                logger.info("Specified output path %s already exists, moving existing directory to %s" %
                            (output_path, newpath))
                shutil.move(output_path, newpath)
            output_path = os.path.dirname(bag_path)
        if zipfile.is_zipfile(bag_path):
            logger.info("Extracting ZIP archived file: %s" % bag_path)
            with open(bag_path, 'rb') as bag_file:
                zipped = zipfile.ZipFile(bag_file)
                zipped.extractall(output_path)
                zipped.close()
        elif tarfile.is_tarfile(bag_path):
            logger.info("Extracting TAR/GZ/BZ2 archived file: %s" % bag_path)
            tarred = tarfile.open(bag_path)
            tarred.extractall(output_path)
            tarred.close()
        else:
            raise RuntimeError("Archive format not supported for file: %s"
                               "\nSupported archive formats are ZIP or TAR/GZ/BZ2" % bag_path)

    extracted_path = os.path.join(output_path, bag_dir)
    logger.info("File %s was successfully extracted to directory %s" % (bag_path, extracted_path))

    return extracted_path 
Example #17
Source File: bdbag_api.py    From bdbag with Apache License 2.0 5 votes vote down vote up
def cleanup_bag(bag_path, save=False):
    logger.info("Cleaning up bag dir: %s" % bag_path)
    if save:
        saved_bag_path = ''.join([bag_path, '_', datetime.strftime(datetime.now(), "%Y-%m-%d_%H.%M.%S")])
        logger.info("Moving bag %s to %s" % (bag_path, saved_bag_path))
        shutil.move(bag_path, saved_bag_path)
        return saved_bag_path
    else:
        shutil.rmtree(bag_path)
        return None 
Example #18
Source File: __init__.py    From home-assistant-custom-components with MIT License 5 votes vote down vote up
def format_date(self, date):
        if self._date_format == "None":
            return date
        return date.strftime(self._date_format) 
Example #19
Source File: backupSnapshot.py    From MozDef with Mozilla Public License 2.0 4 votes vote down vote up
def main():
    logger.debug('started')

    json_headers = {
        'Content-Type': 'application/json',
    }
    try:
        esserver = options.esservers[0]
        idate = date.strftime(datetime.utcnow() - timedelta(days=1), '%Y%m%d')
        bucketdate = date.strftime(datetime.utcnow() - timedelta(days=1), '%Y-%m')
        hostname = socket.gethostname()

        # Create or update snapshot configuration
        logger.debug('Configuring snapshot repository')
        snapshot_config = {
            "type": "s3",
            "settings": {
                "bucket": options.aws_bucket,
                "base_path": "elasticsearch/{0}/{1}".format(bucketdate, hostname)
            }
        }
        r = requests.put('%s/_snapshot/s3backup' % esserver, headers=json_headers, data=json.dumps(snapshot_config))
        if 'status' in r.json():
            logger.error("Error while registering snapshot repo: %s" % r.text)
        else:
            logger.debug('snapshot repo registered')

        # do the actual snapshotting
        for (index, dobackup, rotation, pruning) in zip(options.indices, options.dobackup, options.rotation, options.pruning):
            if dobackup == '1':
                index_to_snapshot = index
                if rotation == 'daily':
                    index_to_snapshot += '-%s' % idate
                elif rotation == 'monthly':
                    index_to_snapshot += '-%s' % idate[:6]

                logger.debug('Creating %s snapshot (this may take a while)...' % index_to_snapshot)
                snapshot_config = {
                    'indices': index_to_snapshot
                }
                epoch = calendar.timegm(datetime.utcnow().utctimetuple())
                r = requests.put(
                    '{0}/_snapshot/s3backup/{1}-{2}?wait_for_completion=true'.format(esserver, index_to_snapshot, epoch),
                    headers=json_headers,
                    data=json.dumps(snapshot_config)
                )
                if 'status' in r.json():
                    logger.error('Error snapshotting %s: %s' % (index_to_snapshot, r.json()))
                else:
                    logger.debug('snapshot %s finished' % index_to_snapshot)

    except Exception as e:
        logger.error("Unhandled exception, terminating: %r" % e) 
Example #20
Source File: CrudContaAReceber.py    From controleEstoque with MIT License 4 votes vote down vote up
def listaContaAReceber(self):

        try:

            # Abrindo Sessao
            conecta = Conexao()
            sessao = conecta.Session()

            # Query
            self.query = (sessao.query(ContaAReceber.__table__,
                                       Cliente.nome,
                                       Cliente.celular,
                                       StatusPagamento.status_pagamento)
                          .join(Cliente)
                          .join(StatusPagamento)
                          .filter(ContaAReceber.data_vencimento.between(
                              self.dataVencimento, self.dataFim),
                ContaAReceber.pagamento == self.statusPagamento)
            )

            # Convertendo variaveis em lista
            self.id = []
            self.nomeCliente = []
            self.telefoneCliente = []
            self.descricao = []
            self.dataVencimento = []
            self.valor = []
            self.valorRecebido = []
            self.idStatusPagamento = []
            self.statusPagamento = []

            # salvando resultado em suas listas
            for row in self.query:

                self.id.append(row.id)
                self.nomeCliente.append(row.nome)
                self.telefoneCliente.append(row.celular)
                self.descricao.append(row.descricao)
                self.dataVencimento.append(
                    date.strftime(row.data_vencimento, "%d-%m-%Y"))
                self.valor.append(row.valor)
                self.valorRecebido.append(row.valor_recebido)
                self.idStatusPagamento.append(row.pagamento)
                self.statusPagamento.append(row.status_pagamento)

            # Fechando a Conexao
            sessao.close()

        except IntegrityError as err:
            print(err)

        pass

    # Selecionando conta a receber por ID 
Example #21
Source File: CrudContaAPagar.py    From controleEstoque with MIT License 4 votes vote down vote up
def listaContaAPagar(self):

        try:

            # Abrindo Sessao
            conecta = Conexao()
            sessao = conecta.Session()

            # Query
            self.query = (sessao.query(ContaAPagar.__table__,
                                       Fornecedor.nome_fantasia,
                                       Fornecedor.telefone,
                                       StatusPagamento.status_pagamento)
                          .join(Fornecedor)
                          .join(StatusPagamento)
                          .filter(ContaAPagar.data_vencimento.between(
                              self.dataVencimento, self.dataFim),
                ContaAPagar.pagamento == self.statusPagamento)
            )
            self.query.all()

            # Convertendo variaveis em lista
            self.id = []
            self.nomeFantasia = []
            self.telefone = []
            self.descricao = []
            self.dataVencimento = []
            self.valor = []
            self.valorPago = []
            self.idStatusPagamento = []
            self.statusPagamento = []

            # salvando resultado em suas listas
            for row in self.query:

                self.id.append(row.id)
                self.nomeFantasia.append(row.nome_fantasia)
                self.telefone.append(row.telefone)
                self.descricao.append(row.descricao)
                self.dataVencimento.append(
                    date.strftime(row.data_vencimento, "%d-%m-%Y"))
                self.valor.append(row.valor)
                self.valorPago.append(row.valor_pago)
                self.idStatusPagamento.append(row.pagamento)
                self.statusPagamento.append(row.status_pagamento)

            # Fechando a Conexao
            sessao.close()

        except IntegrityError as err:
            print(err)

    # Selecionando conta a Pagar por ID